Basic Networking done
This commit is contained in:
113
Assets/Scripts/MainmenuController.cs
Normal file
113
Assets/Scripts/MainmenuController.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using LightReflectiveMirror;
|
||||
using Mirror;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class MainmenuController : MonoBehaviour
|
||||
{
|
||||
public Text usernameTxt;
|
||||
[Header("Server List")]
|
||||
public Transform scrollParent;
|
||||
public GameObject serverRow;
|
||||
public float heightPerRow = 60;
|
||||
public int curSelected = 0;
|
||||
public LightReflectiveMirrorTransport _LRM;
|
||||
public Toggle isPublic;
|
||||
public InputField serverNameInput;
|
||||
void Start()
|
||||
{
|
||||
|
||||
_LRM = (LightReflectiveMirrorTransport)Transport.activeTransport;
|
||||
_LRM.serverListUpdated.AddListener(ServerListUpdate);
|
||||
_LRM.connectedToRelay.AddListener(refreshServers);
|
||||
// serverNameInput.text = usernameTxt.text + "'s Server";
|
||||
// refreshServers();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (_LRM != null) { _LRM.serverListUpdated.RemoveListener(ServerListUpdate); }
|
||||
}
|
||||
|
||||
public void refreshServers()
|
||||
{
|
||||
StartCoroutine(requestList());
|
||||
}
|
||||
|
||||
IEnumerator requestList(){
|
||||
yield return new WaitForSeconds(1);
|
||||
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;
|
||||
for (int i =0; i < _LRM.relayServerList.Count; i++)
|
||||
{
|
||||
//int mapId = int.Parse(_LRM.relayServerList[i].serverName.Substring(0, 1));
|
||||
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.GetComponentInChildren<Button>().onClick.AddListener(() => { joinServer(_LRM.relayServerList[i-1].serverId.ToString()); });
|
||||
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>().load();
|
||||
}
|
||||
public void joinServer(string serverName)
|
||||
{
|
||||
FindObjectOfType<loadingScreen>().serverName = serverName;
|
||||
FindObjectOfType<loadingScreen>().sceneName= "SampleScene";
|
||||
FindObjectOfType<loadingScreen>().isHost=false;
|
||||
|
||||
FindObjectOfType<loadingScreen>().load();
|
||||
}
|
||||
|
||||
public void hostClicked()
|
||||
{
|
||||
if (serverNameInput.text.Length > 3)
|
||||
{
|
||||
FindObjectOfType<loadingScreen>().serverName = serverNameInput.text;
|
||||
FindObjectOfType<loadingScreen>().sceneName= "SampleScene";
|
||||
|
||||
FindObjectOfType<loadingScreen>().isPublic = isPublic.isOn;
|
||||
FindObjectOfType<loadingScreen>().load();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user