Gonna move to server authority

This commit is contained in:
2022-01-31 17:27:38 +05:30
parent f3d21f4ec6
commit 7368968176
1354 changed files with 107808 additions and 80043 deletions

View File

@@ -1,44 +1,44 @@
using UnityEngine;
/*
Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
*/
namespace Mirror.Examples.Basic
{
[AddComponentMenu("")]
public class BasicNetManager : NetworkManager
{
[Header("Canvas UI")]
[Tooltip("Assign Main Panel so it can be turned on from Player:OnStartClient")]
public RectTransform mainPanel;
[Tooltip("Assign Players Panel for instantiating PlayerUI as child")]
public RectTransform playersPanel;
/// <summary>
/// Called on the server when a client adds a new player with NetworkClient.AddPlayer.
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerAddPlayer(NetworkConnection conn)
{
base.OnServerAddPlayer(conn);
Player.ResetPlayerNumbers();
}
/// <summary>
/// Called on the server when a client disconnects.
/// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerDisconnect(NetworkConnection conn)
{
base.OnServerDisconnect(conn);
Player.ResetPlayerNumbers();
}
}
}
using UnityEngine;
/*
Documentation: https://mirror-networking.gitbook.io/docs/components/network-manager
API Reference: https://mirror-networking.com/docs/api/Mirror.NetworkManager.html
*/
namespace Mirror.Examples.Basic
{
[AddComponentMenu("")]
public class BasicNetManager : NetworkManager
{
[Header("Canvas UI")]
[Tooltip("Assign Main Panel so it can be turned on from Player:OnStartClient")]
public RectTransform mainPanel;
[Tooltip("Assign Players Panel for instantiating PlayerUI as child")]
public RectTransform playersPanel;
/// <summary>
/// Called on the server when a client adds a new player with NetworkClient.AddPlayer.
/// <para>The default implementation for this function creates a new player object from the playerPrefab.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerAddPlayer(NetworkConnection conn)
{
base.OnServerAddPlayer(conn);
Player.ResetPlayerNumbers();
}
/// <summary>
/// Called on the server when a client disconnects.
/// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para>
/// </summary>
/// <param name="conn">Connection from client.</param>
public override void OnServerDisconnect(NetworkConnection conn)
{
base.OnServerDisconnect(conn);
Player.ResetPlayerNumbers();
}
}
}

View File

@@ -1,11 +1,11 @@
fileFormatVersion: 2
guid: 20460c43f0320ed4baf8c1dcf953eafa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 20460c43f0320ed4baf8c1dcf953eafa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData: ''
assetBundleName: ''
assetBundleVariant: ''

View File

@@ -1,138 +1,138 @@
using System.Collections.Generic;
using UnityEngine;
namespace Mirror.Examples.Basic
{
public class Player : NetworkBehaviour
{
// Events that the UI will subscribe to
public event System.Action<int> OnPlayerNumberChanged;
public event System.Action<Color32> OnPlayerColorChanged;
public event System.Action<int> OnPlayerDataChanged;
// Players List to manage playerNumber
internal static readonly List<Player> playersList = new List<Player>();
internal static void ResetPlayerNumbers()
{
int playerNumber = 0;
foreach (Player player in playersList)
{
player.playerNumber = playerNumber++;
}
}
[Header("Player UI")]
public GameObject playerUIPrefab;
GameObject playerUI;
[Header("SyncVars")]
/// <summary>
/// This is appended to the player name text, e.g. "Player 01"
/// </summary>
[SyncVar(hook = nameof(PlayerNumberChanged))]
public int playerNumber = 0;
/// <summary>
/// This is updated by UpdateData which is called from OnStartServer via InvokeRepeating
/// </summary>
[SyncVar(hook = nameof(PlayerDataChanged))]
public int playerData = 0;
/// <summary>
/// Random color for the playerData text, assigned in OnStartServer
/// </summary>
[SyncVar(hook = nameof(PlayerColorChanged))]
public Color32 playerColor = Color.white;
// This is called by the hook of playerNumber SyncVar above
void PlayerNumberChanged(int _, int newPlayerNumber)
{
OnPlayerNumberChanged?.Invoke(newPlayerNumber);
}
// This is called by the hook of playerData SyncVar above
void PlayerDataChanged(int _, int newPlayerData)
{
OnPlayerDataChanged?.Invoke(newPlayerData);
}
// This is called by the hook of playerColor SyncVar above
void PlayerColorChanged(Color32 _, Color32 newPlayerColor)
{
OnPlayerColorChanged?.Invoke(newPlayerColor);
}
/// <summary>
/// This is invoked for NetworkBehaviour objects when they become active on the server.
/// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
/// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
/// </summary>
public override void OnStartServer()
{
base.OnStartServer();
// Add this to the static Players List
playersList.Add(this);
// set the Player Color SyncVar
playerColor = Random.ColorHSV(0f, 1f, 0.9f, 0.9f, 1f, 1f);
// Start generating updates
InvokeRepeating(nameof(UpdateData), 1, 1);
}
/// <summary>
/// Invoked on the server when the object is unspawned
/// <para>Useful for saving object data in persistent storage</para>
/// </summary>
public override void OnStopServer()
{
CancelInvoke();
playersList.Remove(this);
}
// This only runs on the server, called from OnStartServer via InvokeRepeating
[ServerCallback]
void UpdateData()
{
playerData = Random.Range(100, 1000);
}
/// <summary>
/// Called on every NetworkBehaviour when it is activated on a client.
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
/// </summary>
public override void OnStartClient()
{
// Activate the main panel
((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(true);
// Instantiate the player UI as child of the Players Panel
playerUI = Instantiate(playerUIPrefab, ((BasicNetManager)NetworkManager.singleton).playersPanel);
// Set this player object in PlayerUI to wire up event handlers
playerUI.GetComponent<PlayerUI>().SetPlayer(this, isLocalPlayer);
// Invoke all event handlers with the current data
OnPlayerNumberChanged.Invoke(playerNumber);
OnPlayerColorChanged.Invoke(playerColor);
OnPlayerDataChanged.Invoke(playerData);
}
/// <summary>
/// This is invoked on clients when the server has caused this object to be destroyed.
/// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
/// </summary>
public override void OnStopClient()
{
// Remove this player's UI object
Destroy(playerUI);
// Disable the main panel for local player
if (isLocalPlayer)
((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(false);
}
}
}
using System.Collections.Generic;
using UnityEngine;
namespace Mirror.Examples.Basic
{
public class Player : NetworkBehaviour
{
// Events that the UI will subscribe to
public event System.Action<int> OnPlayerNumberChanged;
public event System.Action<Color32> OnPlayerColorChanged;
public event System.Action<int> OnPlayerDataChanged;
// Players List to manage playerNumber
internal static readonly List<Player> playersList = new List<Player>();
internal static void ResetPlayerNumbers()
{
int playerNumber = 0;
foreach (Player player in playersList)
{
player.playerNumber = playerNumber++;
}
}
[Header("Player UI")]
public GameObject playerUIPrefab;
GameObject playerUI;
[Header("SyncVars")]
/// <summary>
/// This is appended to the player name text, e.g. "Player 01"
/// </summary>
[SyncVar(hook = nameof(PlayerNumberChanged))]
public int playerNumber = 0;
/// <summary>
/// This is updated by UpdateData which is called from OnStartServer via InvokeRepeating
/// </summary>
[SyncVar(hook = nameof(PlayerDataChanged))]
public int playerData = 0;
/// <summary>
/// Random color for the playerData text, assigned in OnStartServer
/// </summary>
[SyncVar(hook = nameof(PlayerColorChanged))]
public Color32 playerColor = Color.white;
// This is called by the hook of playerNumber SyncVar above
void PlayerNumberChanged(int _, int newPlayerNumber)
{
OnPlayerNumberChanged?.Invoke(newPlayerNumber);
}
// This is called by the hook of playerData SyncVar above
void PlayerDataChanged(int _, int newPlayerData)
{
OnPlayerDataChanged?.Invoke(newPlayerData);
}
// This is called by the hook of playerColor SyncVar above
void PlayerColorChanged(Color32 _, Color32 newPlayerColor)
{
OnPlayerColorChanged?.Invoke(newPlayerColor);
}
/// <summary>
/// This is invoked for NetworkBehaviour objects when they become active on the server.
/// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
/// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
/// </summary>
public override void OnStartServer()
{
base.OnStartServer();
// Add this to the static Players List
playersList.Add(this);
// set the Player Color SyncVar
playerColor = Random.ColorHSV(0f, 1f, 0.9f, 0.9f, 1f, 1f);
// Start generating updates
InvokeRepeating(nameof(UpdateData), 1, 1);
}
/// <summary>
/// Invoked on the server when the object is unspawned
/// <para>Useful for saving object data in persistent storage</para>
/// </summary>
public override void OnStopServer()
{
CancelInvoke();
playersList.Remove(this);
}
// This only runs on the server, called from OnStartServer via InvokeRepeating
[ServerCallback]
void UpdateData()
{
playerData = Random.Range(100, 1000);
}
/// <summary>
/// Called on every NetworkBehaviour when it is activated on a client.
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
/// </summary>
public override void OnStartClient()
{
// Activate the main panel
((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(true);
// Instantiate the player UI as child of the Players Panel
playerUI = Instantiate(playerUIPrefab, ((BasicNetManager)NetworkManager.singleton).playersPanel);
// Set this player object in PlayerUI to wire up event handlers
playerUI.GetComponent<PlayerUI>().SetPlayer(this, isLocalPlayer);
// Invoke all event handlers with the current data
OnPlayerNumberChanged.Invoke(playerNumber);
OnPlayerColorChanged.Invoke(playerColor);
OnPlayerDataChanged.Invoke(playerData);
}
/// <summary>
/// This is invoked on clients when the server has caused this object to be destroyed.
/// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
/// </summary>
public override void OnStopClient()
{
// Remove this player's UI object
Destroy(playerUI);
// Disable the main panel for local player
if (isLocalPlayer)
((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(false);
}
}
}

View File

@@ -1,11 +1,11 @@
fileFormatVersion: 2
guid: a472ac3ae1701d149861871cf416a46d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: a472ac3ae1701d149861871cf416a46d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData: ''
assetBundleName: ''
assetBundleVariant: ''

View File

@@ -1,64 +1,64 @@
using UnityEngine;
using UnityEngine.UI;
namespace Mirror.Examples.Basic
{
public class PlayerUI : MonoBehaviour
{
[Header("Player Components")]
public Image image;
[Header("Child Text Objects")]
public Text playerNameText;
public Text playerDataText;
Player player;
/// <summary>
/// Caches the controlling Player object, subscribes to its events
/// </summary>
/// <param name="player">Player object that controls this UI</param>
/// <param name="isLocalPlayer">true if the Player object is the Local Player</param>
public void SetPlayer(Player player, bool isLocalPlayer)
{
// cache reference to the player that controls this UI object
this.player = player;
// subscribe to the events raised by SyncVar Hooks on the Player object
player.OnPlayerNumberChanged += OnPlayerNumberChanged;
player.OnPlayerColorChanged += OnPlayerColorChanged;
player.OnPlayerDataChanged += OnPlayerDataChanged;
// add a visual background for the local player in the UI
if (isLocalPlayer)
image.color = new Color(1f, 1f, 1f, 0.1f);
}
void OnDisable()
{
player.OnPlayerNumberChanged -= OnPlayerNumberChanged;
player.OnPlayerColorChanged -= OnPlayerColorChanged;
player.OnPlayerDataChanged -= OnPlayerDataChanged;
}
// This value can change as clients leave and join
void OnPlayerNumberChanged(int newPlayerNumber)
{
playerNameText.text = string.Format("Player {0:00}", newPlayerNumber);
}
// Random color set by Player::OnStartServer
void OnPlayerColorChanged(Color32 newPlayerColor)
{
playerNameText.color = newPlayerColor;
}
// This updates from Player::UpdateData via InvokeRepeating on server
void OnPlayerDataChanged(int newPlayerData)
{
// Show the data in the UI
playerDataText.text = string.Format("Data: {0:000}", newPlayerData);
}
}
}
using UnityEngine;
using UnityEngine.UI;
namespace Mirror.Examples.Basic
{
public class PlayerUI : MonoBehaviour
{
[Header("Player Components")]
public Image image;
[Header("Child Text Objects")]
public Text playerNameText;
public Text playerDataText;
Player player;
/// <summary>
/// Caches the controlling Player object, subscribes to its events
/// </summary>
/// <param name="player">Player object that controls this UI</param>
/// <param name="isLocalPlayer">true if the Player object is the Local Player</param>
public void SetPlayer(Player player, bool isLocalPlayer)
{
// cache reference to the player that controls this UI object
this.player = player;
// subscribe to the events raised by SyncVar Hooks on the Player object
player.OnPlayerNumberChanged += OnPlayerNumberChanged;
player.OnPlayerColorChanged += OnPlayerColorChanged;
player.OnPlayerDataChanged += OnPlayerDataChanged;
// add a visual background for the local player in the UI
if (isLocalPlayer)
image.color = new Color(1f, 1f, 1f, 0.1f);
}
void OnDisable()
{
player.OnPlayerNumberChanged -= OnPlayerNumberChanged;
player.OnPlayerColorChanged -= OnPlayerColorChanged;
player.OnPlayerDataChanged -= OnPlayerDataChanged;
}
// This value can change as clients leave and join
void OnPlayerNumberChanged(int newPlayerNumber)
{
playerNameText.text = string.Format("Player {0:00}", newPlayerNumber);
}
// Random color set by Player::OnStartServer
void OnPlayerColorChanged(Color32 newPlayerColor)
{
playerNameText.color = newPlayerColor;
}
// This updates from Player::UpdateData via InvokeRepeating on server
void OnPlayerDataChanged(int newPlayerData)
{
// Show the data in the UI
playerDataText.text = string.Format("Data: {0:000}", newPlayerData);
}
}
}

View File

@@ -1,11 +1,11 @@
fileFormatVersion: 2
guid: 64acca8c87e5ceb44bcbd56ef21e2950
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 64acca8c87e5ceb44bcbd56ef21e2950
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData: ''
assetBundleName: ''
assetBundleVariant: ''