ConnectAndJoinRandomLb.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="ConnectAndJoinRandomLb.cs" company="Exit Games GmbH"/>
  3. // <summary>Prototyping / sample code for Photon Realtime.</summary>
  4. // <author>developer@exitgames.com</author>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. using System.Collections.Generic;
  7. using ExitGames.Client.Photon;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace Photon.Realtime.Demo
  11. {
  12. public class ConnectAndJoinRandomLb : MonoBehaviour, IConnectionCallbacks, IMatchmakingCallbacks, ILobbyCallbacks
  13. {
  14. [SerializeField]
  15. private AppSettings appSettings = new AppSettings();
  16. private LoadBalancingClient lbc;
  17. private ConnectionHandler ch;
  18. public Text StateUiText;
  19. public void Start()
  20. {
  21. this.lbc = new LoadBalancingClient();
  22. this.lbc.AddCallbackTarget(this);
  23. if (!this.lbc.ConnectUsingSettings(appSettings))
  24. {
  25. Debug.LogError("Error while connecting");
  26. }
  27. this.ch = this.gameObject.GetComponent<ConnectionHandler>();
  28. if (this.ch != null)
  29. {
  30. this.ch.Client = this.lbc;
  31. this.ch.StartFallbackSendAckThread();
  32. }
  33. }
  34. public void Update()
  35. {
  36. LoadBalancingClient client = this.lbc;
  37. if (client != null)
  38. {
  39. client.Service();
  40. Text uiText = this.StateUiText;
  41. string state = client.State.ToString();
  42. if (uiText != null && !uiText.text.Equals(state))
  43. {
  44. uiText.text = "State: " + state;
  45. }
  46. }
  47. }
  48. public void OnConnected()
  49. {
  50. }
  51. public void OnConnectedToMaster()
  52. {
  53. Debug.Log("OnConnectedToMaster");
  54. this.lbc.OpJoinRandomRoom(); // joins any open room (no filter)
  55. }
  56. public void OnDisconnected(DisconnectCause cause)
  57. {
  58. Debug.Log("OnDisconnected(" + cause + ")");
  59. }
  60. public void OnCustomAuthenticationResponse(Dictionary<string, object> data)
  61. {
  62. }
  63. public void OnCustomAuthenticationFailed(string debugMessage)
  64. {
  65. }
  66. public void OnRegionListReceived(RegionHandler regionHandler)
  67. {
  68. Debug.Log("OnRegionListReceived");
  69. regionHandler.PingMinimumOfRegions(this.OnRegionPingCompleted, null);
  70. }
  71. public void OnRoomListUpdate(List<RoomInfo> roomList)
  72. {
  73. }
  74. public void OnLobbyStatisticsUpdate(List<TypedLobbyInfo> lobbyStatistics)
  75. {
  76. }
  77. public void OnJoinedLobby()
  78. {
  79. }
  80. public void OnLeftLobby()
  81. {
  82. }
  83. public void OnFriendListUpdate(List<FriendInfo> friendList)
  84. {
  85. }
  86. public void OnCreatedRoom()
  87. {
  88. }
  89. public void OnCreateRoomFailed(short returnCode, string message)
  90. {
  91. }
  92. public void OnJoinedRoom()
  93. {
  94. Debug.Log("OnJoinedRoom");
  95. }
  96. public void OnJoinRoomFailed(short returnCode, string message)
  97. {
  98. }
  99. public void OnJoinRandomFailed(short returnCode, string message)
  100. {
  101. Debug.Log("OnJoinRandomFailed");
  102. this.lbc.OpCreateRoom(new EnterRoomParams());
  103. }
  104. public void OnLeftRoom()
  105. {
  106. }
  107. /// <summary>A callback of the RegionHandler, provided in OnRegionListReceived.</summary>
  108. /// <param name="regionHandler">The regionHandler wraps up best region and other region relevant info.</param>
  109. private void OnRegionPingCompleted(RegionHandler regionHandler)
  110. {
  111. Debug.Log("OnRegionPingCompleted " + regionHandler.BestRegion);
  112. Debug.Log("RegionPingSummary: " + regionHandler.SummaryToCache);
  113. this.lbc.ConnectToRegionMaster(regionHandler.BestRegion.Code);
  114. }
  115. }
  116. }