Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- public class NetworkManager : MonoBehaviour
- {
- [SerializeField] PhotonLogLevel logLevel = PhotonLogLevel.Informational;
- [SerializeField] string gameVersion = "0.0.0";
- [SerializeField] Camera sceneCamera;
- [SerializeField] GameObject playerPrefab;
- [SerializeField] GameObject cameraPrefab;
- GUIStyle boxStyle = null;
- void Start()
- {
- Connect();
- }
- void Connect()
- {
- PhotonNetwork.ConnectUsingSettings(gameVersion);
- PhotonNetwork.logLevel = logLevel;
- }
- void OnGUI()
- {
- var list = PhotonNetwork.GetRoomList();
- if (!PhotonNetwork.inRoom && PhotonNetwork.connected)
- {
- GUI.BeginGroup(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 50, 400, 100));
- if (list.Length > 0)
- {
- PhotonNetwork.JoinRoom("TestRoom");
- }
- else
- {
- if (GUI.Button(new Rect(0, 0, 400, 100), "Create Room"))
- {
- PhotonNetwork.CreateRoom("TestRoom");
- }
- }
- GUI.EndGroup();
- }
- if (PhotonNetwork.connected)
- return;
- boxStyle = new GUIStyle(GUI.skin.box) { alignment = TextAnchor.MiddleCenter };
- string connectionState = PhotonNetwork.connectionStateDetailed.ToString();
- GUI.BeginGroup(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 50, 400, 100));
- GUI.Box(new Rect(0, 0, 400, 100), connectionState, boxStyle);
- GUI.EndGroup();
- }
- void OnJoinedLobby()
- {
- // UI ?
- }
- void OnPhotonRandomJoinFailed()
- {
- PhotonNetwork.CreateRoom(null);
- }
- void OnJoinedRoom()
- {
- CreatePlayer();
- }
- void CreatePlayer()
- {
- float rnd = Random.Range(-5, 5);
- GameObject player_gbj = PhotonNetwork.Instantiate(playerPrefab.name, new Vector3(rnd, .25f, rnd), Quaternion.identity, 0);
- GameObject camera_gbj = Instantiate(cameraPrefab, new Vector3(0, 0, 0), Quaternion.identity);
- SetUpComponents(player_gbj, camera_gbj);
- }
- void SetUpComponents(GameObject player_gbj, GameObject camera_gbj)
- {
- var gunBehaviour = player_gbj.GetComponent<GunBehaviour>();
- gunBehaviour.MainCamera = camera_gbj.GetComponentInChildren<Camera>();
- var zoom = camera_gbj.GetComponent<CameraZoom>();
- gunBehaviour.Zoom = zoom;
- var animation = player_gbj.GetComponentInChildren<CharacterAnimator>();
- animation.Zoom = zoom;
- var camMovement = camera_gbj.GetComponent<BasicCameraBehaviour>();
- camMovement.Target = player_gbj.transform;
- camera_gbj.SetActive(true);
- sceneCamera.gameObject.SetActive(false);
- }
- private void OnConnectedToMaster()
- {
- PhotonNetwork.JoinLobby();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement