Advertisement
chrisall76

Untitled

Nov 1st, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Networking;
  4. using UnityEngine.Networking.Types;
  5. using UnityEngine.Networking.Match;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8.  
  9. public class ENetManager : NetworkLobbyManager {
  10.  
  11.     static public ENetManager Current;
  12.     public GameObject MainCam;
  13.     public Vector3 SpawnLocation;
  14.  
  15.     [HideInInspector]
  16.     public List<GameObject> Players = new List<GameObject>();
  17.     [HideInInspector]
  18.     public GameObject OurLocalPlayer;
  19.  
  20.     void Start() {
  21.         Current = this;
  22.     }
  23.  
  24.     public override void OnLobbyClientSceneChanged(NetworkConnection conn) {
  25.  
  26.         //Only do it for the local player (?)
  27.         if (!conn.playerControllers[0].unetView.isLocalPlayer) {
  28.             return;
  29.         }
  30.  
  31.         //If we're still in the lobby....
  32.         if (Application.loadedLevelName == lobbyScene) {
  33.             Debug.Log("In Lobby!");
  34.         } else {
  35.             //We're not in the lobby
  36.             Debug.Log("In-Game!");
  37.         }
  38.     }
  39.  
  40.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
  41.         Debug.Log("Add player!");
  42.         /*
  43.         //Instantiate the player
  44.         GameObject Player = (GameObject)Instantiate(playerPrefab, SpawnLocation, Quaternion.identity);
  45.         //Instantiate Camera
  46.         GameObject Cam = (GameObject)Instantiate(MainCam, transform.position, Quaternion.identity);
  47.  
  48.         //Set references
  49.         Player.GetComponent<PBattle>()._Cam = Cam.GetComponent<UnityStandardAssets.Cameras.FollowCam>();
  50.         Cam.GetComponent<UnityStandardAssets.Cameras.FollowCam>().SetTarget(Player.transform);
  51.         //Spawn the player
  52.         NetworkServer.AddPlayerForConnection(conn, Player, playerControllerId);
  53.         */
  54.         GameObject playerGO = (GameObject)Instantiate(gamePlayerPrefab, SpawnLocation, Quaternion.identity);
  55.         OurLocalPlayer = playerGO;
  56.         GameObject cameraGO = (GameObject)Instantiate(MainCam, playerGO.transform.position + new Vector3(0, 0, -5), Quaternion.identity);
  57.         NetworkServer.AddPlayerForConnection(conn, playerGO, playerControllerId);
  58.     }
  59.  
  60.     public override void OnMatchCreate(UnityEngine.Networking.Match.CreateMatchResponse matchInfo) {
  61.         base.OnMatchCreate(matchInfo);
  62.     }
  63.  
  64.     //SERVER MANAGEMENT//
  65.     public void StopHosting() {
  66.  
  67.     }
  68.  
  69.     //OTHER//
  70.     public override void OnStartHost() {
  71.         Debug.Log("Started host!");
  72.         base.OnStartHost();
  73.         MainMenu.Current.LobbyB();
  74.  
  75.         //ChangeTo(lobbyPanel);
  76.         //backDelegate = StopHostClbk;
  77.         //SetServerInfo("Hosting", networkAddress);
  78.     }
  79.  
  80.     //When a client connects to the server...
  81.     public override void OnClientConnect(NetworkConnection conn) {
  82.         Debug.Log("Client connected!");
  83.         base.OnClientConnect(conn);
  84.  
  85.         if (!NetworkServer.active) {//only to do on pure client (not self hosting client)
  86.             MainMenu.Current.LobbyB();
  87.         }
  88.     }
  89.  
  90.     public void OnMatchDestroyed(BasicResponse resp) {
  91.         //StopMatchMaker();
  92.         StopHost();
  93.     }
  94.  
  95.     //SERVER CALLBACKS//
  96.     /*
  97.     public override GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId) {
  98.         Debug.Log("HAI");
  99.         GameObject LPlayer = Instantiate(lobbyPlayerPrefab.gameObject) as GameObject;
  100.  
  101.         return LPlayer;
  102.     }*/
  103.  
  104.     public override void OnLobbyServerDisconnect(NetworkConnection conn) {
  105.  
  106.     }
  107.  
  108.     public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer) {
  109.  
  110.         return true;
  111.     }
  112.  
  113.     //CLIENT CALLBACKS//
  114.     public override void OnClientDisconnect(NetworkConnection conn) {
  115.         //Disconnect & go to the main menu...
  116.         base.OnClientDisconnect(conn);
  117.         MainMenu.Current.QuitMatchMaking();
  118.     }
  119.  
  120.     public override void OnClientError(NetworkConnection conn, int errorCode) {
  121.         //Just go back to the main menu...
  122.         MainMenu.Current.QuitMatchMaking();
  123.         Debug.Log("Cient error : " + (errorCode == 6 ? "timeout" : errorCode.ToString()));
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement