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; using UnityEngine.UI; 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 HostGame () { try { Allocation allocation = await RelayService.Instance.CreateAllocationAsync(50); string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId); Debug.Log(joinCode); RelayServerData relayServerData = new RelayServerData(allocation, "dtls"); NetworkManager.Singleton.GetComponent().SetRelayServerData(relayServerData); NetworkManager.Singleton.StartHost(); joinCodeText.text = joinCode; return joinCode; } catch (RelayServiceException e) { Debug.Log(e); return null; } } public InputField joinTextCode; public Text joinCodeText; public void ServerJoinButton() { string joinCode = joinTextCode.text; JoinGame(joinCode); } public async Task JoinGame(string joinCode) { try { Debug.Log("Joining server with: " + joinCode); JoinAllocation joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode); RelayServerData relayServerData = new RelayServerData(joinAllocation, "dtls"); NetworkManager.Singleton.GetComponent().SetRelayServerData(relayServerData); NetworkManager.Singleton.StartClient(); return joinCode; } catch (RelayServiceException e) { Debug.Log(e); return null; } } }