当前提交
3daae290
共有 15 个文件被更改,包括 589 次插入 和 164 次删除
-
14Assets/Resources/TestNetworkManager.prefab
-
2Assets/Scripts/GameLobby/NGO/SetupInGame.cs
-
2Packages/manifest.json
-
26Packages/packages-lock.json
-
2ProjectSettings/EditorBuildSettings.asset
-
5Assets/Scripts/GameLobby/Tests/LobbyRelay HostClient TestScene.unity
-
285Assets/Scripts/GameLobby/Tests/LobbyAndRelayTester.cs
-
25Assets/Scripts/GameLobby/Tests/NetworkRotator.cs
-
11Assets/Scripts/GameLobby/Tests/NetworkRotator.cs.meta
-
220Assets/Scripts/GameLobby/Tests/TestNetworkHeartBeatObject.prefab
-
7Assets/Scripts/GameLobby/Tests/TestNetworkHeartBeatObject.prefab.meta
-
154Assets/TestMono.cs
-
0/Assets/Scripts/GameLobby/Tests/LobbyRelay HostClient TestScene.unity
-
0/Assets/Scripts/GameLobby/Tests/LobbyRelay HostClient TestScene.unity.meta
-
0/Assets/Scripts/GameLobby/Tests/LobbyAndRelayTester.cs.meta
|
|||
using System; |
|||
using Unity.Services.Authentication; |
|||
using System.Collections.Generic; |
|||
using System.Diagnostics; |
|||
using System.Text; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Unity.Netcode; |
|||
using Unity.Netcode.Transports.UTP; |
|||
using Unity.Services.Core; |
|||
using Unity.Services.Lobbies; |
|||
using Unity.Services.Lobbies.Models; |
|||
using Unity.Services.Relay; |
|||
using UnityEngine; |
|||
using Debug = UnityEngine.Debug; |
|||
|
|||
public class LobbyAndRelayTester : MonoBehaviour |
|||
{ |
|||
[SerializeField] |
|||
string m_JoinCode; |
|||
|
|||
[SerializeField] |
|||
string m_RelayCode; |
|||
|
|||
[SerializeField] |
|||
bool m_Host; |
|||
|
|||
Guid m_RelayAllocationId; |
|||
bool m_InLobby; |
|||
string m_WorkingLobbyId; |
|||
|
|||
string m_PlayerId; |
|||
Timer m_Time; |
|||
Lobby m_CurrentLobby; |
|||
|
|||
// This is handled in the LobbyAsyncRequest calls normally, but we need to supply this for the dir
|
|||
Dictionary<string, PlayerDataObject> m_MockUserData; |
|||
UnityTransport m_UnityTransport; |
|||
NetworkManager m_NetworkManager; |
|||
CancellationTokenSource m_CancellationTokenSource; |
|||
Stopwatch m_timer; |
|||
|
|||
// Start is called before the first frame update
|
|||
public async void Start() |
|||
{ |
|||
var profileName = m_Host ? "host" : "client"; |
|||
var initOptions = new InitializationOptions().SetProfile(profileName); |
|||
await UnityServices.InitializeAsync(initOptions); |
|||
await AuthenticationService.Instance.SignInAnonymouslyAsync(); |
|||
m_NetworkManager = NetworkManager.Singleton; |
|||
m_timer = new Stopwatch(); |
|||
m_timer.Start(); |
|||
m_UnityTransport = m_NetworkManager.GetComponent<UnityTransport>(); |
|||
m_PlayerId = AuthenticationService.Instance.PlayerId; |
|||
m_CancellationTokenSource = new CancellationTokenSource(); |
|||
var cancellationToken = m_CancellationTokenSource.Token; |
|||
Debug.Log($"Starting as: {profileName} - {m_PlayerId}"); |
|||
if (m_Host) |
|||
{ |
|||
var hostLobby = await Host_Relay_Lobby(); |
|||
if (hostLobby == null) |
|||
return; |
|||
m_JoinCode = hostLobby.LobbyCode; |
|||
#pragma warning disable 4014
|
|||
KeepLobbyAlive(hostLobby, cancellationToken); |
|||
#pragma warning restore 4014
|
|||
} |
|||
else |
|||
{ |
|||
await Join_Disconnect_Test(); |
|||
} |
|||
|
|||
await PollLobby(cancellationToken); |
|||
} |
|||
|
|||
async Task KeepLobbyAlive(Lobby lobby, CancellationToken cancel) |
|||
{ |
|||
m_InLobby = true; |
|||
var kickedPlayer = false; |
|||
while (m_InLobby && !cancel.IsCancellationRequested) |
|||
{ |
|||
await Task.Delay(9000); |
|||
await LobbyService.Instance.SendHeartbeatPingAsync(lobby.Id); |
|||
|
|||
Debug.Log("HeartBeat."); |
|||
} |
|||
} |
|||
|
|||
async Task PollLobby(CancellationToken cancel) |
|||
{ |
|||
m_InLobby = true; |
|||
while (m_InLobby && !cancel.IsCancellationRequested) |
|||
{ |
|||
await Task.Delay(3000); |
|||
m_CurrentLobby = await LobbyService.Instance.GetLobbyAsync(m_CurrentLobby.Id); |
|||
Debug.Log( |
|||
$"Polled Lobby @ {m_timer.Elapsed.Minutes} : {m_timer.Elapsed.Seconds}.\nID:{m_CurrentLobby.Id} HostID:{m_CurrentLobby.HostId} " + |
|||
$"- Code: {m_CurrentLobby.LobbyCode} - Players: {m_CurrentLobby.Players.Count} - Private: {m_CurrentLobby.IsPrivate}"); |
|||
} |
|||
} |
|||
|
|||
async Task<Lobby> Host_Relay_Lobby() |
|||
{ |
|||
var allocation = await Relay.Instance.CreateAllocationAsync(3, null); |
|||
m_RelayAllocationId = allocation.AllocationId; |
|||
Debug.Log($"RelayAllocation : {m_RelayAllocationId} " + |
|||
$"- {allocation.RelayServer.IpV4} : {allocation.RelayServer.Port}"); |
|||
|
|||
StringBuilder sb = new StringBuilder("Endpoints: \n"); |
|||
foreach (var endpoint in allocation.ServerEndpoints) |
|||
{ |
|||
sb.AppendLine($"Host: {endpoint.Host} - ConnectionType: {endpoint.ConnectionType} " + |
|||
$"- Port: {endpoint.Port} - Secure?:{endpoint.Secure}"); |
|||
} |
|||
|
|||
Debug.Log(sb); |
|||
|
|||
m_UnityTransport.SetHostRelayData(allocation.RelayServer.IpV4, (ushort)allocation.RelayServer.Port, |
|||
allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData); |
|||
|
|||
// Start Host
|
|||
m_NetworkManager.StartHost(); |
|||
|
|||
// Generate Join_Relay_Lobby Code
|
|||
m_RelayCode = await Relay.Instance.GetJoinCodeAsync(m_RelayAllocationId); |
|||
|
|||
// Create Lobby
|
|||
var options = new CreateLobbyOptions |
|||
{ |
|||
Data = new Dictionary<string, DataObject> |
|||
{ |
|||
{ "relayJoinCode", new DataObject(DataObject.VisibilityOptions.Public, m_RelayCode) }, |
|||
}, |
|||
Player = new Player(m_PlayerId, allocationId: m_RelayAllocationId.ToString()) |
|||
}; |
|||
|
|||
m_CurrentLobby = await LobbyService.Instance.CreateLobbyAsync("testLobby", 3, options); |
|||
return m_CurrentLobby; |
|||
} |
|||
|
|||
async Task<Lobby> Join_Relay_Lobby(string lobbyId, string password = "") |
|||
{ |
|||
var player = new Player(m_PlayerId); |
|||
var options = new JoinLobbyByCodeOptions |
|||
{ |
|||
Player = player, |
|||
}; |
|||
m_CurrentLobby = await LobbyService.Instance.JoinLobbyByCodeAsync(lobbyId, options); |
|||
|
|||
Debug.Log($"Joined Lobby: {m_CurrentLobby.Id} - {m_CurrentLobby.Players.Count}"); |
|||
|
|||
m_RelayCode = m_CurrentLobby.Data["relayJoinCode"].Value; |
|||
|
|||
Debug.Log($"Joined Lobby: {m_CurrentLobby.Id} - {m_CurrentLobby.Players.Count} - RelayCode:{m_RelayCode}"); |
|||
|
|||
var joinAllocation = await Relay.Instance.JoinAllocationAsync(m_RelayCode); |
|||
|
|||
await Lobbies.Instance.UpdatePlayerAsync(m_CurrentLobby.Id, player.Id, |
|||
new UpdatePlayerOptions |
|||
{ |
|||
AllocationId = joinAllocation.AllocationId.ToString() |
|||
}); |
|||
Debug.Log($"Joined Lobby: {m_CurrentLobby.Id} - {m_CurrentLobby.Players.Count} - RelayCode:{m_RelayCode}"); |
|||
m_UnityTransport.SetClientRelayData(joinAllocation.RelayServer.IpV4, (ushort)joinAllocation.RelayServer.Port, |
|||
joinAllocation.AllocationIdBytes, joinAllocation.Key, joinAllocation.ConnectionData, |
|||
joinAllocation.HostConnectionData, false); |
|||
|
|||
if (!m_NetworkManager.StartClient()) |
|||
{ |
|||
Debug.Log("Could not start client?"); |
|||
} |
|||
|
|||
return m_CurrentLobby; |
|||
} |
|||
|
|||
async Task Host_Disconnect_Test() |
|||
{ |
|||
var lobby = await Host_Relay_Lobby(); |
|||
Debug.Log($"Created Lobby {lobby.Players.Count}"); |
|||
var kicked = false; |
|||
var callbacks = new LobbyEventCallbacks(); |
|||
callbacks.KickedFromLobby += () => |
|||
{ |
|||
Debug.Log("GOT KICKED!"); |
|||
kicked = true; |
|||
}; |
|||
await LobbyService.Instance.SubscribeToLobbyEventsAsync(lobby.Id, callbacks); |
|||
|
|||
await Task.Delay(5000); // Give it a moment
|
|||
|
|||
//Local Disconnect
|
|||
NetworkManager.Singleton.Shutdown(); |
|||
|
|||
//Wait for either the Lobby to signal that we were kicked, or for the time to run out. (Tested with up to 30s, still
|
|||
await Task.WhenAny(Task.Delay(10000), WaitForKicked()); |
|||
|
|||
async Task WaitForKicked() |
|||
{ |
|||
while (!kicked) |
|||
await Task.Delay(50); |
|||
} |
|||
|
|||
var getPostKickLobbyData = await LobbyService.Instance.GetLobbyAsync(lobby.Id); |
|||
|
|||
var getRelay = await Relay.Instance.JoinAllocationAsync(getPostKickLobbyData.Data["relayJoinCode"].Value); |
|||
|
|||
Debug.Log( |
|||
$"RelayAllocation : {getRelay.AllocationId} - {getRelay.RelayServer.IpV4} : {getRelay.RelayServer.Port}"); |
|||
StringBuilder sb = new StringBuilder("Endpoints: \n"); |
|||
foreach (var endpoint in getRelay.ServerEndpoints) |
|||
{ |
|||
sb.AppendLine( |
|||
$"Host: {endpoint.Host} - ConnectionType: {endpoint.ConnectionType} -Port: {endpoint.Port} - Secure?:{endpoint.Secure}"); |
|||
} |
|||
|
|||
Debug.Log($"Was {m_PlayerId} kicked? Lobby has {getPostKickLobbyData.Players.Count} player(s)"); |
|||
foreach (var player in getPostKickLobbyData.Players) |
|||
{ |
|||
Debug.Log($"Player: {player.Id}"); |
|||
} |
|||
} |
|||
|
|||
//need to start the lobby host in one instance and join from another
|
|||
async Task Join_Disconnect_Test() |
|||
{ |
|||
var lobby = await Join_Relay_Lobby(m_JoinCode); |
|||
|
|||
Debug.Log($"Created Lobby {lobby.Players.Count}"); |
|||
var kicked = false; |
|||
var callbacks = new LobbyEventCallbacks(); |
|||
callbacks.KickedFromLobby += () => |
|||
{ |
|||
Debug.Log($"GOT KICKED! @ {m_timer.Elapsed.Minutes} : {m_timer.Elapsed.Seconds}"); |
|||
kicked = true; |
|||
}; |
|||
|
|||
await LobbyService.Instance.SubscribeToLobbyEventsAsync(lobby.Id, callbacks); |
|||
|
|||
await Task.Delay(3000); |
|||
|
|||
//Local Disconnect
|
|||
NetworkManager.Singleton.Shutdown(); |
|||
|
|||
//Wait for either the Lobby to signal that we were kicked, or for the time to run out. (Tested with up to 30s, still
|
|||
await Task.WhenAny(Task.Delay(10000), WaitForKicked()); |
|||
|
|||
async Task WaitForKicked() |
|||
{ |
|||
while (!kicked) |
|||
{ |
|||
await Task.Delay(50); |
|||
} |
|||
} |
|||
|
|||
if (!kicked) |
|||
{ |
|||
Debug.LogError($"Player was not kicked"); |
|||
} |
|||
|
|||
var getPostKickLobbyData = await LobbyService.Instance.GetLobbyAsync(lobby.Id); |
|||
|
|||
var getRelay = await Relay.Instance.JoinAllocationAsync(getPostKickLobbyData.Data["relayJoinCode"].Value); |
|||
|
|||
Debug.Log( |
|||
$"RelayAllocation : {getRelay.AllocationId} - {getRelay.RelayServer.IpV4} : {getRelay.RelayServer.Port}"); |
|||
StringBuilder sb = new StringBuilder("Endpoints: \n"); |
|||
foreach (var endpoint in getRelay.ServerEndpoints) |
|||
{ |
|||
sb.AppendLine( |
|||
$"Host: {endpoint.Host} - ConnectionType: {endpoint.ConnectionType} -Port: {endpoint.Port} - Secure?:{endpoint.Secure}"); |
|||
} |
|||
|
|||
Debug.Log($"Lobby has {getPostKickLobbyData.Players.Count} player(s)"); |
|||
foreach (var player in getPostKickLobbyData.Players) |
|||
{ |
|||
Debug.Log($"Player: {player.Id}"); |
|||
} |
|||
} |
|||
|
|||
public void OnDestroy() |
|||
{ |
|||
m_CancellationTokenSource?.Cancel(); |
|||
Lobbies.Instance?.DeleteLobbyAsync(m_CurrentLobby.Id); |
|||
} |
|||
} |
|
|||
using System; |
|||
using Unity.Netcode; |
|||
using UnityEngine; |
|||
|
|||
public class NetworkRotator : NetworkBehaviour |
|||
{ |
|||
[SerializeField] |
|||
float m_moveSpeed = 2; |
|||
[SerializeField] |
|||
float m_RotateSpeed = 10; |
|||
bool m_CanRotate; |
|||
|
|||
public override void OnNetworkSpawn() |
|||
{ |
|||
m_CanRotate = IsHost || IsServer; |
|||
} |
|||
|
|||
void Update() |
|||
{ |
|||
if (!m_CanRotate) |
|||
return; |
|||
transform.Translate(0, 0, m_moveSpeed * Time.deltaTime); |
|||
transform.Rotate(0, m_RotateSpeed * Time.deltaTime, 0); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: bf933f918a1d55d4ca5270e20ae0b4f1 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!1 &1776632740697343230 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 2214535155992890716} |
|||
- component: {fileID: 7341743019006184406} |
|||
- component: {fileID: 7653666745749356973} |
|||
- component: {fileID: 4927301803555223107} |
|||
m_Layer: 0 |
|||
m_Name: Cube |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!4 &2214535155992890716 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1776632740697343230} |
|||
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} |
|||
m_LocalPosition: {x: 0, y: 0, z: 0} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_ConstrainProportionsScale: 0 |
|||
m_Children: [] |
|||
m_Father: {fileID: 3276792662332769107} |
|||
m_RootOrder: 0 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!33 &7341743019006184406 |
|||
MeshFilter: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1776632740697343230} |
|||
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} |
|||
--- !u!23 &7653666745749356973 |
|||
MeshRenderer: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1776632740697343230} |
|||
m_Enabled: 1 |
|||
m_CastShadows: 1 |
|||
m_ReceiveShadows: 1 |
|||
m_DynamicOccludee: 1 |
|||
m_StaticShadowCaster: 0 |
|||
m_MotionVectors: 1 |
|||
m_LightProbeUsage: 1 |
|||
m_ReflectionProbeUsage: 1 |
|||
m_RayTracingMode: 2 |
|||
m_RayTraceProcedural: 0 |
|||
m_RenderingLayerMask: 1 |
|||
m_RendererPriority: 0 |
|||
m_Materials: |
|||
- {fileID: 2100000, guid: 31321ba15b8f8eb4c954353edc038b1d, type: 2} |
|||
m_StaticBatchInfo: |
|||
firstSubMesh: 0 |
|||
subMeshCount: 0 |
|||
m_StaticBatchRoot: {fileID: 0} |
|||
m_ProbeAnchor: {fileID: 0} |
|||
m_LightProbeVolumeOverride: {fileID: 0} |
|||
m_ScaleInLightmap: 1 |
|||
m_ReceiveGI: 1 |
|||
m_PreserveUVs: 0 |
|||
m_IgnoreNormalsForChartDetection: 0 |
|||
m_ImportantGI: 0 |
|||
m_StitchLightmapSeams: 1 |
|||
m_SelectedEditorRenderState: 3 |
|||
m_MinimumChartSize: 4 |
|||
m_AutoUVMaxDistance: 0.5 |
|||
m_AutoUVMaxAngle: 89 |
|||
m_LightmapParameters: {fileID: 0} |
|||
m_SortingLayerID: 0 |
|||
m_SortingLayer: 0 |
|||
m_SortingOrder: 0 |
|||
m_AdditionalVertexStreams: {fileID: 0} |
|||
--- !u!65 &4927301803555223107 |
|||
BoxCollider: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 1776632740697343230} |
|||
m_Material: {fileID: 0} |
|||
m_IsTrigger: 0 |
|||
m_Enabled: 1 |
|||
serializedVersion: 2 |
|||
m_Size: {x: 1, y: 1, z: 1} |
|||
m_Center: {x: 0, y: 0, z: 0} |
|||
--- !u!1 &3276792662332769109 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 3276792662332769107} |
|||
- component: {fileID: 3276792662332769106} |
|||
- component: {fileID: 1867191166107922505} |
|||
- component: {fileID: 1314649806897867667} |
|||
- component: {fileID: 4464709501073654867} |
|||
- component: {fileID: 403382390143939945} |
|||
m_Layer: 0 |
|||
m_Name: TestNetworkHeartBeatObject |
|||
m_TagString: Untagged |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!4 &3276792662332769107 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3276792662332769109} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0, y: 0, z: 0} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_ConstrainProportionsScale: 0 |
|||
m_Children: |
|||
- {fileID: 2214535155992890716} |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 0 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!114 &3276792662332769106 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3276792662332769109} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
GlobalObjectIdHash: 951099334 |
|||
AlwaysReplicateAsRoot: 0 |
|||
DontDestroyWithOwner: 0 |
|||
AutoObjectParentSync: 1 |
|||
--- !u!114 &1867191166107922505 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3276792662332769109} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: e96cb6065543e43c4a752faaa1468eb1, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
SyncPositionX: 1 |
|||
SyncPositionY: 1 |
|||
SyncPositionZ: 1 |
|||
SyncRotAngleX: 1 |
|||
SyncRotAngleY: 1 |
|||
SyncRotAngleZ: 1 |
|||
SyncScaleX: 0 |
|||
SyncScaleY: 0 |
|||
SyncScaleZ: 0 |
|||
PositionThreshold: 0.001 |
|||
RotAngleThreshold: 0.01 |
|||
ScaleThreshold: 0.01 |
|||
InLocalSpace: 0 |
|||
Interpolate: 1 |
|||
--- !u!114 &1314649806897867667 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3276792662332769109} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: bf933f918a1d55d4ca5270e20ae0b4f1, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
m_moveSpeed: 1 |
|||
m_RotateSpeed: 1 |
|||
--- !u!54 &4464709501073654867 |
|||
Rigidbody: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3276792662332769109} |
|||
serializedVersion: 2 |
|||
m_Mass: 1 |
|||
m_Drag: 0 |
|||
m_AngularDrag: 0.05 |
|||
m_UseGravity: 0 |
|||
m_IsKinematic: 0 |
|||
m_Interpolate: 0 |
|||
m_Constraints: 0 |
|||
m_CollisionDetection: 0 |
|||
--- !u!114 &403382390143939945 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 3276792662332769109} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: f6c0be61502bb534f922ebb746851216, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|
|||
fileFormatVersion: 2 |
|||
guid: 5458b63e348081b4a89a3b5ca792748a |
|||
PrefabImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using Unity.Services.Authentication; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Unity.Netcode; |
|||
using Unity.Netcode.Transports.UTP; |
|||
using Unity.Services.Core; |
|||
using Unity.Services.Lobbies; |
|||
using Unity.Services.Lobbies.Models; |
|||
using Unity.Services.Relay; |
|||
using UnityEngine; |
|||
|
|||
public class TestMono : MonoBehaviour |
|||
{ |
|||
string m_WorkingLobbyId; |
|||
string playerID; |
|||
bool m_DidSigninComplete = false; |
|||
|
|||
// This is handled in the LobbyAsyncRequest calls normally, but we need to supply this for the dir
|
|||
Dictionary<string, PlayerDataObject> m_MockUserData; |
|||
UnityTransport m_UnityTransport; |
|||
NetworkManager m_NetworkManager; |
|||
|
|||
// Start is called before the first frame update
|
|||
public async void Start() |
|||
{ |
|||
await UnityServices.InitializeAsync(); |
|||
await AuthenticationService.Instance.SignInAnonymouslyAsync(); |
|||
m_NetworkManager = NetworkManager.Singleton; |
|||
|
|||
m_UnityTransport = m_NetworkManager.GetComponent<UnityTransport>(); |
|||
playerID = AuthenticationService.Instance.PlayerId; |
|||
|
|||
await Lobby_Relay_Disconnect_Async_Test(); |
|||
} |
|||
|
|||
async Task Lobby_Relay_Disconnect_Async_Test() |
|||
{ |
|||
var lobby = await Create_Relay_And_Lobby(); |
|||
Debug.Log($"Created Lobby {lobby.Players.Count}"); |
|||
var kicked = false; |
|||
var callbacks = new LobbyEventCallbacks(); |
|||
callbacks.KickedFromLobby += () => |
|||
{ |
|||
Debug.Log("GOT KICKED!"); |
|||
kicked = true; |
|||
}; |
|||
await LobbyService.Instance.SubscribeToLobbyEventsAsync(lobby.Id, callbacks); |
|||
|
|||
await Task.Delay(5000); // Give it a moment
|
|||
|
|||
//Local Disconnect
|
|||
NetworkManager.Singleton.Shutdown(); |
|||
|
|||
//Wait for either the Lobby to signal that we were kicked, or for the time to run out. (Tested with up to 30s, still
|
|||
await Task.WhenAny(Task.Delay(10000), WaitForKicked()); |
|||
async Task WaitForKicked() |
|||
{ |
|||
while (!kicked) |
|||
{ |
|||
await Task.Delay(50); |
|||
} |
|||
} |
|||
|
|||
var getPostKickLobbyData = await LobbyService.Instance.GetLobbyAsync(lobby.Id); |
|||
try |
|||
{ |
|||
var getRelay = await Relay.Instance.JoinAllocationAsync(getPostKickLobbyData.Data["joinCode"].Value); |
|||
|
|||
Debug.Log($"RelayAllocation : {getRelay.AllocationId} - {getRelay.RelayServer.IpV4} : {getRelay.RelayServer.Port}"); |
|||
StringBuilder sb = new StringBuilder("Endpoints: \n"); |
|||
foreach (var endpoint in getRelay.ServerEndpoints) |
|||
{ |
|||
sb.AppendLine($"Host: {endpoint.Host} - ConnectionType: {endpoint.ConnectionType} -Port: {endpoint.Port} - Secure?:{endpoint.Secure}"); |
|||
} |
|||
} |
|||
catch(Exception ex) |
|||
{ |
|||
Debug.Log($"Error getting Relay after disconnect: {ex}."); |
|||
} |
|||
Debug.Log($"Was {playerID} kicked? Lobby has {getPostKickLobbyData.Players.Count} player(s)"); |
|||
foreach (var player in getPostKickLobbyData.Players) |
|||
{ |
|||
Debug.Log($"Player: {player.Id}"); |
|||
} |
|||
} |
|||
|
|||
public async Task<Lobby> Create_Relay_And_Lobby() |
|||
{ |
|||
// Allocate Relay
|
|||
var allocation = await Relay.Instance.CreateAllocationAsync(2, null); |
|||
|
|||
try |
|||
{ |
|||
|
|||
Debug.Log($"RelayAllocation : {allocation.AllocationId} - {allocation.RelayServer.IpV4} : {allocation.RelayServer.Port}"); |
|||
StringBuilder sb = new StringBuilder("Endpoints: \n"); |
|||
foreach (var endpoint in allocation.ServerEndpoints) |
|||
{ |
|||
sb.AppendLine($"Host: {endpoint.Host} - ConnectionType: {endpoint.ConnectionType} -Port: {endpoint.Port} - Secure?:{endpoint.Secure}"); |
|||
} |
|||
} |
|||
catch(Exception ex) |
|||
{ |
|||
Debug.Log("Error getting Relay after disconnect: {ex}."); |
|||
} |
|||
// Generate Join Code
|
|||
var joinCode = await Relay.Instance.GetJoinCodeAsync(allocation.AllocationId); |
|||
|
|||
// Create Lobby
|
|||
var options = new CreateLobbyOptions |
|||
{ |
|||
Data = new Dictionary<string, DataObject> |
|||
{ |
|||
{ "joinCode", new DataObject(DataObject.VisibilityOptions.Public, joinCode) }, |
|||
}, |
|||
Player = new Player(playerID, allocationId: allocation.AllocationId.ToString()) |
|||
}; |
|||
var lobby = await LobbyService.Instance.CreateLobbyAsync("testLobby", 2, options); |
|||
m_UnityTransport.SetHostRelayData(allocation.RelayServer.IpV4, (ushort)allocation.RelayServer.Port, |
|||
allocation.AllocationIdBytes, allocation.Key, allocation.ConnectionData, true); |
|||
|
|||
// Start Host
|
|||
m_NetworkManager.StartHost(); |
|||
return lobby; |
|||
} |
|||
|
|||
public async Task<Lobby> Join(string lobbyId, string password = "") |
|||
{ |
|||
var player = new Player(playerID); |
|||
var options = new JoinLobbyByIdOptions() |
|||
{ |
|||
Player = player, |
|||
}; |
|||
var lobby = await LobbyService.Instance.JoinLobbyByIdAsync(lobbyId, options); |
|||
|
|||
// Join Relay
|
|||
var joinCode = lobby.Data["joinCode"].Value; |
|||
var join = await Relay.Instance.JoinAllocationAsync(joinCode); |
|||
|
|||
await Lobbies.Instance.UpdatePlayerAsync(lobby.Id, player.Id, new UpdatePlayerOptions |
|||
{ |
|||
AllocationId = join.AllocationId.ToString(), |
|||
ConnectionInfo = joinCode |
|||
}); |
|||
|
|||
m_UnityTransport.SetClientRelayData(join.RelayServer.IpV4, (ushort)join.RelayServer.Port, |
|||
join.AllocationIdBytes, join.Key, join.ConnectionData, join.HostConnectionData, true); |
|||
|
|||
m_NetworkManager.StartClient(); |
|||
return lobby; |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue