MainmenuController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using LightReflectiveMirror;
  2. using Mirror;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class MainmenuController : MonoBehaviour
  8. {
  9. public Text usernameTxt;
  10. [Header("Server List")]
  11. public Transform scrollParent;
  12. public GameObject serverRow;
  13. public float heightPerRow = 60;
  14. public int curSelected = 0;
  15. public LightReflectiveMirrorTransport _LRM;
  16. public Toggle isPublic;
  17. public InputField serverNameInput;
  18. public Slider maxPlayerCount;
  19. void Start()
  20. {
  21. _LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
  22. _LRM.serverListUpdated.AddListener(ServerListUpdate);
  23. _LRM.connectedToRelay.AddListener(refreshServers);
  24. usernameTxt.text = PlayerPrefs.GetString("username");
  25. serverNameInput.text = usernameTxt.text + "'s Server";
  26. // refreshServers();
  27. }
  28. // Update is called once per frame
  29. void Update()
  30. {
  31. }
  32. void OnDisable()
  33. {
  34. if (_LRM != null) { _LRM.serverListUpdated.RemoveListener(ServerListUpdate); }
  35. }
  36. public void refreshServers()
  37. {
  38. StartCoroutine(requestList());
  39. }
  40. IEnumerator requestList(){
  41. yield return new WaitForSeconds(1);
  42. if(_LRM == null){Debug.Log("WTF!");}
  43. _LRM.RequestServerList();
  44. }
  45. public void ServerListUpdate()
  46. {
  47. //
  48. //clear all entries
  49. Debug.Log("it works");
  50. foreach(Transform t in scrollParent) { Destroy(t.gameObject); }
  51. for (int i =0; i < _LRM.relayServerList.Count; i++)
  52. {
  53. //int mapId = int.Parse(_LRM.relayServerList[i].serverName.Substring(0, 1));
  54. GameObject go = Instantiate(serverRow, scrollParent);
  55. go.transform.GetChild(0).GetComponent<Text>().text = _LRM.relayServerList[i].serverName;
  56. go.transform.GetChild(1).GetComponent<Text>().text = _LRM.relayServerList[i].currentPlayers + "/" + _LRM.relayServerList[i].maxPlayers;
  57. go.GetComponentInChildren<Button>().onClick.AddListener(() => { joinServer(_LRM.relayServerList[i-1].serverId.ToString()); });
  58. }
  59. scrollParent.GetComponent<RectTransform>().sizeDelta = new Vector2(scrollParent.GetComponent<RectTransform>().sizeDelta.x, heightPerRow *(_LRM.relayServerList.Count+1));
  60. }
  61. public void selectServer(int id)
  62. {
  63. Debug.Log("Selected server :" + id);
  64. if(id < scrollParent.childCount-1) { return; }
  65. curSelected = id;
  66. foreach(Transform t in scrollParent.GetComponentsInChildren<Transform>())
  67. {
  68. if(t.GetComponent<Image>()!=null)t.GetComponent<Image>().color = serverRow.GetComponent<Image>().color;
  69. }
  70. scrollParent.GetChild(id).GetComponent<Image>().color = Color.grey;
  71. }
  72. public void joinServer()
  73. {
  74. LoadingScreen.instance.serverName = _LRM.relayServerList[curSelected].serverId.ToString();
  75. LoadingScreen.instance.load();
  76. }
  77. public void joinServer(string serverName)
  78. {
  79. LoadingScreen.instance.serverName = serverName;
  80. LoadingScreen.instance.sceneName= "SampleScene";
  81. LoadingScreen.instance.isHost=false;
  82. LoadingScreen.instance.alreadyConnected=false;
  83. LoadingScreen.instance.load();
  84. }
  85. public void hostClicked()
  86. {
  87. if (serverNameInput.text.Length > 3)
  88. {
  89. LoadingScreen.instance.serverName = serverNameInput.text;
  90. LoadingScreen.instance.sceneName= "SampleScene";
  91. LoadingScreen.instance.isPublic = isPublic.isOn;
  92. LoadingScreen.instance.alreadyConnected=false;
  93. LoadingScreen.instance.isHost=true;
  94. ServerMetadata metadata = new ServerMetadata();
  95. metadata.maxPlayerCount = (int)maxPlayerCount.value;
  96. LoadingScreen.instance.serverMetadata = JsonUtility.ToJson(metadata);
  97. LoadingScreen.instance.load();
  98. }
  99. }
  100. }
  101. [System.Serializable]
  102. public class ServerMetadata{
  103. public int maxPlayerCount;
  104. public ServerMetadata(){
  105. maxPlayerCount = 0;
  106. }
  107. }