您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

66 行
2.2 KiB

using System;
using Unity.Services.Relay;
using Unity.Services.Relay.Models;
using UnityEngine;
namespace LobbyRelaySample.relay
{
/// <summary>
/// Wrapper for all the interaction with the Relay API.
/// </summary>
public static class RelayAPIInterface
{
/// <summary>
/// A Relay Allocation represents a "server" for a new host.
/// </summary>
public static async void AllocateAsync(int maxConnections, Action<Allocation> onComplete)
{
try
{
Allocation allocation = await Relay.Instance.CreateAllocationAsync(maxConnections);
onComplete.Invoke(allocation);
}
catch (RelayServiceException ex)
{
Debug.LogError(ex.Message + "\n" + ex.StackTrace);
throw;
}
}
/// <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.
/// </summary>
public static async void GetJoinCodeAsync(Guid hostAllocationId, Action<string> onComplete)
{
try
{
string joinCode = await Relay.Instance.GetJoinCodeAsync(hostAllocationId);
onComplete.Invoke(joinCode);
}
catch (RelayServiceException ex)
{
Debug.LogError(ex.Message + "\n" + ex.StackTrace);
throw;
}
}
/// <summary>
/// Clients call this to retrieve the host's Allocation via a Relay join code.
/// </summary>
public static async void JoinAsync(string joinCode, Action<JoinAllocation> onComplete)
{
try
{
JoinAllocation joinAllocation = await Relay.Instance.JoinAllocationAsync(joinCode);
onComplete.Invoke(joinAllocation);
}
catch (RelayServiceException ex)
{
Debug.LogError(ex.Message + "\n" + ex.StackTrace);
throw;
}
}
}
}