menuController.cs 4.3 KB

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