MultiplayerManager.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Photon.Pun;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. public class MultiplayerManager : MonoBehaviourPunCallbacks
  8. {
  9. // Start is called before the first frame update
  10. private static string _uid = "";
  11. public static MultiplayerManager instance;
  12. public bool isVR = false;
  13. public static string uid { get {
  14. if(_uid == "")
  15. {
  16. char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
  17. while(_uid.Length < 6)
  18. {
  19. _uid += chars[Random.Range(0, chars.Length)];
  20. }
  21. Debug.Log("New uid: " + _uid);
  22. }
  23. return _uid; } }
  24. void Awake()
  25. {
  26. if(instance != null) { Destroy(gameObject); return; }
  27. instance = this;
  28. DontDestroyOnLoad(gameObject);
  29. PhotonNetwork.ConnectUsingSettings();
  30. }
  31. public override void OnConnectedToMaster()
  32. {
  33. base.OnConnectedToMaster();
  34. Debug.Log("Connected to photon");
  35. }
  36. // Update is called once per frame
  37. public void ConnectToAdmin()
  38. {
  39. PhotonNetwork.CreateRoom(uid);
  40. }
  41. public override void OnCreatedRoom()
  42. {
  43. base.OnCreatedRoom();
  44. Debug.Log("New room created : " + uid);
  45. MPChat.isAdmin = false;
  46. PhotonNetwork.LoadLevel(isVR ? "ADMIN_MP_VR" : "ADMIN_MP");
  47. }
  48. }