Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class NetworkManager : MonoBehaviour {
- string registerGameName = "POD_Server_MADPEN_T001";
- float refreshRequestLength = 3.0f;
- HostData[] hostData;
- private void StartServer()
- {
- Network.InitializeServer (16,25002,false);
- MasterServer.RegisterHost(registerGameName,"Souls for Zombies","Do Not Enter");
- }
- void OnServerInitialized()
- {
- Debug.Log ("Server Started");
- }
- void OnMasterServerEvent(MasterServerEvent masterServerEvent)
- {
- if(masterServerEvent == MasterServerEvent.RegistrationSucceeded)
- {
- Debug.Log ("Server Registered");
- }
- }
- public IEnumerator RefreshHostList()
- {
- Debug.Log ("Refreshing List");
- MasterServer.RequestHostList (registerGameName);
- float timeStarted = Time.time;
- float timeStopped = Time.time + refreshRequestLength;
- while (Time.time < timeStopped)
- {
- hostData = MasterServer.PollHostList();
- yield return new WaitForEndOfFrame();
- }
- if( hostData == null || hostData.Length == 0)
- {
- Debug.Log ("no servers");
- }
- else
- {
- Debug.Log ("found server");
- }
- }
- public void OnGUI()
- {
- if (Network.isClient || Network.isServer)
- return;
- if(GUI.Button(new Rect(25f,25f,150f,30f),"Start New Server"))
- {
- StartServer ();
- }
- if(GUI.Button(new Rect(25f,65f,150f,30f),"Refresh Server List"))
- {
- StartCoroutine ("RefreshHostList");
- }
- if (hostData != null)
- {
- for(int i = 0;i < hostData.Length ; i++)
- {
- if(GUI.Button (new Rect(Screen.width/2,65f + (30f * i),300f,30f), hostData[i].gameName))
- {
- Network.Connect(hostData[i]);
- Debug.Log("I am a client Now");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement