Advertisement
Unbalance

NetworkManager.cs

Jan 2nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3.  
  4. public class NetworkManager : MonoBehaviour
  5. {
  6.  
  7.     [SerializeField] PhotonLogLevel logLevel = PhotonLogLevel.Informational;
  8.     [SerializeField] string gameVersion = "0.0.0";
  9.     [SerializeField] Camera sceneCamera;
  10.     [SerializeField] GameObject playerPrefab;
  11.     [SerializeField] GameObject cameraPrefab;
  12.     GUIStyle boxStyle = null;
  13.  
  14.     void Start()
  15.     {
  16.         Connect();
  17.     }
  18.  
  19.     void Connect()
  20.     {
  21.         PhotonNetwork.ConnectUsingSettings(gameVersion);
  22.         PhotonNetwork.logLevel = logLevel;
  23.     }
  24.  
  25.     void OnGUI()
  26.     {
  27.         var list = PhotonNetwork.GetRoomList();
  28.         if (!PhotonNetwork.inRoom &&  PhotonNetwork.connected)
  29.         {
  30.             GUI.BeginGroup(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 50, 400, 100));
  31.             if (list.Length > 0)
  32.             {
  33.                     PhotonNetwork.JoinRoom("TestRoom");
  34.             }
  35.             else
  36.             {
  37.                 if (GUI.Button(new Rect(0, 0, 400, 100), "Create Room"))
  38.                 {
  39.                     PhotonNetwork.CreateRoom("TestRoom");
  40.                 }
  41.             }
  42.             GUI.EndGroup();
  43.  
  44.         }
  45.  
  46.         if (PhotonNetwork.connected)
  47.             return;
  48.        
  49.         boxStyle = new GUIStyle(GUI.skin.box) { alignment = TextAnchor.MiddleCenter };
  50.         string connectionState = PhotonNetwork.connectionStateDetailed.ToString();
  51.         GUI.BeginGroup(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 50, 400, 100));
  52.         GUI.Box(new Rect(0, 0, 400, 100), connectionState, boxStyle);
  53.         GUI.EndGroup();
  54.     }
  55.  
  56.     void OnJoinedLobby()
  57.     {
  58.     // UI ?
  59.     }
  60.  
  61.     void OnPhotonRandomJoinFailed()
  62.     {
  63.         PhotonNetwork.CreateRoom(null);
  64.     }
  65.  
  66.     void OnJoinedRoom()
  67.     {
  68.         CreatePlayer();
  69.     }
  70.  
  71.     void CreatePlayer()
  72.     {
  73.  
  74.         float rnd = Random.Range(-5, 5);
  75.         GameObject player_gbj = PhotonNetwork.Instantiate(playerPrefab.name, new Vector3(rnd, .25f, rnd), Quaternion.identity, 0);
  76.         GameObject camera_gbj = Instantiate(cameraPrefab, new Vector3(0, 0, 0), Quaternion.identity);
  77.         SetUpComponents(player_gbj, camera_gbj);
  78.  
  79.     }
  80.  
  81.     void SetUpComponents(GameObject player_gbj, GameObject camera_gbj)
  82.     {
  83.         var gunBehaviour = player_gbj.GetComponent<GunBehaviour>();
  84.         gunBehaviour.MainCamera = camera_gbj.GetComponentInChildren<Camera>();
  85.  
  86.         var zoom = camera_gbj.GetComponent<CameraZoom>();
  87.         gunBehaviour.Zoom = zoom;
  88.  
  89.         var animation = player_gbj.GetComponentInChildren<CharacterAnimator>();
  90.         animation.Zoom = zoom;
  91.  
  92.         var camMovement = camera_gbj.GetComponent<BasicCameraBehaviour>();
  93.         camMovement.Target = player_gbj.transform;
  94.  
  95.         camera_gbj.SetActive(true);
  96.         sceneCamera.gameObject.SetActive(false);
  97.     }
  98.     private void OnConnectedToMaster()
  99.     {
  100.         PhotonNetwork.JoinLobby();
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement