Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. using Photon.Pun;
  2. using Photon.Realtime;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. public class PhotonRoom : MonoBehaviourPunCallbacks, IInRoomCallbacks
  9. {
  10. public static PhotonRoom room;
  11. private PhotonView PV;
  12. public int multiplayerScene;
  13. private int currentScene;
  14. private void Awake()
  15. {
  16. if(PhotonRoom.room == null)
  17. {
  18. PhotonRoom.room = this;
  19. }
  20. else
  21. {
  22. if(PhotonRoom.room != this)
  23. {
  24. Destroy(PhotonRoom.room.gameObject);
  25. PhotonRoom.room = this;
  26. }
  27. }
  28. DontDestroyOnLoad(this.gameObject);
  29. PV = GetComponent<PhotonView>();
  30. }
  31. public override void OnEnable()
  32. {
  33. base.OnEnable();
  34. PhotonNetwork.AddCallbackTarget(this);
  35. SceneManager.sceneLoaded += OnSceneFinisedLoading;
  36. }
  37. public override void OnDisable()
  38. {
  39. base.OnDisable();
  40. PhotonNetwork.RemoveCallbackTarget(this);
  41. SceneManager.sceneLoaded -= OnSceneFinisedLoading;
  42. }
  43. public override void OnJoinedRoom()
  44. {
  45. base.OnJoinedRoom();
  46. Debug.Log("Has joined room");
  47. if (!PhotonNetwork.IsMasterClient)
  48. return;
  49. StartGame();
  50. }
  51. void StartGame()
  52. {
  53. Debug.Log("Loading Level");
  54. PhotonNetwork.LoadLevel(multiplayerScene);
  55. }
  56. void OnSceneFinisedLoading(Scene scene, LoadSceneMode mode)
  57. {
  58. currentScene = scene.buildIndex;
  59. if(currentScene == multiplayerScene)
  60. {
  61. CreatePlayer();
  62. }
  63. }
  64. private void CreatePlayer()
  65. {
  66. PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), transform.position, Quaternion.identity, 0);
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement