ConnectToRegionMaster
How do I use Connect To Region Master
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: NetworkingPeer.cs
Copy
336 public bool ConnectToRegionMaster(CloudRegionCode region)
337 {
338 if (PhotonHandler.AppQuits)
339 {
340 Debug.LogWarning("Ignoring Connect() because app gets closed. If this is an error, check PhotonHandler.AppQuits.");
341 return false;
342 }
343
344 IsUsingNameServer = true;
345 this.CloudRegion = region;
346
347 if (this.State == global::PeerState.ConnectedToNameServer)
348 {
349 return this.OpAuthenticate(this.mAppId, this.mAppVersionPun, this.PlayerName, this.CustomAuthenticationValues, region.ToString());
350 }
351
352 #if RHTTP
353 string address = (this.UsedProtocol == ConnectionProtocol.RHttp) ? this.NameServerAddressHttp : this.NameServerAddress;
354 #else
355 string address = this.NameServerAddress;
356 #endif
357
358 if (!address.Contains(":"))
359 {
360 int port = 0;
361 ProtocolToNameServerPort.TryGetValue(this.UsedProtocol, out port);
362 address = string.Format("{0}:{1}", address, port);
363 //Debug.Log("Server to connect to: "+ address + " settings protocol: " + PhotonNetwork.PhotonServerSettings.Protocol);
364 }
365 if (!base.Connect(address, "ns"))
366 {
367 return false;
368 }
369
370 this.State = global::PeerState.ConnectingToNameServer;
371 return true;
372 }
File name: PhotonHandler.cs
Copy
232 internal IEnumerator PingAvailableRegionsCoroutine(bool connectToBest)
233 {
234 BestRegionCodeCurrently = CloudRegionCode.none;
235 while (PhotonNetwork.networkingPeer.AvailableRegions == null)
236 {
237 if (PhotonNetwork.connectionStateDetailed != PeerState.ConnectingToNameServer && PhotonNetwork.connectionStateDetailed != PeerState.ConnectedToNameServer)
238 {
239 Debug.LogError("Call ConnectToNameServer to ping available regions.");
240 yield break; // break if we don't connect to the nameserver at all
241 }
242
243 Debug.Log("Waiting for AvailableRegions. State: " + PhotonNetwork.connectionStateDetailed + " Server: " + PhotonNetwork.Server + " PhotonNetwork.networkingPeer.AvailableRegions " + (PhotonNetwork.networkingPeer.AvailableRegions != null));
244 yield return new WaitForSeconds(0.25f); // wait until pinging finished (offline mode won't ping)
245 }
246
247 if (PhotonNetwork.networkingPeer.AvailableRegions == null || PhotonNetwork.networkingPeer.AvailableRegions.Count == 0)
248 {
249 Debug.LogError("No regions available. Are you sure your appid is valid and setup?");
250 yield break; // break if we don't get regions at all
251 }
252
253 //#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IPHONE)
254 //#pragma warning disable 0162 // the library variant defines if we should use PUN's SocketUdp variant (at all)
255 //if (PhotonPeer.NoSocket)
256 //{
257 // if (PhotonNetwork.logLevel >= PhotonLogLevel.Informational)
258 // {
259 // Debug.Log("PUN disconnects to re-use native sockets for pining servers and to find the best.");
260 // }
261 // PhotonNetwork.Disconnect();
262 //}
263 //#pragma warning restore 0162
264 //#endif
265
266 PhotonPingManager pingManager = new PhotonPingManager();
267 foreach (Region region in PhotonNetwork.networkingPeer.AvailableRegions)
268 {
269 SP.StartCoroutine(pingManager.PingSocket(region));
270 }
271
272 while (!pingManager.Done)
273 {
274 yield return new WaitForSeconds(0.1f); // wait until pinging finished (offline mode won't ping)
275 }
276
277
278 Region best = pingManager.BestRegion;
279 PhotonHandler.BestRegionCodeCurrently = best.Code;
280 PhotonHandler.BestRegionCodeInPreferences = best.Code;
281
282 Debug.Log("Found best region: " + best.Code + " ping: " + best.Ping + ". Calling ConnectToRegionMaster() is: " + connectToBest);
283
284
285 if (connectToBest)
286 {
287 PhotonNetwork.networkingPeer.ConnectToRegionMaster(best.Code);
288 }
289 }
File name: PhotonNetwork.cs
Copy
1109 /// - Invalid AppId (calls: OnFailedToConnectToPhoton(). check exact AppId value)
1110 /// - Network issues (calls: OnFailedToConnectToPhoton())
1111 /// - Invalid region (calls: OnConnectionFail() with DisconnectCause.InvalidRegion)
1112 /// - Subscription CCU limit reached (calls: OnConnectionFail() with DisconnectCause.MaxCcuReached. also calls: OnPhotonMaxCccuReached())
1118 public static bool ConnectUsingSettings(string gameVersion)
1119 {
1120 if (PhotonServerSettings == null)
1121 {
1122 Debug.LogError("Can't connect: Loading settings failed. ServerSettings asset must be in any 'Resources' folder as: " + serverSettingsAssetFile);
1123 return false;
1124 }
1125
1126 SwitchToProtocol(PhotonServerSettings.Protocol);
1127 networkingPeer.SetApp(PhotonServerSettings.AppID, gameVersion);
1128
1129 if (PhotonServerSettings.HostType == ServerSettings.HostingOption.OfflineMode)
1130 {
1131 offlineMode = true;
1132 return true;
1133 }
1134
1135 if (offlineMode)
1136 {
1137 // someone can set offlineMode in code and then call ConnectUsingSettings() with non-offline settings. Warning for that case:
1138 Debug.LogWarning("ConnectUsingSettings() disabled the offline mode. No longer offline.");
1139 }
1140
1141 offlineMode = false; // Cleanup offline mode
1142 isMessageQueueRunning = true;
1143 networkingPeer.IsInitialConnect = true;
1144
1145 if (PhotonServerSettings.HostType == ServerSettings.HostingOption.SelfHosted)
1146 {
1147 networkingPeer.IsUsingNameServer = false;
1148 networkingPeer.MasterServerAddress = (PhotonServerSettings.ServerPort == 0) ? PhotonServerSettings.ServerAddress : PhotonServerSettings.ServerAddress + ":" + PhotonServerSettings.ServerPort;
1149
1150 return networkingPeer.Connect(networkingPeer.MasterServerAddress, ServerConnection.MasterServer);
1151 }
1152
1153 if (PhotonServerSettings.HostType == ServerSettings.HostingOption.BestRegion)
1154 {
1155 return ConnectToBestCloudServer(gameVersion);
1156 }
1157
1158 return networkingPeer.ConnectToRegionMaster(PhotonServerSettings.PreferredRegion);
1159 }
File name: PhotonNetwork.cs
Copy
1213 /// The ping result can be overridden via PhotonNetwork.OverrideBestCloudServer(..)
1221 /// - Invalid AppId (calls: OnFailedToConnectToPhoton(). check exact AppId value)
1222 /// - Network issues (calls: OnFailedToConnectToPhoton())
1223 /// - Invalid region (calls: OnConnectionFail() with DisconnectCause.InvalidRegion)
1224 /// - Subscription CCU limit reached (calls: OnConnectionFail() with DisconnectCause.MaxCcuReached. also calls: OnPhotonMaxCccuReached())
1231 public static bool ConnectToBestCloudServer(string gameVersion)
1232 {
1233 if (PhotonServerSettings == null)
1234 {
1235 Debug.LogError("Can't connect: Loading settings failed. ServerSettings asset must be in any 'Resources' folder as: " + PhotonNetwork.serverSettingsAssetFile);
1236 return false;
1237 }
1238
1239 if (PhotonServerSettings.HostType == ServerSettings.HostingOption.OfflineMode)
1240 {
1241 return PhotonNetwork.ConnectUsingSettings(gameVersion);
1242 }
1243
1244 networkingPeer.IsInitialConnect = true;
1245 networkingPeer.SetApp(PhotonServerSettings.AppID, gameVersion);
1246
1247 CloudRegionCode bestFromPrefs = PhotonHandler.BestRegionCodeInPreferences;
1248 if (bestFromPrefs != CloudRegionCode.none)
1249 {
1250 Debug.Log("Best region found in PlayerPrefs. Connecting to: " + bestFromPrefs);
1251 return networkingPeer.ConnectToRegionMaster(bestFromPrefs);
1252 }
1253
1254 bool couldConnect = PhotonNetwork.networkingPeer.ConnectToNameServer();
1255 return couldConnect;
1256 }
ConnectToRegionMaster 132 lượt xem
Gõ tìm kiếm nhanh...