TouhouLS/Assets/RealCode/Menu/ButtonS/ButtonConnectOnline.cs

84 lines
2.4 KiB
C#
Raw Permalink Normal View History

2020-08-22 05:29:00 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon.LoadBalancing;
public class ButtonConnectOnline : ButtonOnClick {
public static string region;
public override void OnClick() {
if (GameTransition.Instance.state == GameState.Menu) {
GameTransition.Instance.state = GameState.ConnectionInProgress;
GameTransition.Instance.inMultiplayer = true;
StartCoroutine(ConnectToMultiplayerCoroutine());
}
}
public IEnumerator ConnectToMultiplayerCoroutine(){
if (NetworkManager.net.ConnectToNameServer()){
Debug.Log("Connecting to name server");
} else {
Debug.Log("Name Server connection failed");
yield break;
}
while (!NetworkManager.onNameServer || !NetworkManager.isReady) yield return null;
Debug.Log("Connected to name server");
if (NetworkManager.net.OpGetRegions()){
Debug.Log("Started region request");
} else {
Debug.Log("Failed region request");
yield break;
}
while (NetworkManager.net.AvailableRegions == null) yield return null;
Debug.Log("Received region list");
// testing, quick load
if (NetworkManager.instance.quickLoadTestingLobby){
region = "usw";
}
// select best region
else {
region = null;
while(region == null) yield return null;
GameTransition.Instance.state = GameState.ConnectionInProgress;
}
if(NetworkManager.net.ConnectToRegionMaster(region)){
Debug.LogFormat("Connecting to region master '{0}'", region);
} else {
Debug.LogFormat("Failed to connect to region master '{0}'", region);
yield break;
}
while (!NetworkManager.onMasterLobby) yield return null;
Debug.Log("Connected to region master");
Debug.Log("You can quick join now");
// testing, quick load
if (NetworkManager.instance.quickLoadTestingLobby){
var activeScene = "QUICKJOIN";
var ro = new RoomOptions();
ro.IsVisible = false;
ro.IsOpen = true;
ro.MaxPlayers = NetworkManager.instance.expectedMaxPlayers;
NetworkManager.net.OpJoinOrCreateRoomWithProperties(activeScene, ro, null);
// connect to room
while(!NetworkManager.inRoom) yield return null;
// wait for max players to auto start
while(NetworkManager.net.CurrentRoom.PlayerCount != NetworkManager.instance.expectedMaxPlayers) yield return null;
GameBoardInstance.instance.SetupGame();
}
}
}