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.

72 lines
1.6 KiB

5 years ago
  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 ConnectionHandler 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. foreach (ClientData client in Clients.ConnectedClients)
  41. {
  42. GameObject clientObject = Instantiate(ClientTag, Content);
  43. TextMeshProUGUI clientText = clientObject.GetComponent<TextMeshProUGUI>();
  44. clientText.text = client.Name;
  45. clientText.color = client.Color;
  46. clientObject.SetActive(true);
  47. }
  48. }
  49. public void OnClick_StartGame()
  50. {
  51. UnityEngine.SceneManagement.SceneManager.LoadScene(SceneToStart);
  52. }
  53. }