浏览代码

relay SDK upgrade, removed some redundancies' and brought the publicly available packages out of the Candidates scope.

/main/staging/Open_beta_Update/open_beta_relay
当前提交
221171ef
共有 12 个文件被更改,包括 75 次插入101 次删除
  1. 7
      Assets/Scripts/Game/GameManager.cs
  2. 14
      Assets/Scripts/Lobby/LobbyAsyncRequests.cs
  3. 77
      Assets/Scripts/Relay/RelayAPIInterface.cs
  4. 4
      Assets/Scripts/Relay/RelayUtpClient.cs
  5. 4
      Assets/Scripts/Relay/RelayUtpHost.cs
  6. 4
      Assets/Scripts/Relay/RelayUtpSetup.cs
  7. 19
      Assets/Scripts/Tests/PlayMode/RelayRoundTripTests.cs
  8. 8
      Packages/manifest.json
  9. 19
      Packages/packages-lock.json
  10. 12
      ProjectSettings/PackageManagerSettings.asset
  11. 8
      Assets/StreamingAssets.meta

7
Assets/Scripts/Game/GameManager.cs


using System;
using LobbyRelaySample.Relay;
using LobbyRelaySample.relay;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

private bool m_coolingDown;
/// <summary>Rather than a setter, this is usable in-editor. It won't accept an enum, however.</summary>
public void SetLobbyColorFilter(int color){ m_lobbyColorFilter = (LobbyColor)color; }
public void SetLobbyColorFilter(int color)
{
m_lobbyColorFilter = (LobbyColor)color;
}
private LobbyColor m_lobbyColorFilter;

14
Assets/Scripts/Lobby/LobbyAsyncRequests.cs


using System;
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
namespace LobbyRelaySample

}
#region Once connected to a lobby, cache the local lobby object so we don't query for it for every lobby operation.
// (This assumes that the player will be actively in just one lobby at a time, though they could passively be in more.)
private Queue<Action> m_pendingOperations = new Queue<Action>();
private string m_currentLobbyId = null;

onError?.Invoke(response); // TODO: Hmm...how do we know if there was a failure?
}
}
/// <param name="onComplete">If no lobby is retrieved, this is given null.</param>
private void RetrieveLobbyAsync(string lobbyId, Action<Lobby> onComplete)
{

void OnLeftLobby()
{
onComplete?.Invoke();
// Lobbies will automatically delete the lobby if unoccupied, so we don't need to take further action.
}
}

else
dataCurr.Add(dataNew.Key, dataObj);
}
LobbyAPIInterface.UpdateLobbyAsync(lobby.Id, dataCurr, (r) => { onComplete?.Invoke(); });
}

private bool ShouldUpdateData(Action caller, Action onComplete, bool shouldRetryIfLobbyNull)
{
if (m_isMidRetrieve)
{ m_pendingOperations.Enqueue(caller);
{
m_pendingOperations.Enqueue(caller);
Lobby lobby = m_lastKnownLobby;
if (lobby == null)
{

return false;
}
/// <summary>
/// Lobby requires a periodic ping to detect rooms that are still active, in order to mitigate "zombie" lobbies.
/// </summary>

if (m_heartbeatTime > k_heartbeatPeriod)
{
{
m_heartbeatTime -= k_heartbeatPeriod;
LobbyAPIInterface.HeartbeatPlayerAsync(m_lastKnownLobby.Id);
}

77
Assets/Scripts/Relay/RelayAPIInterface.cs


using System;
using System.Threading.Tasks;
using Unity.Services.Relay.Allocations;
namespace LobbyRelaySample.Relay
namespace LobbyRelaySample.relay
{
/// <summary>
/// Wrapper for all the interaction with the Relay API.

/// <summary>
/// A Relay Allocation represents a "server" for a new host.
/// </summary>
public static void AllocateAsync(int maxConnections, Action<Allocation> onComplete)
public static async void AllocateAsync(int maxConnections, Action<Allocation> onComplete)
CreateAllocationRequest createAllocationRequest = new CreateAllocationRequest(new AllocationRequest(maxConnections));
var task = RelayService.AllocationsApiClient.CreateAllocationAsync(createAllocationRequest);
AsyncRequest.DoRequest(task, OnResponse);
try
{
Allocation allocation = await Relay.Instance.CreateAllocationAsync(maxConnections);
void OnResponse(Response<AllocateResponseBody> response)
onComplete.Invoke(allocation);
}
catch (RelayServiceException ex)
if (response == null)
Debug.LogError("Relay returned a null Allocation. This might occur if the Relay service has an outage, if your cloud project ID isn't linked, or if your Relay package version is outdated.");
else if (response.Status >= 200 && response.Status < 300)
onComplete?.Invoke(response.Result.Data.Allocation);
else
Debug.LogError($"Allocation returned a non Success code: {response.Status}");
};
Debug.LogError(ex.Message + "\n" + ex.StackTrace);
throw;
}
}
/// <summary>

public static void GetJoinCodeAsync(Guid hostAllocationId, Action<string> onComplete)
public static async void GetJoinCodeAsync(Guid hostAllocationId, Action<string> onComplete)
GetJoinCodeAsync(hostAllocationId, a =>
try
{
string joinCode = await Relay.Instance.GetJoinCodeAsync(hostAllocationId);
onComplete.Invoke(joinCode);
}
catch (RelayServiceException ex)
if (a.Status >= 200 && a.Status < 300)
onComplete.Invoke(a.Result.Data.JoinCode);
else
{
Debug.LogError($"Join Code Get returned a non Success code: {a.Status}");
}
});
}
private static void GetJoinCodeAsync(Guid hostAllocationId, Action<Response<JoinCodeResponseBody>> onComplete)
{
CreateJoincodeRequest joinCodeRequest = new CreateJoincodeRequest(new JoinCodeRequest(hostAllocationId));
var task = RelayService.AllocationsApiClient.CreateJoincodeAsync(joinCodeRequest);
AsyncRequest.DoRequest(task, onComplete);
Debug.LogError(ex.Message + "\n" + ex.StackTrace);
throw;
}
public static void JoinAsync(string joinCode, Action<JoinAllocation> onComplete)
public static async void JoinAsync(string joinCode, Action<JoinAllocation> onComplete)
JoinAsync(joinCode, a =>
try
if (a.Status >= 200 && a.Status < 300)
onComplete.Invoke(a.Result.Data.Allocation);
else
{
Debug.LogError($"Join Call returned a non Success code: {a.Status}");
}
});
}
public static void JoinAsync(string joinCode, Action<Response<JoinResponseBody>> onComplete)
{
JoinRelayRequest joinRequest = new JoinRelayRequest(new JoinRequest(joinCode));
var task = RelayService.AllocationsApiClient.JoinRelayAsync(joinRequest);
AsyncRequest.DoRequest(task, onComplete);
JoinAllocation joinAllocation = await Relay.Instance.JoinAllocationAsync(joinCode);
onComplete.Invoke(joinAllocation);
}
catch (RelayServiceException ex)
{
Debug.LogError(ex.Message + "\n" + ex.StackTrace);
throw;
}
}
}
}

4
Assets/Scripts/Relay/RelayUtpClient.cs


using System.Collections.Generic;
using Unity.Networking.Transport;
using UnityEngine;
using MsgType = LobbyRelaySample.Relay.RelayUtpSetup.MsgType;
using MsgType = LobbyRelaySample.relay.RelayUtpSetup.MsgType;
namespace LobbyRelaySample.Relay
namespace LobbyRelaySample.relay
{
/// <summary>
/// This observes the local player and updates remote players over Relay when there are local changes, demonstrating basic data transfer over the Unity Transport (UTP).

4
Assets/Scripts/Relay/RelayUtpHost.cs


using System.Collections.Generic;
using Unity.Networking.Transport;
using MsgType = LobbyRelaySample.Relay.RelayUtpSetup.MsgType;
using MsgType = LobbyRelaySample.relay.RelayUtpSetup.MsgType;
namespace LobbyRelaySample.Relay
namespace LobbyRelaySample.relay
{
/// <summary>
/// In addition to maintaining a heartbeat with the Relay server to keep it from timing out, the host player must pass network events

4
Assets/Scripts/Relay/RelayUtpSetup.cs


using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Jobs;
namespace LobbyRelaySample.Relay
namespace LobbyRelaySample.relay
{
/// <summary>
/// Responsible for setting up a connection with Relay using UTP, for the lobby host.

protected NetworkDriver m_networkDriver;
protected List<NetworkConnection> m_connections;
protected NetworkEndPoint m_endpointForServer;
protected JobHandle m_currentUpdateHandle;
protected LocalLobby m_localLobby;
protected LobbyUser m_localUser;
protected Action<bool, RelayUtpClient> m_onJoinComplete;

19
Assets/Scripts/Tests/PlayMode/RelayRoundTripTests.cs


using System;
using System.Collections;
using LobbyRelaySample.Relay;
using LobbyRelaySample.relay;
using NUnit.Framework;
using Unity.Services.Relay;
using Unity.Services.Relay.Models;

Allocation allocation = null;
RelayAPIInterface.AllocateAsync(4, (a) => { allocation = a; });
while (allocation == null && timeout > 0)
{ yield return new WaitForSeconds(0.25f);
{
yield return new WaitForSeconds(0.25f);
timeout -= 0.25f;
}

string joinCode = null;
RelayAPIInterface.GetJoinCodeAsync(allocationId, (j) => { joinCode = j; });
while (joinCode == null && timeout > 0)
{ yield return new WaitForSeconds(0.25f);
{
yield return new WaitForSeconds(0.25f);
Response<JoinResponseBody> joinResponse = null;
JoinAllocation joinResponse = null;
{ yield return new WaitForSeconds(0.25f);
{
yield return new WaitForSeconds(0.25f);
var codeIp = joinResponse.Result.Data.Allocation.RelayServer.IpV4;
var codePort = joinResponse.Result.Data.Allocation.RelayServer.Port;
var codeIp = joinResponse.RelayServer.IpV4;
var codePort = joinResponse.RelayServer.Port;
Assert.AreEqual(codeIp, allocationIP);
Assert.AreEqual(codePort, allocationPort);
}

8
Packages/manifest.json


"com.unity.nuget.newtonsoft-json": "2.0.0",
"com.unity.services.authentication": "1.0.0-pre.4",
"com.unity.services.core": "1.0.0",
"com.unity.services.lobby": "1.0.0-pre.3",
"com.unity.services.relay": "1.0.0-pre.3",
"com.unity.services.lobby": "1.0.0-pre.4",
"com.unity.services.relay": "1.0.0-pre.4",
"com.unity.sysroot.linux-x86_64": "0.1.15-preview",
"com.unity.test-framework": "1.1.27",
"com.unity.textmeshpro": "3.0.6",

"name": "Candidates",
"url": "https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates",
"scopes": [
"com.unity.services.lobby",
"com.unity.services.relay",
"com.unity.services.authentication",
"com.unity.services.core",
"com.unity.transport"
]
}

19
Packages/packages-lock.json


"com.unity.services.core": "1.1.0-pre.8",
"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
},
"com.unity.services.core": {
"version": "1.1.0-pre.8",

"com.unity.modules.unitywebrequest": "1.0.0"
},
"url": "https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
"version": "1.0.0-pre.3",
"version": "1.0.0-pre.4",
"depth": 0,
"source": "registry",
"dependencies": {

"com.unity.modules.unitywebrequesttexture": "1.0.0",
"com.unity.modules.unitywebrequestwww": "1.0.0",
"com.unity.nuget.newtonsoft-json": "2.0.0",
"com.unity.services.authentication": "1.0.0-pre.3"
"com.unity.services.authentication": "1.0.0-pre.4"
"url": "https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
"version": "1.0.0-pre.3",
"version": "1.0.0-pre.4",
"com.unity.services.core": "1.1.0-pre.4",
"com.unity.services.core": "1.1.0-pre.8",
"com.unity.nuget.newtonsoft-json": "2.0.0"
"com.unity.nuget.newtonsoft-json": "2.0.0",
"com.unity.services.authentication": "1.0.0-pre.4"
"url": "https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates"
"url": "https://packages.unity.com"
},
"com.unity.sysroot": {
"version": "0.1.19-preview",

12
ProjectSettings/PackageManagerSettings.asset


m_Name: Candidates
m_Url: https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates
m_Scopes:
- com.unity.services.lobby
- com.unity.services.relay
- com.unity.services.authentication
- com.unity.services.core
- com.unity.transport
m_IsDefault: 0
m_Capabilities: 0

m_Name: Candidates
m_Url: https://artifactory.prd.cds.internal.unity3d.com/artifactory/api/npm/upm-candidates
m_Scopes:
- com.unity.services.lobby
- com.unity.services.relay
- com.unity.services.authentication
- com.unity.services.core
- com.unity.transport
m_IsDefault: 0
m_Capabilities: 0

m_Scopes:
- com.unity.services.lobby
- com.unity.services.relay
- com.unity.services.authentication
- com.unity.services.core
- com.unity.transport
m_SelectedScopeIndex: 0

8
Assets/StreamingAssets.meta


fileFormatVersion: 2
guid: 856fc4d990badc74a96656143dedb8e9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存