ConnectAndJoinRandom.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ConnectAndJoinRandom.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Utilities,
  4. // </copyright>
  5. // <summary>
  6. // Simple component to call ConnectUsingSettings and to get into a PUN room easily.
  7. // </summary>
  8. // <remarks>
  9. // A custom inspector provides a button to connect in PlayMode, should AutoConnect be false.
  10. // </remarks>
  11. // <author>developer@exitgames.com</author>
  12. // --------------------------------------------------------------------------------------------------------------------
  13. //#if UNITY_EDITOR
  14. //using UnityEditor;
  15. //#endif
  16. using UnityEngine;
  17. //using Photon.Pun;
  18. using Photon.Realtime;
  19. namespace Photon.Pun.UtilityScripts
  20. {
  21. /// <summary>Simple component to call ConnectUsingSettings and to get into a PUN room easily.</summary>
  22. /// <remarks>A custom inspector provides a button to connect in PlayMode, should AutoConnect be false.</remarks>
  23. public class ConnectAndJoinRandom : MonoBehaviourPunCallbacks
  24. {
  25. /// <summary>Connect automatically? If false you can set this to true later on or call ConnectUsingSettings in your own scripts.</summary>
  26. public bool AutoConnect = true;
  27. /// <summary>Used as PhotonNetwork.GameVersion.</summary>
  28. public byte Version = 1;
  29. /// <summary>Max number of players allowed in room. Once full, a new room will be created by the next connection attemping to join.</summary>
  30. [Tooltip("The max number of players allowed in room. Once full, a new room will be created by the next connection attemping to join.")]
  31. public byte MaxPlayers = 4;
  32. public int playerTTL = -1;
  33. public void Start()
  34. {
  35. if (this.AutoConnect)
  36. {
  37. this.ConnectNow();
  38. }
  39. }
  40. public void ConnectNow()
  41. {
  42. Debug.Log("ConnectAndJoinRandom.ConnectNow() will now call: PhotonNetwork.ConnectUsingSettings().");
  43. PhotonNetwork.ConnectUsingSettings();
  44. PhotonNetwork.GameVersion = this.Version + "." + SceneManagerHelper.ActiveSceneBuildIndex;
  45. }
  46. // below, we implement some callbacks of the Photon Realtime API.
  47. // Being a MonoBehaviourPunCallbacks means, we can override the few methods which are needed here.
  48. public override void OnConnectedToMaster()
  49. {
  50. Debug.Log("OnConnectedToMaster() was called by PUN. This client is now connected to Master Server in region [" + PhotonNetwork.CloudRegion +
  51. "] and can join a room. Calling: PhotonNetwork.JoinRandomRoom();");
  52. PhotonNetwork.JoinRandomRoom();
  53. }
  54. public override void OnJoinedLobby()
  55. {
  56. Debug.Log("OnJoinedLobby(). This client is now connected to Relay in region [" + PhotonNetwork.CloudRegion + "]. This script now calls: PhotonNetwork.JoinRandomRoom();");
  57. PhotonNetwork.JoinRandomRoom();
  58. }
  59. public override void OnJoinRandomFailed(short returnCode, string message)
  60. {
  61. Debug.Log("OnJoinRandomFailed() was called by PUN. No random room available in region [" + PhotonNetwork.CloudRegion + "], so we create one. Calling: PhotonNetwork.CreateRoom(null, new RoomOptions() {maxPlayers = 4}, null);");
  62. RoomOptions roomOptions = new RoomOptions() { MaxPlayers = this.MaxPlayers };
  63. if (playerTTL >= 0)
  64. roomOptions.PlayerTtl = playerTTL;
  65. PhotonNetwork.CreateRoom(null, roomOptions, null);
  66. }
  67. // the following methods are implemented to give you some context. re-implement them as needed.
  68. public override void OnDisconnected(DisconnectCause cause)
  69. {
  70. Debug.Log("OnDisconnected(" + cause + ")");
  71. }
  72. public override void OnJoinedRoom()
  73. {
  74. Debug.Log("OnJoinedRoom() called by PUN. Now this client is in a room in region [" + PhotonNetwork.CloudRegion + "]. Game is now running.");
  75. }
  76. }
  77. //#if UNITY_EDITOR
  78. //[CanEditMultipleObjects]
  79. //[CustomEditor(typeof(ConnectAndJoinRandom), true)]
  80. //public class ConnectAndJoinRandomInspector : Editor
  81. //{
  82. // void OnEnable() { EditorApplication.update += Update; }
  83. // void OnDisable() { EditorApplication.update -= Update; }
  84. // bool isConnectedCache = false;
  85. // void Update()
  86. // {
  87. // if (this.isConnectedCache != PhotonNetwork.IsConnected)
  88. // {
  89. // this.Repaint();
  90. // }
  91. // }
  92. // public override void OnInspectorGUI()
  93. // {
  94. // this.isConnectedCache = !PhotonNetwork.IsConnected;
  95. // this.DrawDefaultInspector(); // Draw the normal inspector
  96. // if (Application.isPlaying && !PhotonNetwork.IsConnected)
  97. // {
  98. // if (GUILayout.Button("Connect"))
  99. // {
  100. // ((ConnectAndJoinRandom)this.target).ConnectNow();
  101. // }
  102. // }
  103. // }
  104. //}
  105. //#endif
  106. }