You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.9 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using Networking;
  6. using Networking.Server;
  7. public class LobbyUIController : MonoBehaviour
  8. {
  9. [SerializeField]
  10. private ClientList Clients;
  11. [SerializeField]
  12. private string SceneToStart;
  13. [Header("UI Elements")]
  14. [SerializeField]
  15. private TextMeshProUGUI IPText;
  16. [SerializeField]
  17. private Transform Content;
  18. [SerializeField]
  19. private GameObject ClientTag;
  20. [SerializeField]
  21. private ServerObject Server;
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. IPText.text = "Enter '" + Networking.Utility.GetIP(Networking.Utility.ADDRESSFAM.IPv4) + "' into browser";
  26. }
  27. private void OnEnable()
  28. {
  29. Clients.OnClientsChange += DisplayClients;
  30. DisplayClients(Clients.ConnectedClients);
  31. if (Server.server != null)
  32. Server.RegisterHandler(LoginProtocols.StartGame, RecieveStartMsg);
  33. else
  34. Debug.LogWarning("Server not started yet");
  35. }
  36. private void OnDisable()
  37. {
  38. Clients.OnClientsChange -= DisplayClients;
  39. if (Server.server != null)
  40. Server.UnregisterHandler(LoginProtocols.StartGame);
  41. }
  42. private void DisplayClients(List<ClientData> data)
  43. {
  44. //Debug.Log("Updating Connected Clients");
  45. foreach (Transform child in Content)
  46. if (child.gameObject != ClientTag)
  47. Destroy(child.gameObject);
  48. for (int i = 0; i < Clients.ConnectedClients.Count; i++)
  49. {
  50. ClientData client = Clients.ConnectedClients[i];
  51. GameObject clientObject = Instantiate(ClientTag, Content);
  52. TextMeshProUGUI clientText = clientObject.GetComponent<TextMeshProUGUI>();
  53. clientText.text = client.Name + " the " + client.characterAnimal;
  54. clientText.color = client.Color;
  55. Material materialInstance = clientText.fontMaterial;
  56. materialInstance.EnableKeyword(ShaderUtilities.Keyword_Underlay);
  57. materialInstance.SetColor("_UnderlayColor", new Color(0.2264151f, 0.2264151f, 0.2264151f, 1));
  58. materialInstance.SetFloat("_UnderlayOffsetX", 0.5f);
  59. materialInstance.SetFloat("_UnderlayOffsetY", -0.5f);
  60. clientObject.SetActive(true);
  61. }
  62. }
  63. public void OnClick_Back()
  64. {
  65. UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu Server");
  66. }
  67. public void OnClick_StartGame()
  68. {
  69. #if UNITY_EDITOR
  70. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  71. #else
  72. if (Clients.ConnectedClients.Count >= 2 && Clients.ConnectedClients.Count <= 8){
  73. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  74. }
  75. #endif
  76. }
  77. public void RecieveStartMsg(UnityEngine.Networking.NetworkMessage msg)
  78. {
  79. OnClick_StartGame();
  80. }
  81. }