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.

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