132 lines
4.3 KiB
C#
132 lines
4.3 KiB
C#
using LightReflectiveMirror;
|
|
using Mirror;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class menuController : MonoBehaviour
|
|
{
|
|
public Text usernameTxt;
|
|
public GameObject modSelectionPanel;
|
|
public GameObject serverSelectionPanel;
|
|
[Header("Server List")]
|
|
public Transform scrollParent;
|
|
public GameObject serverRow;
|
|
public float heightPerRow = 30;
|
|
public int curSelected = 0;
|
|
public LightReflectiveMirrorTransport _LRM;
|
|
public Toggle isPrivate;
|
|
public InputField serverNameInput;
|
|
void Start()
|
|
{
|
|
|
|
if (FindObjectOfType<settings>() != null) { FindObjectOfType<settings>().reloadSettings(); }
|
|
string[] args = System.Environment.GetCommandLineArgs();
|
|
for(int i =0; i < args.Length; i++)
|
|
{
|
|
string arg = args[i];
|
|
if(arg.Contains("username"))
|
|
{
|
|
usernameTxt.text = args[i + 1];
|
|
|
|
}
|
|
}
|
|
|
|
_LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
|
|
_LRM.serverListUpdated.AddListener(ServerListUpdate);
|
|
|
|
PlayerPrefs.SetString("username",usernameTxt.text);
|
|
PlayerPrefs.Save();
|
|
serverNameInput.text = usernameTxt.text + "'s Server";
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
if (_LRM != null) { _LRM.serverListUpdated.RemoveListener(ServerListUpdate); }
|
|
}
|
|
|
|
public void refreshServers()
|
|
{
|
|
if(_LRM == null){Debug.Log("WTF!");}
|
|
_LRM.RequestServerList();
|
|
}
|
|
|
|
public void ServerListUpdate()
|
|
{
|
|
//
|
|
//clear all entries
|
|
Debug.Log("it works");
|
|
foreach(Transform t in scrollParent) { Destroy(t.gameObject); }
|
|
bool b1 = false;
|
|
modSelector ModSelector = FindObjectOfType<modSelector>();
|
|
for (int i =0; i < _LRM.relayServerList.Count; i++)
|
|
{
|
|
//int mapId = int.Parse(_LRM.relayServerList[i].serverName.Substring(0, 1));
|
|
if(_LRM.relayServerList[i].serverData.Contains(ModSelector.scenes[ModSelector.currentSelectedId]))
|
|
{
|
|
GameObject go = Instantiate(serverRow, scrollParent);
|
|
go.transform.GetChild(0).GetComponent<Text>().text = _LRM.relayServerList[i].serverName;
|
|
go.transform.GetChild(1).GetComponent<Text>().text = _LRM.relayServerList[i].currentPlayers + "/" + _LRM.relayServerList[i].maxPlayers;
|
|
go.GetComponent<Button>().onClick.AddListener(() => { selectServer(i - 1); });
|
|
if (!b1)
|
|
{
|
|
go.GetComponent<Image>().color = Color.grey;
|
|
b1 = true;
|
|
}
|
|
}
|
|
}
|
|
scrollParent.GetComponent<RectTransform>().sizeDelta = new Vector2(scrollParent.GetComponent<RectTransform>().sizeDelta.x, heightPerRow *(_LRM.relayServerList.Count+1));
|
|
}
|
|
|
|
public void selectServer(int id)
|
|
{
|
|
Debug.Log("Selected server :" + id);
|
|
if(id < scrollParent.childCount-1) { return; }
|
|
curSelected = id;
|
|
foreach(Transform t in scrollParent.GetComponentsInChildren<Transform>())
|
|
{
|
|
if(t.GetComponent<Image>()!=null)t.GetComponent<Image>().color = serverRow.GetComponent<Image>().color;
|
|
}
|
|
scrollParent.GetChild(id).GetComponent<Image>().color = Color.grey;
|
|
}
|
|
|
|
public void joinServer()
|
|
{
|
|
FindObjectOfType<loadingScreen>().serverName = _LRM.relayServerList[curSelected].serverId.ToString();
|
|
FindObjectOfType<loadingScreen>().isHost = false;
|
|
|
|
FindObjectOfType<loadingScreen>().load();
|
|
}
|
|
|
|
public void hostClicked()
|
|
{
|
|
if (serverNameInput.text.Length > 3)
|
|
{
|
|
FindObjectOfType<loadingScreen>().serverName = serverNameInput.text;
|
|
FindObjectOfType<loadingScreen>().isPrivate = !isPrivate.isOn;
|
|
FindObjectOfType<loadingScreen>().isHost = true;
|
|
FindObjectOfType<loadingScreen>().load();
|
|
}
|
|
}
|
|
|
|
|
|
public void selectMod(int modId)
|
|
{
|
|
modSelectionPanel.SetActive(false);
|
|
serverSelectionPanel.SetActive(true);
|
|
}
|
|
|
|
public void gotoModSelection()
|
|
{
|
|
modSelectionPanel.SetActive(true);
|
|
serverSelectionPanel.SetActive(false);
|
|
}
|
|
}
|