menuController.cs 4.1 KB

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