浏览代码

Merge branch 'master-staging'

/main
UnityJacob 2 年前
当前提交
83d46e73
共有 4 个文件被更改,包括 22 次插入79 次删除
  1. 4
      README.md
  2. 20
      CHANGELOG.md
  3. 11
      Assets/Scripts/GameLobby/Relay/RelayAPIInterface.cs.meta
  4. 66
      Assets/Scripts/GameLobby/Relay/RelayAPIInterface.cs

4
README.md


# Game Lobby Sample
_Tested with Unity 2020.3 for PC and Mac._
**Version 1.1**
_Tested with Unity 2021.2 for PC and Mac._
This sample demonstrates how to use the Lobby and Relay packages to create a typical game lobby experience. It also includes Vivox Voice chat. Players can host lobbies that other players can join using a public lobby list or lobby code, and then connect with Relay to use Unity Transport ("UTP") for basic real-time communication between them. Relay allows players to securely communicate with each other while maintaining connection anonymity. Connecting to the lobby will also connect to Vivox to enable voice chat as long as an audio input device is available.

20
CHANGELOG.md


#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.

11
Assets/Scripts/GameLobby/Relay/RelayAPIInterface.cs.meta


fileFormatVersion: 2
guid: e72a74dc383f2164b88dd15df02cbe5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

66
Assets/Scripts/GameLobby/Relay/RelayAPIInterface.cs


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);
}
}
}
}
正在加载...
取消
保存