From b11f8c5710e4d7810b14ddf8cefee17adc87e03c Mon Sep 17 00:00:00 2001 From: MrGadget1024 Date: Fri, 10 Dec 2021 11:12:41 -0500 Subject: [PATCH] Updated Basic Example - Moved some code from PlayerUI to Player - smaller data types - organization --- .../Mirror/Examples/Basic/Scripts/Player.cs | 69 +++++++++++-------- .../Mirror/Examples/Basic/Scripts/PlayerUI.cs | 28 ++------ 2 files changed, 47 insertions(+), 50 deletions(-) diff --git a/Assets/Mirror/Examples/Basic/Scripts/Player.cs b/Assets/Mirror/Examples/Basic/Scripts/Player.cs index 8f1788a44..582874622 100644 --- a/Assets/Mirror/Examples/Basic/Scripts/Player.cs +++ b/Assets/Mirror/Examples/Basic/Scripts/Player.cs @@ -6,16 +6,16 @@ namespace Mirror.Examples.Basic public class Player : NetworkBehaviour { // Events that the PlayerUI will subscribe to - public event System.Action OnPlayerNumberChanged; + public event System.Action OnPlayerNumberChanged; public event System.Action OnPlayerColorChanged; - public event System.Action OnPlayerDataChanged; + public event System.Action OnPlayerDataChanged; // Players List to manage playerNumber static readonly List playersList = new List(); [Header("Player UI")] public GameObject playerUIPrefab; - GameObject playerUI; + GameObject playerUIObject; #region SyncVars @@ -25,13 +25,7 @@ public class Player : NetworkBehaviour /// This is appended to the player name text, e.g. "Player 01" /// [SyncVar(hook = nameof(PlayerNumberChanged))] - public int playerNumber = 0; - - /// - /// This is updated by UpdateData which is called from OnStartServer via InvokeRepeating - /// - [SyncVar(hook = nameof(PlayerDataChanged))] - public int playerData = 0; + public byte playerNumber = 0; /// /// Random color for the playerData text, assigned in OnStartServer @@ -39,24 +33,30 @@ public class Player : NetworkBehaviour [SyncVar(hook = nameof(PlayerColorChanged))] public Color32 playerColor = Color.white; + /// + /// This is updated by UpdateData which is called from OnStartServer via InvokeRepeating + /// + [SyncVar(hook = nameof(PlayerDataChanged))] + public ushort playerData = 0; + // This is called by the hook of playerNumber SyncVar above - void PlayerNumberChanged(int _, int newPlayerNumber) + void PlayerNumberChanged(byte _, byte 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); } + // This is called by the hook of playerData SyncVar above + void PlayerDataChanged(ushort _, ushort newPlayerData) + { + OnPlayerDataChanged?.Invoke(newPlayerData); + } + #endregion #region Server @@ -76,6 +76,9 @@ public override void OnStartServer() // set the Player Color SyncVar playerColor = Random.ColorHSV(0f, 1f, 0.9f, 0.9f, 1f, 1f); + // set the initial player data + playerData = (ushort)Random.Range(100, 1000); + // Start generating updates InvokeRepeating(nameof(UpdateData), 1, 1); } @@ -85,7 +88,7 @@ public override void OnStartServer() [ServerCallback] internal static void ResetPlayerNumbers() { - int playerNumber = 0; + byte playerNumber = 0; foreach (Player player in playersList) player.playerNumber = playerNumber++; } @@ -94,7 +97,7 @@ internal static void ResetPlayerNumbers() [ServerCallback] void UpdateData() { - playerData = Random.Range(100, 1000); + playerData = (ushort)Random.Range(100, 1000); } /// @@ -128,15 +131,22 @@ public override void OnStartLocalPlayer() public override void OnStartClient() { // Instantiate the player UI as child of the Players Panel - playerUI = Instantiate(playerUIPrefab, CanvasUI.instance.playersPanel); + playerUIObject = Instantiate(playerUIPrefab, CanvasUI.instance.playersPanel); + PlayerUI playerUI = playerUIObject.GetComponent(); - // Set this player object in PlayerUI to wire up event handlers - playerUI.GetComponent().SetPlayer(this, isLocalPlayer); + // wire up all events to handlers in PlayerUI + OnPlayerNumberChanged = playerUI.OnPlayerNumberChanged; + OnPlayerColorChanged = playerUI.OnPlayerColorChanged; + OnPlayerDataChanged = playerUI.OnPlayerDataChanged; - // Invoke all event handlers with the current data - OnPlayerNumberChanged?.Invoke(playerNumber); - OnPlayerColorChanged?.Invoke(playerColor); - OnPlayerDataChanged?.Invoke(playerData); + // Invoke all event handlers with the initial data from spawn payload + OnPlayerNumberChanged.Invoke(playerNumber); + OnPlayerColorChanged.Invoke(playerColor); + OnPlayerDataChanged.Invoke(playerData); + + // Set isLocalPlayer for this Player in UI for background shading + if (isLocalPlayer) + playerUI.SetLocalPlayer(); } /// @@ -145,8 +155,13 @@ public override void OnStartClient() /// public override void OnStopClient() { + // disconnect event handlers + OnPlayerNumberChanged = null; + OnPlayerColorChanged = null; + OnPlayerDataChanged = null; + // Remove this player's UI object - Destroy(playerUI); + Destroy(playerUIObject); // Disable the main panel for local player if (isLocalPlayer) diff --git a/Assets/Mirror/Examples/Basic/Scripts/PlayerUI.cs b/Assets/Mirror/Examples/Basic/Scripts/PlayerUI.cs index a67cc86dd..2d41ce498 100644 --- a/Assets/Mirror/Examples/Basic/Scripts/PlayerUI.cs +++ b/Assets/Mirror/Examples/Basic/Scripts/PlayerUI.cs @@ -12,49 +12,31 @@ public class PlayerUI : MonoBehaviour public Text playerNameText; public Text playerDataText; - Player player; - /// /// Caches the controlling Player object, subscribes to its events /// /// Player object that controls this UI /// true if the Player object is the Local Player - public void SetPlayer(Player player, bool isLocalPlayer) + public void SetLocalPlayer() { - // 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; + image.color = new Color(1f, 1f, 1f, 0.1f); } // This value can change as clients leave and join - void OnPlayerNumberChanged(int newPlayerNumber) + public void OnPlayerNumberChanged(byte newPlayerNumber) { playerNameText.text = string.Format("Player {0:00}", newPlayerNumber); } // Random color set by Player::OnStartServer - void OnPlayerColorChanged(Color32 newPlayerColor) + public void OnPlayerColorChanged(Color32 newPlayerColor) { playerNameText.color = newPlayerColor; } // This updates from Player::UpdateData via InvokeRepeating on server - void OnPlayerDataChanged(int newPlayerData) + public void OnPlayerDataChanged(ushort newPlayerData) { // Show the data in the UI playerDataText.text = string.Format("Data: {0:000}", newPlayerData);