CharacterSelection.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using UnityEngine;
  2. using Mirror;
  3. namespace Mirror.Examples.CharacterSelection
  4. {
  5. public class CharacterSelection : NetworkBehaviour
  6. {
  7. public Transform floatingInfo;
  8. [SyncVar]
  9. public int characterNumber = 0;
  10. public TextMesh textMeshName;
  11. [SyncVar(hook = nameof(HookSetName))]
  12. public string playerName = "";
  13. void HookSetName(string _old, string _new)
  14. {
  15. //Debug.Log("HookSetName");
  16. AssignName();
  17. }
  18. [SyncVar(hook = nameof(HookSetColor))]
  19. public Color characterColour;
  20. private Material cachedMaterial;
  21. public MeshRenderer[] characterRenderers;
  22. void HookSetColor(Color _old, Color _new)
  23. {
  24. //Debug.Log("HookSetColor");
  25. AssignColours();
  26. }
  27. public void AssignColours()
  28. {
  29. foreach (MeshRenderer meshRenderer in characterRenderers)
  30. {
  31. cachedMaterial = meshRenderer.material;
  32. cachedMaterial.color = characterColour;
  33. }
  34. }
  35. void OnDestroy()
  36. {
  37. if (cachedMaterial) { Destroy(cachedMaterial); }
  38. }
  39. public void AssignName()
  40. {
  41. textMeshName.text = playerName;
  42. }
  43. // To change server controlled sync vars, clients end Commands, and the hooks will fire
  44. // Although not used in this example, we could change some character aspects without replacing current prefab.
  45. //[Command]
  46. //public void CmdSetupCharacter(string _playerName, Color _characterColour)
  47. //{
  48. // playerName = _playerName;
  49. // characterColour = _characterColour;
  50. //}
  51. }
  52. }