You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

169 lines
4.4 KiB

using System.Collections.Generic;
using TMPro;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
using Unity.Networking.Transport.Relay;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
using Unity.Services.Relay;
using Unity.Services.Relay.Models;
using UnityEngine;
public class TestLobby : MonoBehaviour
{
public TMP_InputField TMP_InputField;
public TMP_Text TMP_Text;
private Lobby hostLobby;
private float heartbeatTimer;
public async void Start()
{
await UnityServices.InitializeAsync();
AuthenticationService.Instance.SignedIn += () =>
{
Debug.Log("signed in " + AuthenticationService.Instance.PlayerId);
};
await AuthenticationService.Instance.SignInAnonymouslyAsync();
}
private async void CreateLobby()
{ try
{
string lobbyName = "Test";
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, 10);
Debug.Log(lobby.Name);
}
catch (LobbyServiceException ex)
{
Debug.Log(ex);
}
}
private void Update()
{
HandleLobbyHeartbeat();
if (Input.GetKeyDown(KeyCode.B))
{
CreateLobby();
}
if (Input.GetKeyDown(KeyCode.N))
{
ListLobbies();
}
}
private async void HandleLobbyHeartbeat()
{
if (hostLobby != null)
{
heartbeatTimer -= Time.deltaTime;
if (heartbeatTimer < 0f)
{
float heartbeatTimerMax = 15f;
heartbeatTimer = heartbeatTimerMax;
await LobbyService.Instance.SendHeartbeatPingAsync(hostLobby.Id);
}
}
}
private async void ListLobbies()
{
QueryLobbiesOptions options = new QueryLobbiesOptions()
{
Count = 25,
Filters = new List<QueryFilter>()
{
new QueryFilter(QueryFilter.FieldOptions.AvailableSlots, "0", QueryFilter.OpOptions.GT)
},
Order = new List<QueryOrder>()
{
new QueryOrder(false, QueryOrder.FieldOptions.Created)
}
};
QueryResponse lobbies = await Lobbies.Instance.QueryLobbiesAsync();
foreach (var lobbie in lobbies.Results)
{
Debug.Log($"{lobbie.Name} (${lobbie.Id})");
}
}
private void JoinLobby()
{
///
LobbyService.Instance.JoinLobbyByIdAsync("tst");
}
public async void CreateRelay()
{
try
{
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(3);
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
TMP_Text.text = joinCode;
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartHost();
}
catch (RelayServiceException ex)
{
Debug.Log(ex);
}
}
public async void Server()
{
try
{
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(3);
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
TMP_Text.text = joinCode;
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartServer();
}
catch (RelayServiceException ex)
{
Debug.Log(ex);
}
}
public async void JoinRelay(string code)
{
code = TMP_InputField.text;
TMP_Text.text = code;
try
{
JoinAllocation allocation = await RelayService.Instance.JoinAllocationAsync(code);
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartClient();
}
catch (RelayServiceException ex)
{
Debug.Log(ex);
}
}
}