111
This commit is contained in:
@@ -1,65 +1,100 @@
|
||||
using UnityEngine;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Mirror;
|
||||
using Mirror.Discovery;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Tuan.GameFramework
|
||||
{
|
||||
public class AutoStart : MonoBehaviour
|
||||
{
|
||||
public NetworkManager networkManager;
|
||||
public NetworkDiscovery networkDiscovery;
|
||||
|
||||
public bool alwaysAutoStart = false;
|
||||
public VRNetworkDiscovery networkDiscovery;
|
||||
readonly Dictionary<long, ServerResponse> discoveredServers = new Dictionary<long, ServerResponse>();
|
||||
public float waitForHostTimeout = 1f;
|
||||
|
||||
private bool hostFound = false;
|
||||
private async void Start()
|
||||
{
|
||||
networkDiscovery.OnServerFound.AddListener(OnServerFound);
|
||||
await Connect();
|
||||
// skips waiting for users to press ui button
|
||||
if (alwaysAutoStart)
|
||||
{
|
||||
await Waiter();
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask Connect()
|
||||
public async UniTask Waiter()
|
||||
{
|
||||
if (networkManager == null || networkDiscovery == null)
|
||||
{
|
||||
Debug.LogError("请确认 NetworkManager 和 NetworkDiscovery 已设置");
|
||||
return;
|
||||
}
|
||||
|
||||
hostFound = false;
|
||||
|
||||
discoveredServers.Clear();
|
||||
networkDiscovery.StartDiscovery();
|
||||
|
||||
await UniTask.WaitForSeconds(1.1f);
|
||||
|
||||
if (!hostFound)
|
||||
// we have set this as 3.1 seconds, default discovery scan is 3 seconds, allows some time if host and client are started at same time
|
||||
await UniTask.WaitForSeconds(waitForHostTimeout);
|
||||
if (discoveredServers == null || discoveredServers.Count <= 0)
|
||||
{
|
||||
Debug.Log("未发现 Host,自己成为 Host");
|
||||
networkManager.StartHost();
|
||||
Debug.Log("No Servers found, starting as Host.");
|
||||
await UniTask.WaitForSeconds(0.1f);
|
||||
discoveredServers.Clear();
|
||||
VRNetworkManager.singleton.StartHost();
|
||||
networkDiscovery.AdvertiseServer();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnServerFound(ServerResponse info)
|
||||
void Connect(ServerResponse info)
|
||||
{
|
||||
if (hostFound) return;
|
||||
|
||||
hostFound = true;
|
||||
Debug.Log($"发现 Host: {info.uri.Host}");
|
||||
|
||||
Debug.Log($"Connecting to: {info.uri.Host}");
|
||||
networkDiscovery.StopDiscovery();
|
||||
|
||||
networkManager.networkAddress = info.uri.Host;
|
||||
networkManager.StartClient();
|
||||
VRNetworkManager.singleton.StartClient(info.uri);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
public void OnDiscoveredServer(ServerResponse info)
|
||||
{
|
||||
if (networkDiscovery != null)
|
||||
networkDiscovery.OnServerFound.RemoveListener(OnServerFound);
|
||||
discoveredServers[info.serverId] = info;
|
||||
Connect(info);
|
||||
}
|
||||
|
||||
public void StartAsHost()
|
||||
{
|
||||
Debug.Log("Starting as host");
|
||||
discoveredServers.Clear();
|
||||
VRNetworkManager.singleton.StartHost();
|
||||
networkDiscovery.AdvertiseServer();
|
||||
|
||||
}
|
||||
|
||||
public void StartAsServer()
|
||||
{
|
||||
Debug.Log("Starting as server");
|
||||
discoveredServers.Clear();
|
||||
// VRNetworkManager.singleton.onlineScene = SceneManager.GetActiveScene().name;
|
||||
VRNetworkManager.singleton.StartServer();
|
||||
networkDiscovery.AdvertiseServer();
|
||||
|
||||
}
|
||||
|
||||
public void StartAsClient()
|
||||
{
|
||||
Debug.Log("Starting as client.");
|
||||
discoveredServers.Clear();
|
||||
networkDiscovery.StartDiscovery();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
Debug.Log("Stopping");
|
||||
// stop host if host mode
|
||||
if (NetworkServer.active && NetworkClient.isConnected)
|
||||
{
|
||||
VRNetworkManager.singleton.StopHost();
|
||||
}
|
||||
// stop client if client-only
|
||||
else if (NetworkClient.isConnected)
|
||||
{
|
||||
VRNetworkManager.singleton.StopClient();
|
||||
}
|
||||
// stop server if server-only
|
||||
else if (NetworkServer.active)
|
||||
{
|
||||
VRNetworkManager.singleton.StopServer();
|
||||
}
|
||||
networkDiscovery.StopDiscovery();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user