menuController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. modSelector ModSelector = FindObjectOfType<modSelector>();
  46. for (int i =0; i < _LRM.relayServerList.Count; i++)
  47. {
  48. //int mapId = int.Parse(_LRM.relayServerList[i].serverName.Substring(0, 1));
  49. if(NetworkManager.singleton.onlineScene == ModSelector.scenes[ModSelector.currentSelectedId])
  50. // if(mapId == FindObjectOfType<modSelector>().currentSelectedId)
  51. {
  52. GameObject go = Instantiate(serverRow, scrollParent);
  53. go.transform.GetChild(0).GetComponent<Text>().text = _LRM.relayServerList[i].serverName;
  54. go.transform.GetChild(1).GetComponent<Text>().text = _LRM.relayServerList[i].currentPlayers + "/" + _LRM.relayServerList[i].maxPlayers;
  55. go.GetComponent<Button>().onClick.AddListener(() => { selectServer(i - 1); });
  56. if (!b1)
  57. {
  58. go.GetComponent<Image>().color = Color.grey;
  59. b1 = true;
  60. }
  61. }
  62. }
  63. scrollParent.GetComponent<RectTransform>().sizeDelta = new Vector2(scrollParent.GetComponent<RectTransform>().sizeDelta.x, heightPerRow *(_LRM.relayServerList.Count+1));
  64. }
  65. public void selectServer(int id)
  66. {
  67. Debug.Log("Selected server :" + id);
  68. if(id < scrollParent.childCount-1) { return; }
  69. curSelected = id;
  70. foreach(Transform t in scrollParent.GetComponentsInChildren<Transform>())
  71. {
  72. if(t.GetComponent<Image>()!=null)t.GetComponent<Image>().color = serverRow.GetComponent<Image>().color;
  73. }
  74. scrollParent.GetChild(id).GetComponent<Image>().color = Color.grey;
  75. }
  76. public void joinServer()
  77. {
  78. NetworkManager.singleton.networkAddress= _LRM.relayServerList[curSelected].serverId.ToString();
  79. NetworkManager.singleton.StartClient();
  80. }
  81. public void hostClicked()
  82. {
  83. if (serverNameInput.text.Length > 3)
  84. {
  85. modSelector ModSelector = FindObjectOfType<modSelector>();
  86. _LRM.serverName = serverNameInput.text;
  87. _LRM.extraServerData = ModSelector.scenes[ModSelector.currentSelectedId];
  88. _LRM.isPublicServer = !isPrivate.isOn;
  89. _LRM.ServerStart();
  90. NetworkManager.singleton.StartHost();
  91. }
  92. }
  93. public void selectMod(int modId)
  94. {
  95. modSelectionPanel.SetActive(false);
  96. serverSelectionPanel.SetActive(true);
  97. }
  98. public void gotoModSelection()
  99. {
  100. modSelectionPanel.SetActive(true);
  101. serverSelectionPanel.SetActive(false);
  102. }
  103. }