menuController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 menuController : MonoBehaviour
  8. {
  9. public GameObject modSelectionPanel;
  10. public GameObject serverSelectionPanel;
  11. [Header("Server List")]
  12. public Transform scrollParent;
  13. public GameObject serverRow;
  14. public float heightPerRow = 30;
  15. public int curSelected = 0;
  16. private LightReflectiveMirrorTransport _LRM;
  17. public Toggle isPrivate;
  18. public InputField serverNameInput;
  19. void Start()
  20. {
  21. if (_LRM == null)
  22. {
  23. _LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
  24. _LRM.serverListUpdated.AddListener(ServerListUpdate);
  25. }
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. }
  31. void OnDisable()
  32. {
  33. if (_LRM != null) { _LRM.serverListUpdated.RemoveListener(ServerListUpdate); }
  34. }
  35. public void refreshServers()
  36. {
  37. _LRM.RequestServerList();
  38. }
  39. public void ServerListUpdate()
  40. {
  41. //
  42. //clear all entries
  43. foreach(Transform t in scrollParent) { Destroy(t.gameObject); }
  44. bool b1 = false;
  45. for(int i =0; i < _LRM.relayServerList.Count; i++)
  46. {
  47. int mapId = int.Parse(_LRM.relayServerList[i].serverName.Substring(0, 1));
  48. if(mapId == FindObjectOfType<modSelector>().currentSelectedId)
  49. {
  50. GameObject go = Instantiate(serverRow, scrollParent);
  51. go.transform.GetChild(0).GetComponent<Text>().text = _LRM.relayServerList[i].serverName;
  52. go.transform.GetChild(1).GetComponent<Text>().text = _LRM.relayServerList[i].currentPlayers + "/" + _LRM.relayServerList[i].maxPlayers;
  53. go.GetComponent<Button>().onClick.AddListener(() => { selectServer(i - 1); });
  54. if (!b1)
  55. {
  56. go.GetComponent<Image>().color = Color.grey;
  57. b1 = true;
  58. }
  59. }
  60. }
  61. scrollParent.GetComponent<RectTransform>().sizeDelta = new Vector2(scrollParent.GetComponent<RectTransform>().sizeDelta.x, heightPerRow *(_LRM.relayServerList.Count+1));
  62. }
  63. public void selectServer(int id)
  64. {
  65. Debug.Log("Selected server :" + id);
  66. if(id < scrollParent.childCount-1) { return; }
  67. curSelected = id;
  68. foreach(Transform t in scrollParent.GetComponentsInChildren<Transform>())
  69. {
  70. if(t.GetComponent<Image>()!=null)t.GetComponent<Image>().color = serverRow.GetComponent<Image>().color;
  71. }
  72. scrollParent.GetChild(id).GetComponent<Image>().color = Color.grey;
  73. }
  74. public void joinServer()
  75. {
  76. NetworkManager.singleton.networkAddress= _LRM.relayServerList[curSelected].serverId.ToString();
  77. NetworkManager.singleton.StartClient();
  78. }
  79. public void hostClicked()
  80. {
  81. if (serverNameInput.text.Length > 3)
  82. {
  83. _LRM.serverName = FindObjectOfType<modSelector>().currentSelectedId+serverNameInput.text;
  84. _LRM.isPublicServer = !isPrivate.isOn;
  85. _LRM.ServerStart();
  86. NetworkManager.singleton.StartHost();
  87. }
  88. }
  89. public void selectMod(int modId)
  90. {
  91. modSelectionPanel.SetActive(false);
  92. serverSelectionPanel.SetActive(true);
  93. }
  94. public void gotoModSelection()
  95. {
  96. modSelectionPanel.SetActive(true);
  97. serverSelectionPanel.SetActive(false);
  98. }
  99. }