Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine.Networking.Match;
- using UnityEngine.Networking;
- using UnityEngine;
- public class ConnectionManager : NetworkManager
- {
- public GameObject managerPrefab;
- public GameObject activeManager;
- NetworkConnection clientConnection;
- bool joinedRoom = false;
- bool isHost = false;
- bool isClient = false;
- bool isServer = false;
- public void Update()
- {
- }
- public override void OnStopServer()
- {
- GameClosed();
- }
- public override void OnStopClient()
- {
- GameClosed();
- }
- public override void OnStartHost()
- {
- isHost = true;
- }
- public override void OnStartServer()
- {
- isServer = true;
- joinedRoom = true;
- activeManager = (GameObject)Instantiate(FindPrefab("Manager"), transform.position, transform.rotation);
- NetworkServer.Spawn(activeManager);
- }
- public override void OnStartClient(NetworkClient client)
- {
- isClient = true;
- joinedRoom = true;
- clientConnection = client.connection;
- }
- private void CloseGame()
- {
- if (isHost)
- StopHost();
- else if (isClient)
- StopClient();
- else
- StopServer();
- }
- private void GameClosed()
- {
- if (activeManager != null)
- {
- activeManager.BroadcastMessage("StopGame");
- NetworkServer.Destroy(activeManager);
- }
- joinedRoom = false;
- isServer = false;
- isClient = false;
- isHost = false;
- }
- public GameObject FindPrefab(string prefab)
- {
- return spawnPrefabs.First(p => p.name == prefab);
- }
- void OnGUI()
- {
- if (!joinedRoom)
- {
- if (GUILayout.Button("Start Local Server"))
- StartServer();
- if (GUILayout.Button("Start Local Host"))
- StartHost();
- if (GUILayout.Button("Start Client"))
- StartClient();
- }
- else if (!ClientScene.ready)
- {
- //ClientScene.Ready(clientConnection);
- }
- else
- {
- GUILayout.Label("Ready!");
- }
- if (joinedRoom)
- if (GUILayout.Button("Close"))
- CloseGame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement