Advertisement
diegographics

Untitled

Mar 12th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class NetworkManager : MonoBehaviour {
  5.     private const string typeName = "Genesis";
  6.     private const string gameName = "DiegosLounge";
  7.     private HostData[] hostList;
  8.     public GameObject playerPrefab;
  9.     private void RefreshHostList()
  10.     {
  11.         MasterServer.RequestHostList(typeName);
  12.     }
  13.  
  14.     void OnMasterServerEvent(MasterServerEvent msEvent)
  15.     {
  16.         if (msEvent == MasterServerEvent.HostListReceived)
  17.             hostList = MasterServer.PollHostList();
  18.     }
  19.     private void JoinServer(HostData hostData)
  20.     {
  21.         Network.Connect(hostData);
  22.     }
  23.  
  24.     void OnConnectedToServer()
  25.     {
  26.         Debug.Log("Server Joined");
  27.         SpawnPlayer();
  28.     }
  29.     private void StartServer()
  30.     {
  31.         Debug.Log("Try to Start Server");
  32.         Network.InitializeServer(4, 25000, !Network.HavePublicAddress());
  33.         MasterServer.RegisterHost(typeName, gameName);
  34.     }
  35.     void OnServerInitialized()
  36.     {
  37.         Debug.Log("Server Initializied");
  38.         SpawnPlayer();
  39.     }
  40.     // Use this for initialization
  41.     void Start () {
  42.         Debug.Log("NetworkManager.cs is running...");
  43.         PhotonNetwork.logLevel = PhotonLogLevel.Full;
  44.     }
  45.     private void SpawnPlayer()
  46.     {
  47.         Network.Instantiate(playerPrefab, new Vector3(0f, 5f, 0f), Quaternion.identity, 0);e ip
  48.     }
  49.     void OnGUI()
  50.     {
  51.         if (!Network.isClient && !Network.isServer)
  52.         {
  53.             if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
  54.                 StartServer();
  55.  
  56.             if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
  57.                 RefreshHostList();
  58.  
  59.             if (hostList != null)
  60.             {
  61.                 for (int i = 0; i < hostList.Length; i++)
  62.                 {
  63.                     if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
  64.                         JoinServer(hostList[i]);
  65.                 }
  66.             }
  67.         }
  68.     }
  69.    
  70.     // Update is called once per frame
  71.     void Update () {
  72.    
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement