浏览代码
Cleaning up with some comments and shifting the RelayNGOUtpSetup to its own file.
/main/staging/ngo_minigame_cleanup
Cleaning up with some comments and shifting the RelayNGOUtpSetup to its own file.
/main/staging/ngo_minigame_cleanup
nathaniel.buck@unity3d.com
3 年前
当前提交
1041eaf9
共有 6 个文件被更改,包括 124 次插入 和 123 次删除
-
2Assets/Scripts/Game/InGame/InGameRunner.cs
-
41Assets/Scripts/Game/InGame/SetupInGame.cs
-
15Assets/Scripts/Game/InGame/SymbolContainer.cs
-
90Assets/Scripts/Relay/RelayUtpSetup.cs
-
88Assets/Scripts/Game/InGame/RelayNGOUtpSetup.cs
-
11Assets/Scripts/Game/InGame/RelayNGOUtpSetup.cs.meta
|
|||
using System; |
|||
using Unity.Services.Relay.Models; |
|||
using UnityEngine; |
|||
using LobbyRelaySample.relay; |
|||
|
|||
namespace LobbyRelaySample.inGame |
|||
{ |
|||
/* |
|||
* When using the Relay adapter for UTP to connect the NetworkManager for Netcode for GameObjects (NGO), we need to provide the Allocation info without manually binding 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 // If this is a MonoBehaviour, it can be added to the InGameRunner object for easier cleanup on game end.
|
|||
{ |
|||
private inGame.SetupInGame m_setupInGame; |
|||
private LocalLobby m_localLobby; |
|||
private Action m_onJoin; |
|||
|
|||
public void Initialize(inGame.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 inGame.SetupInGame m_setupInGame; |
|||
private LocalLobby m_localLobby; |
|||
private Action m_onJoin; |
|||
|
|||
public void Initialize(inGame.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(); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 90f27286cd4428d4c8487405d4141891 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue