55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using Unity.Services.Core;
|
|
using Unity.Services.Authentication;
|
|
using Unity.Services.Relay;
|
|
using Unity.Services.Relay.Http;
|
|
using Unity.Services.Relay.Models;
|
|
using Unity.Netcode;
|
|
using Unity.Netcode.Transports.UTP;
|
|
using Unity.Networking.Transport;
|
|
using Unity.Networking.Transport.Relay;
|
|
using NetworkEvent = Unity.Networking.Transport.NetworkEvent;
|
|
|
|
public class ServerMenu : MonoBehaviour
|
|
{
|
|
private async void Start() {
|
|
await UnityServices.InitializeAsync();
|
|
|
|
AuthenticationService.Instance.SignedIn += () => {
|
|
Debug.Log ("Signed in as: " + AuthenticationService.Instance.PlayerId);
|
|
};
|
|
await AuthenticationService.Instance.SignInAnonymouslyAsync();
|
|
}
|
|
|
|
public void HostButton () {
|
|
HostGame();
|
|
}
|
|
public async Task<string> HostGame () {
|
|
try {
|
|
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(3);
|
|
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
|
|
|
|
Debug.Log(joinCode);
|
|
|
|
RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
|
|
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
|
|
|
|
NetworkManager.Singleton.StartHost();
|
|
|
|
return joinCode;
|
|
} catch (RelayServiceException e) {
|
|
Debug.Log(e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void JoinGame () {
|
|
NetworkManager.Singleton.StartClient();
|
|
}
|
|
}
|