StatesGui.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="TabViewManager.cs" company="Exit Games GmbH">
  3. // </copyright>
  4. // <summary>
  5. // Output detailed information about Pun Current states, using the old Unity UI framework.
  6. // </summary>
  7. // <author>developer@exitgames.com</author>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using UnityEngine;
  10. using Photon.Realtime;
  11. namespace Photon.Pun.UtilityScripts
  12. {
  13. /// <summary>
  14. /// Output detailed information about Pun Current states, using the old Unity UI framework.
  15. /// </summary>
  16. public class StatesGui : MonoBehaviour
  17. {
  18. public Rect GuiOffset = new Rect(250, 0, 300, 300);
  19. public bool DontDestroy = true;
  20. public bool ServerTimestamp;
  21. public bool DetailedConnection;
  22. public bool Server;
  23. public bool AppVersion;
  24. public bool UserId;
  25. public bool Room;
  26. public bool RoomProps;
  27. public bool EventsIn;
  28. public bool LocalPlayer;
  29. public bool PlayerProps;
  30. public bool Others;
  31. public bool Buttons;
  32. public bool ExpectedUsers;
  33. private Rect GuiRect = new Rect();
  34. private static StatesGui Instance;
  35. void Awake()
  36. {
  37. if (Instance != null)
  38. {
  39. DestroyImmediate(this.gameObject);
  40. return;
  41. }
  42. if (DontDestroy)
  43. {
  44. Instance = this;
  45. DontDestroyOnLoad(this.gameObject);
  46. }
  47. if (EventsIn)
  48. {
  49. PhotonNetwork.NetworkingClient.LoadBalancingPeer.TrafficStatsEnabled = true;
  50. }
  51. }
  52. void OnDisable()
  53. {
  54. if (DontDestroy && Instance == this)
  55. {
  56. Instance = null;
  57. }
  58. }
  59. float native_width = 800;
  60. float native_height = 480;
  61. void OnGUI()
  62. {
  63. if (PhotonNetwork.NetworkingClient == null || PhotonNetwork.NetworkingClient.LoadBalancingPeer == null || PhotonNetwork.NetworkingClient.LoadBalancingPeer.TrafficStatsIncoming == null)
  64. {
  65. return;
  66. }
  67. //set up scaling
  68. float rx = Screen.width / native_width;
  69. float ry = Screen.height / native_height;
  70. GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));
  71. Rect GuiOffsetRuntime = new Rect(this.GuiOffset);
  72. if (GuiOffsetRuntime.x < 0)
  73. {
  74. GuiOffsetRuntime.x = Screen.width - GuiOffsetRuntime.width;
  75. }
  76. GuiRect.xMin = GuiOffsetRuntime.x;
  77. GuiRect.yMin = GuiOffsetRuntime.y;
  78. GuiRect.xMax = GuiOffsetRuntime.x + GuiOffsetRuntime.width;
  79. GuiRect.yMax = GuiOffsetRuntime.y + GuiOffsetRuntime.height;
  80. GUILayout.BeginArea(GuiRect);
  81. GUILayout.BeginHorizontal();
  82. if (this.ServerTimestamp)
  83. {
  84. GUILayout.Label((((double)PhotonNetwork.ServerTimestamp) / 1000d).ToString("F3"));
  85. }
  86. if (Server)
  87. {
  88. GUILayout.Label(PhotonNetwork.ServerAddress + " " + PhotonNetwork.Server);
  89. }
  90. if (DetailedConnection)
  91. {
  92. GUILayout.Label(PhotonNetwork.NetworkClientState.ToString());
  93. }
  94. if (AppVersion)
  95. {
  96. GUILayout.Label(PhotonNetwork.NetworkingClient.AppVersion);
  97. }
  98. GUILayout.EndHorizontal();
  99. GUILayout.BeginHorizontal();
  100. if (UserId)
  101. {
  102. GUILayout.Label("UID: " + ((PhotonNetwork.AuthValues != null) ? PhotonNetwork.AuthValues.UserId : "no UserId"));
  103. GUILayout.Label("UserId:" + PhotonNetwork.LocalPlayer.UserId);
  104. }
  105. GUILayout.EndHorizontal();
  106. if (Room)
  107. {
  108. if (PhotonNetwork.InRoom)
  109. {
  110. GUILayout.Label(this.RoomProps ? PhotonNetwork.CurrentRoom.ToStringFull() : PhotonNetwork.CurrentRoom.ToString());
  111. }
  112. else
  113. {
  114. GUILayout.Label("not in room");
  115. }
  116. }
  117. if (EventsIn)
  118. {
  119. int fragments = PhotonNetwork.NetworkingClient.LoadBalancingPeer.TrafficStatsIncoming.FragmentCommandCount;
  120. GUILayout.Label("Events Received: "+PhotonNetwork.NetworkingClient.LoadBalancingPeer.TrafficStatsGameLevel.EventCount + " Fragments: "+fragments);
  121. }
  122. if (this.LocalPlayer)
  123. {
  124. GUILayout.Label(PlayerToString(PhotonNetwork.LocalPlayer));
  125. }
  126. if (Others)
  127. {
  128. foreach (Player player in PhotonNetwork.PlayerListOthers)
  129. {
  130. GUILayout.Label(PlayerToString(player));
  131. }
  132. }
  133. if (ExpectedUsers)
  134. {
  135. if (PhotonNetwork.InRoom)
  136. {
  137. int countExpected = (PhotonNetwork.CurrentRoom.ExpectedUsers != null) ? PhotonNetwork.CurrentRoom.ExpectedUsers.Length : 0;
  138. GUILayout.Label("Expected: " + countExpected + " " +
  139. ((PhotonNetwork.CurrentRoom.ExpectedUsers != null) ? string.Join(",", PhotonNetwork.CurrentRoom.ExpectedUsers) : "")
  140. );
  141. }
  142. }
  143. if (Buttons)
  144. {
  145. if (!PhotonNetwork.IsConnected && GUILayout.Button("Connect"))
  146. {
  147. PhotonNetwork.ConnectUsingSettings();
  148. }
  149. GUILayout.BeginHorizontal();
  150. if (PhotonNetwork.IsConnected && GUILayout.Button("Disconnect"))
  151. {
  152. PhotonNetwork.Disconnect();
  153. }
  154. if (PhotonNetwork.IsConnected && GUILayout.Button("Close Socket"))
  155. {
  156. PhotonNetwork.NetworkingClient.LoadBalancingPeer.StopThread();
  157. }
  158. GUILayout.EndHorizontal();
  159. if (PhotonNetwork.IsConnected && PhotonNetwork.InRoom && GUILayout.Button("Leave"))
  160. {
  161. PhotonNetwork.LeaveRoom();
  162. }
  163. if (PhotonNetwork.IsConnected && PhotonNetwork.InRoom && PhotonNetwork.CurrentRoom.PlayerTtl>0 && GUILayout.Button("Leave(abandon)"))
  164. {
  165. PhotonNetwork.LeaveRoom(false);
  166. }
  167. if (PhotonNetwork.IsConnected && !PhotonNetwork.InRoom && GUILayout.Button("Join Random"))
  168. {
  169. PhotonNetwork.JoinRandomRoom();
  170. }
  171. if (PhotonNetwork.IsConnected && !PhotonNetwork.InRoom && GUILayout.Button("Create Room"))
  172. {
  173. PhotonNetwork.CreateRoom(null);
  174. }
  175. }
  176. GUILayout.EndArea();
  177. }
  178. private string PlayerToString(Player player)
  179. {
  180. if (PhotonNetwork.NetworkingClient == null)
  181. {
  182. Debug.LogError("nwp is null");
  183. return "";
  184. }
  185. return string.Format("#{0:00} '{1}'{5} {4}{2} {3} {6}", player.ActorNumber + "/userId:<" + player.UserId + ">", player.NickName, player.IsMasterClient ? "(master)" : "", this.PlayerProps ? player.CustomProperties.ToStringFull() : "", (PhotonNetwork.LocalPlayer.ActorNumber == player.ActorNumber) ? "(you)" : "", player.UserId, player.IsInactive ? " / Is Inactive" : "");
  186. }
  187. }
  188. }