CanvasReferencer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using UnityEngine.UI;
  4. using static Mirror.Examples.CharacterSelection.NetworkManagerCharacterSelection;
  5. namespace Mirror.Examples.CharacterSelection
  6. {
  7. public class CanvasReferencer : MonoBehaviour
  8. {
  9. // Make sure to attach these Buttons in the Inspector
  10. public Button buttonExit, buttonNextCharacter, buttonGo, buttonColour, buttonColourReset;
  11. public Text textTitle, textHealth, textSpeed, textAttack, textAbilities;
  12. public InputField inputFieldPlayerName;
  13. public Transform podiumPosition;
  14. private int currentlySelectedCharacter = 1;
  15. private CharacterData characterData;
  16. private GameObject currentInstantiatedCharacter;
  17. private CharacterSelection characterSelection;
  18. public SceneReferencer sceneReferencer;
  19. public Camera cameraObj;
  20. private void Start()
  21. {
  22. characterData = CharacterData.characterDataSingleton;
  23. if (characterData == null)
  24. {
  25. Debug.Log("Add CharacterData prefab singleton into the scene.");
  26. return;
  27. }
  28. buttonExit.onClick.AddListener(ButtonExit);
  29. buttonNextCharacter.onClick.AddListener(ButtonNextCharacter);
  30. buttonGo.onClick.AddListener(ButtonGo);
  31. buttonColour.onClick.AddListener(ButtonColour);
  32. buttonColourReset.onClick.AddListener(ButtonColourReset);
  33. //Adds a listener to the main input field and invokes a method when the value changes.
  34. inputFieldPlayerName.onValueChanged.AddListener(delegate { InputFieldChangedPlayerName(); });
  35. LoadData();
  36. SetupCharacters();
  37. }
  38. public void ButtonExit()
  39. {
  40. //Debug.Log("ButtonExit");
  41. if (sceneReferencer)
  42. {
  43. sceneReferencer.CloseCharacterSelection();
  44. }
  45. }
  46. public void ButtonGo()
  47. {
  48. //Debug.Log("ButtonGo");
  49. // presumes we're already in-game
  50. if (sceneReferencer && NetworkClient.active)
  51. {
  52. // You could check if prefab (character number) has not changed, and if so just update the sync vars and hooks of current prefab, this would call a command from your player.
  53. // this is not fully setup for this example, but provides a minor template to follow if needed
  54. //NetworkClient.localPlayer.GetComponent<CharacterSelection>().CmdSetupCharacter(StaticVariables.playerName, StaticVariables.characterColour);
  55. CreateCharacterMessage _characterMessage = new CreateCharacterMessage
  56. {
  57. playerName = StaticVariables.playerName,
  58. characterNumber = StaticVariables.characterNumber,
  59. characterColour = StaticVariables.characterColour
  60. };
  61. ReplaceCharacterMessage replaceCharacterMessage = new ReplaceCharacterMessage
  62. {
  63. createCharacterMessage = _characterMessage
  64. };
  65. NetworkManagerCharacterSelection.singleton.ReplaceCharacter(replaceCharacterMessage);
  66. sceneReferencer.CloseCharacterSelection();
  67. }
  68. else
  69. {
  70. // not in-game
  71. SceneManager.LoadScene("MirrorCharacterSelection");
  72. }
  73. }
  74. public void ButtonNextCharacter()
  75. {
  76. //Debug.Log("ButtonNextCharacter");
  77. currentlySelectedCharacter += 1;
  78. if (currentlySelectedCharacter >= characterData.characterPrefabs.Length)
  79. {
  80. currentlySelectedCharacter = 1;
  81. }
  82. SetupCharacters();
  83. StaticVariables.characterNumber = currentlySelectedCharacter;
  84. }
  85. public void ButtonColour()
  86. {
  87. //Debug.Log("ButtonColour");
  88. StaticVariables.characterColour = Random.ColorHSV(0f, 1f, 1f, 1f, 0f, 1f);
  89. SetupCharacterColours();
  90. }
  91. public void ButtonColourReset()
  92. {
  93. //Debug.Log("ButtonColourReset ");
  94. StaticVariables.characterColour = new Color(0, 0, 0, 0);
  95. SetupCharacters();
  96. }
  97. private void SetupCharacters()
  98. {
  99. textTitle.text = "" + characterData.characterTitles[currentlySelectedCharacter];
  100. textHealth.text = "Health: " + characterData.characterHealths[currentlySelectedCharacter];
  101. textSpeed.text = "Speed: " + characterData.characterSpeeds[currentlySelectedCharacter];
  102. textAttack.text = "Attack: " + characterData.characterAttack[currentlySelectedCharacter];
  103. textAbilities.text = "Abilities:\n" + characterData.characterAbilities[currentlySelectedCharacter];
  104. if (currentInstantiatedCharacter)
  105. {
  106. Destroy(currentInstantiatedCharacter);
  107. }
  108. currentInstantiatedCharacter = Instantiate(characterData.characterPrefabs[currentlySelectedCharacter]);
  109. currentInstantiatedCharacter.transform.position = podiumPosition.position;
  110. currentInstantiatedCharacter.transform.rotation = podiumPosition.rotation;
  111. characterSelection = currentInstantiatedCharacter.GetComponent<CharacterSelection>();
  112. currentInstantiatedCharacter.transform.SetParent(this.transform.root);
  113. SetupCharacterColours();
  114. SetupPlayerName();
  115. if (cameraObj)
  116. {
  117. characterSelection.floatingInfo.forward = cameraObj.transform.forward;
  118. }
  119. }
  120. public void SetupCharacterColours()
  121. {
  122. // Debug.Log("SetupCharacterColours");
  123. if (StaticVariables.characterColour != new Color(0, 0, 0, 0))
  124. {
  125. characterSelection.characterColour = StaticVariables.characterColour;
  126. characterSelection.AssignColours();
  127. }
  128. }
  129. public void InputFieldChangedPlayerName()
  130. {
  131. //Debug.Log("InputFieldChangedPlayerName");
  132. StaticVariables.playerName = inputFieldPlayerName.text;
  133. SetupPlayerName();
  134. }
  135. public void SetupPlayerName()
  136. {
  137. //Debug.Log("SetupPlayerName");
  138. if (characterSelection)
  139. {
  140. characterSelection.playerName = StaticVariables.playerName;
  141. characterSelection.AssignName();
  142. }
  143. }
  144. public void LoadData()
  145. {
  146. // check if the static save data has been pre-set
  147. if (StaticVariables.playerName != "")
  148. {
  149. if (inputFieldPlayerName)
  150. {
  151. inputFieldPlayerName.text = StaticVariables.playerName;
  152. }
  153. }
  154. else
  155. {
  156. StaticVariables.playerName = "Player Name";
  157. }
  158. // check that prefab is set, or exists for saved character number data
  159. if (StaticVariables.characterNumber > 0 && StaticVariables.characterNumber < characterData.characterPrefabs.Length)
  160. {
  161. currentlySelectedCharacter = StaticVariables.characterNumber;
  162. }
  163. else
  164. {
  165. StaticVariables.characterNumber = currentlySelectedCharacter;
  166. }
  167. }
  168. }
  169. }