Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Photon.Pun;
- using Photon.Realtime;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class PhotonRoom : MonoBehaviourPunCallbacks, IInRoomCallbacks
- {
- public static PhotonRoom room;
- private PhotonView PV;
- public int multiplayerScene;
- private int currentScene;
- private void Awake()
- {
- if(PhotonRoom.room == null)
- {
- PhotonRoom.room = this;
- }
- else
- {
- if(PhotonRoom.room != this)
- {
- Destroy(PhotonRoom.room.gameObject);
- PhotonRoom.room = this;
- }
- }
- DontDestroyOnLoad(this.gameObject);
- PV = GetComponent<PhotonView>();
- }
- public override void OnEnable()
- {
- base.OnEnable();
- PhotonNetwork.AddCallbackTarget(this);
- SceneManager.sceneLoaded += OnSceneFinisedLoading;
- }
- public override void OnDisable()
- {
- base.OnDisable();
- PhotonNetwork.RemoveCallbackTarget(this);
- SceneManager.sceneLoaded -= OnSceneFinisedLoading;
- }
- public override void OnJoinedRoom()
- {
- base.OnJoinedRoom();
- Debug.Log("Has joined room");
- if (!PhotonNetwork.IsMasterClient)
- return;
- StartGame();
- }
- void StartGame()
- {
- Debug.Log("Loading Level");
- PhotonNetwork.LoadLevel(multiplayerScene);
- }
- void OnSceneFinisedLoading(Scene scene, LoadSceneMode mode)
- {
- currentScene = scene.buildIndex;
- if(currentScene == multiplayerScene)
- {
- CreatePlayer();
- }
- }
- private void CreatePlayer()
- {
- PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), transform.position, Quaternion.identity, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement