Advertisement
Guest User

ConnectionManager.cs

a guest
Jun 19th, 2015
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine.Networking.Match;
  6. using UnityEngine.Networking;
  7. using UnityEngine;
  8.  
  9. public class ConnectionManager : NetworkManager
  10. {
  11.     public GameObject managerPrefab;
  12.  
  13.     public GameObject activeManager;
  14.  
  15.     NetworkConnection clientConnection;
  16.     bool joinedRoom = false;
  17.     bool isHost = false;
  18.     bool isClient = false;
  19.     bool isServer = false;
  20.  
  21.     public void Update()
  22.     {
  23.  
  24.     }
  25.  
  26.     public override void OnStopServer()
  27.     {
  28.         GameClosed();
  29.     }
  30.  
  31.     public override void OnStopClient()
  32.     {
  33.         GameClosed();
  34.     }
  35.  
  36.     public override void OnStartHost()
  37.     {
  38.         isHost = true;
  39.     }
  40.  
  41.     public override void OnStartServer()
  42.     {
  43.         isServer = true;
  44.         joinedRoom = true;
  45.         activeManager = (GameObject)Instantiate(FindPrefab("Manager"), transform.position, transform.rotation);
  46.         NetworkServer.Spawn(activeManager);
  47.     }
  48.  
  49.     public override void OnStartClient(NetworkClient client)
  50.     {
  51.         isClient = true;
  52.         joinedRoom = true;
  53.         clientConnection = client.connection;
  54.     }
  55.  
  56.     private void CloseGame()
  57.     {
  58.         if (isHost)
  59.             StopHost();
  60.         else if (isClient)
  61.             StopClient();
  62.         else
  63.             StopServer();
  64.     }
  65.  
  66.     private void GameClosed()
  67.     {
  68.         if (activeManager != null)
  69.         {
  70.             activeManager.BroadcastMessage("StopGame");
  71.             NetworkServer.Destroy(activeManager);
  72.         }
  73.         joinedRoom = false;
  74.         isServer = false;
  75.         isClient = false;
  76.         isHost = false;
  77.     }
  78.  
  79.     public GameObject FindPrefab(string prefab)
  80.     {
  81.         return spawnPrefabs.First(p => p.name == prefab);
  82.     }
  83.  
  84.     void OnGUI()
  85.     {
  86.         if (!joinedRoom)
  87.         {
  88.             if (GUILayout.Button("Start Local Server"))
  89.                 StartServer();
  90.             if (GUILayout.Button("Start Local Host"))
  91.                 StartHost();
  92.             if (GUILayout.Button("Start Client"))
  93.                 StartClient();
  94.         }
  95.         else if (!ClientScene.ready)
  96.         {
  97.             //ClientScene.Ready(clientConnection);
  98.         }
  99.         else
  100.         {
  101.             GUILayout.Label("Ready!");
  102.         }
  103.         if (joinedRoom)
  104.             if (GUILayout.Button("Close"))
  105.                 CloseGame();
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement