浏览代码

SetupIngame removed most callbacks

Removed RelayNGOUtpSetup
/main/staging/2021_Upgrade/Async_Refactor
当前提交
33b59ac0
共有 3 个文件被更改,包括 43 次插入114 次删除
  1. 58
      Assets/Scripts/GameLobby/NGO/SetupInGame.cs
  2. 88
      Assets/Scripts/GameLobby/NGO/RelayNGOUtpSetup.cs
  3. 11
      Assets/Scripts/GameLobby/NGO/RelayNGOUtpSetup.cs.meta

58
Assets/Scripts/GameLobby/NGO/SetupInGame.cs


using System;
using System.Threading.Tasks;
using LobbyRelaySample.relay;
using Unity.Services.Relay;
using UnityEngine;
namespace LobbyRelaySample.ngo

private bool m_doesNeedCleanup = false;
private bool m_hasConnectedViaNGO = false;
private Action<UnityTransport> m_initializeTransport;
private LocalLobby m_lobby;
private LobbyUser m_localUser;

/// </summary>
private async Task CreateNetworkManager()
{
UnityTransport transport = NetworkManager.Singleton.GetComponentInChildren<UnityTransport>();
m_inGameRunner = Instantiate(m_IngameRunnerPrefab).GetComponentInChildren<InGameRunner>();
m_inGameRunner.Initialize(OnConnectionVerified, m_lobby.PlayerCount, OnGameEnd, m_localUser);
NetworkManager.Singleton.gameObject.AddComponent<RelayUtpNGOSetupHost>().Initialize(this, m_lobby, () => { m_initializeTransport(transport); NetworkManager.Singleton.StartHost(); });
{
await SetRelayHostData();
NetworkManager.Singleton.StartHost();
}
NetworkManager.Singleton.gameObject.AddComponent<RelayUtpNGOSetupClient>().Initialize(this, m_lobby, () => { m_initializeTransport(transport); NetworkManager.Singleton.StartClient(); });
await Task.Delay(1);
m_inGameRunner = Instantiate(m_IngameRunnerPrefab).GetComponentInChildren<InGameRunner>();
m_inGameRunner.Initialize(OnConnectionVerified, m_lobby.PlayerCount, OnGameEnd, m_localUser);
{
await SetRelayClientData();
NetworkManager.Singleton.StartClient();
}
}
async Task SetRelayHostData()
{
UnityTransport transport = NetworkManager.Singleton.GetComponentInChildren<UnityTransport>();
var allocation = await Relay.Instance.CreateAllocationAsync(m_lobby.MaxPlayerCount);
var joincode = await Relay.Instance.GetJoinCodeAsync(allocation.AllocationId);
m_lobby.RelayNGOCode = joincode;
bool isSecure = false;
var endpoint = RelayUtpSetup.GetEndpointForAllocation(allocation.ServerEndpoints,
allocation.RelayServer.IpV4, allocation.RelayServer.Port, out isSecure);
transport.SetHostRelayData(RelayUtpSetup.AddressFromEndpoint(endpoint), endpoint.Port,
allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData, isSecure);
}
async Task SetRelayClientData()
{
UnityTransport transport = NetworkManager.Singleton.GetComponentInChildren<UnityTransport>();
var joinAllocation = await Relay.Instance.JoinAllocationAsync(m_lobby.RelayCode);
bool isSecure = false;
var endpoint = RelayUtpSetup.GetEndpointForAllocation(joinAllocation.ServerEndpoints,
joinAllocation.RelayServer.IpV4, joinAllocation.RelayServer.Port, out isSecure);
transport.SetClientRelayData(RelayUtpSetup.AddressFromEndpoint(endpoint), endpoint.Port,
joinAllocation.AllocationIdBytes, joinAllocation.Key,
joinAllocation.ConnectionData, joinAllocation.HostConnectionData, isSecure);
}
private void OnConnectionVerified()

{ m_localUser = user; // Same, regarding redundancy.
}
/// <summary>
/// Once the Relay Allocation is created, this passes its data to the UnityTransport.
/// </summary>
public void SetRelayServerData(string address, int port, byte[] allocationBytes, byte[] key, byte[] connectionData, byte[] hostConnectionData, bool isSecure)
{
m_initializeTransport = (transport) => { transport.SetRelayServerData(address, (ushort)port, allocationBytes, key, connectionData, hostConnectionData, isSecure); };
}
public void OnReceiveMessage(MessageType type, object msg)
{

88
Assets/Scripts/GameLobby/NGO/RelayNGOUtpSetup.cs


using System;
using Unity.Services.Relay.Models;
using UnityEngine;
using LobbyRelaySample.relay;
namespace LobbyRelaySample.ngo
{
/*
* To use Netcode for GameObjects (NGO), we use the Relay adapter for UTP, attached to a NetworkManager. This needs to be provided the Allocation info before we bind to it.
* In actual use, if you are using NGO for your game's networking, you would not also use the RelayUtpSetupHost/RelayUtpSetupClient at all, since their direct data transmission would be unnecessary.
* We keep both versions for this sample to demonstrate how each is set up, whether you want to just use Lobby + Relay or use NGO as well.
*/
/// <summary>
/// Host logic: Request a new Allocation, and then pass its info to the UTP adapter for NGO.
/// </summary>
public class RelayUtpNGOSetupHost : MonoBehaviour // This is a MonoBehaviour so that it can be added to the InGameRunner object for easier cleanup on game end.
{
private SetupInGame m_setupInGame;
private LocalLobby m_localLobby;
private Action m_onJoin;
public void Initialize(SetupInGame setupInGame, LocalLobby lobby, Action onJoin)
{
m_setupInGame = setupInGame;
m_localLobby = lobby;
m_onJoin = onJoin;
RelayAPIInterface.AllocateAsync(m_localLobby.MaxPlayerCount, OnAllocation);
}
private void OnAllocation(Allocation allocation)
{
RelayAPIInterface.GetJoinCodeAsync(allocation.AllocationId, OnRelayCode);
bool isSecure = false;
var endpoint = RelayUtpSetup.GetEndpointForAllocation(allocation.ServerEndpoints, allocation.RelayServer.IpV4, allocation.RelayServer.Port, out isSecure);
m_setupInGame.SetRelayServerData(RelayUtpSetup.AddressFromEndpoint(endpoint), endpoint.Port, allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData, allocation.ConnectionData, isSecure);
m_onJoin?.Invoke();
}
private void OnRelayCode(string relayCode)
{
m_localLobby.RelayNGOCode = relayCode;
}
}
/// <summary>
/// Client logic: Wait to receive the Relay code for NGO, and then pass the allocation info to the UTP adapter.
/// </summary>
public class RelayUtpNGOSetupClient : MonoBehaviour // This is also a MonoBehaviour for access to OnDestroy, to ensure unsubscription from the local lobby on game end.
{
private SetupInGame m_setupInGame;
private LocalLobby m_localLobby;
private Action m_onJoin;
public void Initialize(SetupInGame setupInGame, LocalLobby lobby, Action onJoin)
{
m_setupInGame = setupInGame;
m_localLobby = lobby;
m_onJoin = onJoin;
m_localLobby.onChanged += OnLobbyChange;
}
public void OnDestroy()
{
m_localLobby.onChanged -= OnLobbyChange;
}
private void OnLobbyChange(LocalLobby lobby)
{
if (m_localLobby.RelayNGOCode != null)
{
RelayAPIInterface.JoinAsync(m_localLobby.RelayNGOCode, OnJoin);
m_localLobby.onChanged -= OnLobbyChange;
}
}
private void OnJoin(JoinAllocation joinAllocation)
{
if (joinAllocation == null || this == null) // The returned JoinAllocation is null if allocation failed. This would be destroyed already if you quit the lobby while Relay is connecting.
return;
bool isSecure = false;
var endpoint = RelayUtpSetup.GetEndpointForAllocation(joinAllocation.ServerEndpoints, joinAllocation.RelayServer.IpV4, joinAllocation.RelayServer.Port, out isSecure);
m_setupInGame.SetRelayServerData(RelayUtpSetup.AddressFromEndpoint(endpoint), endpoint.Port, joinAllocation.AllocationIdBytes, joinAllocation.Key, joinAllocation.ConnectionData, joinAllocation.HostConnectionData, isSecure);
m_onJoin?.Invoke();
}
}
}

11
Assets/Scripts/GameLobby/NGO/RelayNGOUtpSetup.cs.meta


fileFormatVersion: 2
guid: 90f27286cd4428d4c8487405d4141891
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存