Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class NetworkManagerScript : MonoBehaviour
- {
- public Transform spawnObject;
- public GameObject playerPrefab;
- private string currentMenu;
- private float bttnW = 150;
- private float bttnH = 40;
- private bool Refreshing = false;
- private HostData[] hostData;
- private string gameName = "Medieval_Swords_Online";
- void Start()
- {
- currentMenu = "Multiplayer";
- }
- void Update()
- {
- if (Refreshing)
- {
- if (MasterServer.PollHostList().Length > 0)
- {
- Debug.Log(MasterServer.PollHostList().Length.ToString());
- Refreshing = false;
- hostData = MasterServer.PollHostList();
- }
- }
- }
- //Own Functions
- void startServer()
- {
- //initialize the server
- Network.InitializeServer(32, 25001, !Network.HavePublicAddress());
- MasterServer.RegisterHost(gameName, "Medieval Swords Online", "This is a beginners game");
- }
- void RefreshHostList()
- {
- MasterServer.RequestHostList(gameName);
- Refreshing = true;
- }
- void spawnPlayer()
- {
- Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity, 0);
- }
- //Unity Server Events
- void OnServerInitialized()
- {
- //event when the server is initialized
- Debug.Log("Server Initialized!");
- spawnPlayer();
- }
- void OnMasterServerEvent(MasterServerEvent mse)
- {
- if(mse == MasterServerEvent.RegistrationSucceeded)
- {
- Debug.Log("Registered Server");
- }
- }
- void OnConnectedToServer()
- {
- spawnPlayer();
- }
- //Gui
- void OnGUI()
- {
- if (currentMenu == "Main")
- Menu_Main();
- if (currentMenu == "Multiplayer")
- Menu_Multiplayer();
- }
- void Menu_Main()
- {
- }
- void Menu_Multiplayer()
- {
- if (!Network.isClient && !Network.isServer)
- {
- if (GUI.Button(new Rect(50, 50, bttnW, bttnH), "Start Server"))
- {
- startServer();
- }
- if (GUI.Button(new Rect(50, 100, bttnW, bttnH), "Refresh"))
- {
- Debug.Log("Refreshing...");
- RefreshHostList();
- }
- GUILayout.BeginArea(new Rect(Screen.width - 500, 0, 500, Screen.height), "Server List");
- GUILayout.Space(20);
- foreach (HostData hostedGame in MasterServer.PollHostList())
- {
- GUILayout.BeginHorizontal("Box");
- GUILayout.Label(hostedGame.gameName);
- if (GUILayout.Button("Connect"))
- {
- Network.Connect(hostedGame);
- }
- GUILayout.EndHorizontal();
- }
- GUILayout.EndArea();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement