Guest User

networkManager

a guest
Sep 14th, 2014
13
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class networkManager : MonoBehaviour {
  5.  
  6.     public static bool connected {get; private set;}
  7.     public static GameObject localPlayer;
  8.     public static bool spawnedAlready = false;
  9.    
  10.     public string connectionIP  = "127.0.0.1";
  11.     public int portNumber       = 8632;
  12.     public int seedNumber       = 1;
  13.     public static int seed = 1;
  14.    
  15.     void Awake()
  16.     {
  17.         if(spawnedAlready == false){
  18.             spawnedAlready = true;
  19.             DontDestroyOnLoad(gameObject);
  20.         }
  21.         else{
  22.             DestroyImmediate(gameObject);
  23.         }
  24.     }
  25.    
  26.     void Start(){
  27.         Network.SetLevelPrefix(0);
  28.     }
  29.    
  30.     void Update(){
  31.         if (Input.GetKeyDown(KeyCode.Escape) && connected){
  32.             Network.RemoveRPCs(localPlayer.networkView.viewID);
  33.             Network.Disconnect();
  34.             Application.Quit();
  35.         }  
  36.     }
  37.  
  38.    
  39.     private void OnGUI(){
  40.         if(!connected){
  41.             connectionIP = GUILayout.TextField(connectionIP);
  42.             int.TryParse(GUILayout.TextField(portNumber.ToString()), out portNumber);
  43.             int.TryParse(GUILayout.TextField(seedNumber.ToString()), out seedNumber);
  44.            
  45.             if(GUILayout.Button("Connect")){
  46.                 Network.Connect(connectionIP,portNumber);
  47.             }
  48.            
  49.             if (GUILayout.Button("Host")){
  50.                 Network.InitializeServer(8,portNumber,true);
  51.             }
  52.         }
  53.         else{
  54.             int i = 0;
  55.             while (i < Network.connections.Length) {
  56.                 GUILayout.Label("Player " + Network.connections[i] + " - " + Network.GetLastPing(Network.connections[i]) + " ms");
  57.                 i++;
  58.             }
  59.         }
  60.     }
  61.    
  62.     IEnumerator LoadMeLevel(){
  63.         Network.SetSendingEnabled(0, false);
  64.         Network.isMessageQueueRunning = false;
  65.         Network.SetLevelPrefix(1);
  66.         Application.LoadLevel(1);
  67.         yield return new WaitForSeconds(0.1F);
  68.         Network.isMessageQueueRunning = true;
  69.         Network.SetSendingEnabled(0, true);
  70.        
  71.         foreach(GameObject go in FindObjectsOfType(typeof(GameObject))){
  72.             go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
  73.         }
  74.     }
  75.    
  76.     //Messages
  77.     void OnServerInitialized(){
  78.         connected = true;
  79.         Random.seed = Random.Range(1,999999);
  80.         seed = Random.seed;
  81.         seedNumber = seed;
  82.         print("Server initialized with: "+Random.seed.ToString());
  83.         StartCoroutine("LoadMeLevel");
  84.     }
  85.    
  86.     void OnConnectedToServer(){
  87.         print("Connected to server");
  88.     }
  89.    
  90.     void OnPlayerConnected(NetworkPlayer player){
  91.         print ("Playe connected. Sending seed: "+seed.ToString());
  92.         networkView.RPC("GetSeed",RPCMode.OthersBuffered,new object[]{seed});
  93.     }
  94.    
  95.     [RPC]
  96.     void GetSeed(int theSeed){
  97.         print("Received seed: "+theSeed.ToString());
  98.         seed = theSeed;
  99.         connected = true;
  100.         StartCoroutine("LoadMeLevel");
  101.     }
  102.    
  103.     void OnPlayerDisconnected(NetworkPlayer networkPlayer){
  104.         Network.RemoveRPCs(networkPlayer);
  105.         Network.DestroyPlayerObjects(networkPlayer);
  106.     }
  107. }
RAW Paste Data