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.

78 lines
2.0 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. // Start is called before the first frame update
  21. void Start()
  22. {
  23. IPText.text = "Lobby - " + Networking.Utility.GetIP(Networking.Utility.ADDRESSFAM.IPv4);
  24. }
  25. private void OnEnable()
  26. {
  27. Clients.OnClientsChange += DisplayClients;
  28. DisplayClients(Clients.ConnectedClients);
  29. }
  30. private void OnDisable()
  31. {
  32. Clients.OnClientsChange -= DisplayClients;
  33. }
  34. private void DisplayClients(List<ClientData> data)
  35. {
  36. //Debug.Log("Updating Connected Clients");
  37. foreach (Transform child in Content)
  38. if (child.gameObject != ClientTag)
  39. Destroy(child.gameObject);
  40. for (int i = 0; i < Clients.ConnectedClients.Count; i++)
  41. {
  42. ClientData client = Clients.ConnectedClients[i];
  43. GameObject clientObject = Instantiate(ClientTag, Content);
  44. TextMeshProUGUI clientText = clientObject.GetComponent<TextMeshProUGUI>();
  45. clientText.text = client.Name + " the " + client.characterAnimal;
  46. clientText.color = client.Color;
  47. clientObject.SetActive(true);
  48. }
  49. }
  50. public void OnClick_Back()
  51. {
  52. UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu Server");
  53. }
  54. public void OnClick_StartGame()
  55. {
  56. #if UNITY_EDITOR
  57. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  58. #else
  59. if (Clients.ConnectedClients.Count >= 2 && Clients.ConnectedClients.Count <= 4){
  60. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  61. }
  62. #endif
  63. }
  64. }