UnityJacob
2 年前
当前提交
83d46e73
共有 4 个文件被更改,包括 22 次插入 和 79 次删除
-
4README.md
-
20CHANGELOG.md
-
11Assets/Scripts/GameLobby/Relay/RelayAPIInterface.cs.meta
-
66Assets/Scripts/GameLobby/Relay/RelayAPIInterface.cs
|
|||
#1.1 (2022-11-18) |
|||
## Highlights: |
|||
This updates the sample to support the latest Lobby-Wire integration, which introduces a websocket callback workflow for listening to Lobby changes. |
|||
This update also addresses multiple rounds of user feedback from discord, forums and github, where we heard that the sample had too much uneccesary infrastructure for it's size. |
|||
As such we've taken steps to simplify it. |
|||
|
|||
### **Lobby with Wire Integration** |
|||
Switch from polling model to a websocket-callback architecture for listening to Lobby changes. |
|||
### **Sample Simplification Refactor** |
|||
We heard feedback from users that there simply was too much in this sample. We took the opportunity that came with the incoming Wire changes to Lobby to take a hard look at the rest of the project and replace the generic infrastructure with much simpler coupled code. |
|||
### **Tasks over Callbacks** |
|||
We refactored to a largerly Task-based workflow, as it makes reading asynchronous service code much easier. |
|||
|
|||
## Features: |
|||
* **Observer** class removed, replaced with **CallbackValue<T>** which is responsible for notifying UI when a value has been changed, and hooks neatly into the Lobby API callback workflow. |
|||
* ** Messenger and Locator** patterns retired. Replaced with the more tightly coupled, but more readable **Unity Singleton Pattern** on the GameManager and InGameRunner |
|||
* **LobbySynchronizer** retired, we now subscribe to Lobby change callbacks in **LobbyManager.BindLocalLobbyToRemote** |
|||
* **RelayUTP** classes retired, the lobby change callbacks are so snappy we no longer need to use the Relay and Transport combo to create a decent Lobby experience. |
|||
|
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: e72a74dc383f2164b88dd15df02cbe5f |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using Unity.Services.Relay.Models; |
|||
using UnityEngine; |
|||
using RelayService = Unity.Services.Relay.Relay; |
|||
|
|||
namespace LobbyRelaySample.relay |
|||
{ |
|||
/// <summary>
|
|||
/// Wrapper for all the interaction with the Relay API.
|
|||
/// Relay acts as an intermediary between hosts and clients for privacy. Each player will connect to an obfuscated IP address provided by Relay as though connecting directly to other players.
|
|||
/// </summary>
|
|||
public static class RelayAPIInterface |
|||
{ |
|||
/// <summary>
|
|||
/// A Relay Allocation represents a "server" for a new host.
|
|||
/// </summary>
|
|||
public static void AllocateAsync(int maxConnections, Action<Allocation> onComplete) |
|||
{ |
|||
var task = RelayService.Instance.CreateAllocationAsync(maxConnections); |
|||
AsyncRequestRelay.Instance.DoRequest(task, OnResponse); |
|||
|
|||
void OnResponse(Allocation response) |
|||
{ |
|||
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 |
|||
onComplete?.Invoke(response); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Only after an Allocation has been completed can a Relay join code be obtained. This code will be stored in the lobby's data as non-public
|
|||
/// such that players can retrieve the Relay join code only after connecting to the lobby. (Note that this is not the same as the lobby code.)
|
|||
/// </summary>
|
|||
public static void GetJoinCodeAsync(Guid hostAllocationId, Action<string> onComplete) |
|||
{ |
|||
var task = RelayService.Instance.GetJoinCodeAsync(hostAllocationId); |
|||
AsyncRequestRelay.Instance.DoRequest(task, OnResponse); |
|||
|
|||
void OnResponse(string response) |
|||
{ |
|||
if (response == null) |
|||
Debug.LogError("Could not retrieve a Relay join code."); |
|||
else |
|||
onComplete?.Invoke(response); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Clients call this to retrieve the host's Allocation via a Relay join code.
|
|||
/// </summary>
|
|||
public static void JoinAsync(string joinCode, Action<JoinAllocation> onComplete) |
|||
{ |
|||
var task = RelayService.Instance.JoinAllocationAsync(joinCode); |
|||
AsyncRequestRelay.Instance.DoRequest(task, OnResponse); |
|||
|
|||
void OnResponse(JoinAllocation response) |
|||
{ |
|||
if (response == null) |
|||
Debug.LogError("Could not join async with Relay join code " + joinCode); |
|||
else |
|||
onComplete?.Invoke(response); |
|||
} |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue