Quality of service
Use Quality of Service (QoS) measurements to automatically select the best Relay region for your players.
読み終わるまでの所要時間 1 分最終更新 1日前
Relay’s quality of service (QoS) feature allows you to select a region automatically based on quality of service data by starting the NetworkDriver as a host player without selecting a target region.
Creating an allocation request without specifying a region triggers the allocations service to use quality of service data to select the best available region based on the quality of the connection between each region and the host. Relay considers both latency and packet loss.
The following code is an example of how to create an allocation request:
private async Task<string> StartRelayServer(){ // Initialize UGS and authenticate if not already signed in await UnityServices.InitializeAsync(); if (!AuthenticationService.Instance.IsSignedIn) { await AuthenticationService.Instance.SignInAnonymouslyAsync(); } // Request an allocation to the Relay service var relayMaxPlayers = 5; Allocation allocation; try { allocation = await RelayService.Instance.CreateAllocationAsync(relayMaxPlayers); } catch (Exception e) { Debug.LogError($"Create allocation request failed: {e.Message}"); return null; } // Request the join code to share with clients string joinCode; try { joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId); } catch (Exception e) { Debug.LogError($"Create join code request failed: {e.Message}"); return null; } // Configure the transport with Relay server data using AllocationUtils NetworkManager.Singleton.GetComponent<UnityTransport>() .SetRelayServerData(AllocationUtils.ToRelayServerData(allocation, "dtls")); // Start the host (or use StartServer() for a dedicated server) return NetworkManager.Singleton.StartHost() ? joinCode : null;}