Player.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace Mirror.Examples.Basic
  4. {
  5. public class Player : NetworkBehaviour
  6. {
  7. // Events that the UI will subscribe to
  8. public event System.Action<int> OnPlayerNumberChanged;
  9. public event System.Action<Color32> OnPlayerColorChanged;
  10. public event System.Action<int> OnPlayerDataChanged;
  11. // Players List to manage playerNumber
  12. internal static readonly List<Player> playersList = new List<Player>();
  13. internal static void ResetPlayerNumbers()
  14. {
  15. int playerNumber = 0;
  16. foreach (Player player in playersList)
  17. {
  18. player.playerNumber = playerNumber++;
  19. }
  20. }
  21. [Header("Player UI")]
  22. public GameObject playerUIPrefab;
  23. GameObject playerUI;
  24. [Header("SyncVars")]
  25. /// <summary>
  26. /// This is appended to the player name text, e.g. "Player 01"
  27. /// </summary>
  28. [SyncVar(hook = nameof(PlayerNumberChanged))]
  29. public int playerNumber = 0;
  30. /// <summary>
  31. /// This is updated by UpdateData which is called from OnStartServer via InvokeRepeating
  32. /// </summary>
  33. [SyncVar(hook = nameof(PlayerDataChanged))]
  34. public int playerData = 0;
  35. /// <summary>
  36. /// Random color for the playerData text, assigned in OnStartServer
  37. /// </summary>
  38. [SyncVar(hook = nameof(PlayerColorChanged))]
  39. public Color32 playerColor = Color.white;
  40. // This is called by the hook of playerNumber SyncVar above
  41. void PlayerNumberChanged(int _, int newPlayerNumber)
  42. {
  43. OnPlayerNumberChanged?.Invoke(newPlayerNumber);
  44. }
  45. // This is called by the hook of playerData SyncVar above
  46. void PlayerDataChanged(int _, int newPlayerData)
  47. {
  48. OnPlayerDataChanged?.Invoke(newPlayerData);
  49. }
  50. // This is called by the hook of playerColor SyncVar above
  51. void PlayerColorChanged(Color32 _, Color32 newPlayerColor)
  52. {
  53. OnPlayerColorChanged?.Invoke(newPlayerColor);
  54. }
  55. /// <summary>
  56. /// This is invoked for NetworkBehaviour objects when they become active on the server.
  57. /// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
  58. /// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
  59. /// </summary>
  60. public override void OnStartServer()
  61. {
  62. base.OnStartServer();
  63. // Add this to the static Players List
  64. playersList.Add(this);
  65. // set the Player Color SyncVar
  66. playerColor = Random.ColorHSV(0f, 1f, 0.9f, 0.9f, 1f, 1f);
  67. // Start generating updates
  68. InvokeRepeating(nameof(UpdateData), 1, 1);
  69. }
  70. /// <summary>
  71. /// Invoked on the server when the object is unspawned
  72. /// <para>Useful for saving object data in persistent storage</para>
  73. /// </summary>
  74. public override void OnStopServer()
  75. {
  76. CancelInvoke();
  77. playersList.Remove(this);
  78. }
  79. // This only runs on the server, called from OnStartServer via InvokeRepeating
  80. [ServerCallback]
  81. void UpdateData()
  82. {
  83. playerData = Random.Range(100, 1000);
  84. }
  85. /// <summary>
  86. /// Called on every NetworkBehaviour when it is activated on a client.
  87. /// <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>
  88. /// </summary>
  89. public override void OnStartClient()
  90. {
  91. // Activate the main panel
  92. ((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(true);
  93. // Instantiate the player UI as child of the Players Panel
  94. playerUI = Instantiate(playerUIPrefab, ((BasicNetManager)NetworkManager.singleton).playersPanel);
  95. // Set this player object in PlayerUI to wire up event handlers
  96. playerUI.GetComponent<PlayerUI>().SetPlayer(this, isLocalPlayer);
  97. // Invoke all event handlers with the current data
  98. OnPlayerNumberChanged.Invoke(playerNumber);
  99. OnPlayerColorChanged.Invoke(playerColor);
  100. OnPlayerDataChanged.Invoke(playerData);
  101. }
  102. /// <summary>
  103. /// This is invoked on clients when the server has caused this object to be destroyed.
  104. /// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
  105. /// </summary>
  106. public override void OnStopClient()
  107. {
  108. // Remove this player's UI object
  109. Destroy(playerUI);
  110. // Disable the main panel for local player
  111. if (isLocalPlayer)
  112. ((BasicNetManager)NetworkManager.singleton).mainPanel.gameObject.SetActive(false);
  113. }
  114. }
  115. }