Unity용 Multiplay Hosting SDK
Integrate Multiplay Hosting functionality into your Unity game server.
읽는 시간 5분최근 업데이트: 10시간 전
Unity용 Multiplay Hosting SDK에는 게임에서 Multiplay Hosting 스케일링과 게임 서버 서비스를 활용하는 데 필요한 모든 기능이 담겨 있습니다.
Unity.Services.Multiplay요구 사항 및 제한 사항
Unity용 Multiplay Hosting SDK는 Unity 에디터 버전 2020.3 이상에서 작동합니다.SDK 초기화
Instance 메서드를 사용하여IMultiplayServiceasync void Example_InitSDK(){ try { await UnityServices.InitializeAsync(); } catch (Exception e) { Debug.Log(e); }}
게임 서버 구성
ServerConfigServerConfigServerConfigServerConfigServerConfig파라미터 | 유형 | 설명 |
|---|---|---|
| long | 서버 ID입니다. |
| 문자열 | 할당 ID입니다. |
| ushort | 서버 쿼리 프로토콜 포트 번호입니다. |
| ushort | 게임 세션의 연결 포트입니다. 게임 클라이언트가 게임 세션에 연결하기 위해 사용할 수 있는 포트 번호입니다. |
| 문자열 | 게임 서버가 로그 파일을 저장하는 디렉토리입니다. |
ServerConfigusing System.Collections;using System.Collections.Generic;using Unity.Services.Multiplay;using UnityEngine;public class Example_ServerConfiguration{ /// <summary> /// A simple example of accessing each of the server config's fields and printing them to the debug console. /// </summary> public static void LogServerConfig() { var serverConfig = MultiplayService.Instance.ServerConfig; Debug.Log($"Server ID[{serverConfig.ServerId}]"); Debug.Log($"AllocationID[{serverConfig.AllocationId}]"); Debug.Log($"Port[{serverConfig.Port}]"); Debug.Log($"QueryPort[{serverConfig.QueryPort}"); Debug.Log($"LogDirectory[{serverConfig.ServerLogDirectory}]"); }}
게임 서버 준비
ReadyServerForPlayersAsyncusing System.Threading.Tasks;using Unity.Services.Multiplay;/// <summary>/// Ready the server. This is to indicate that the server is ready to accept players./// Readiness is the server's way of saying it's ready for players to join the server./// You must wait until you have been Allocated before you can call ReadyServerForPlayersAsync./// </summary>private async void Example_ReadyingServer(){ // After the server is back to a blank slate and ready to accept new players await MultiplayService.Instance.ReadyServerForPlayersAsync();}
게임 서버 준비 해제
UnreadyServerAsyncusing System.Threading.Tasks;using Unity.Services.Multiplay;/// <summary>/// Unready the server. This is to indicate that the server is in some condition which means it can't accept players./// For example, after a game has ended and you need to reset the server to prepare for a new match./// </summary>private async void Example_UnreadyingServer(){ // The match has ended and players are disconnected from the server await MultiplayService.Instance.UnreadyServerAsync();}
서버 쿼리 핸들러 시작
StartServerQueryHandlerAsync파라미터 | 유형 | 설명 |
|---|---|---|
| ushort | 서버의 최대 플레이어 수입니다. |
| 문자열 | 서버 이름입니다. |
| 문자열 | 서버가 실행 중인 게임 유형의 이름 또는 식별자입니다. |
| 문자열 | 게임의 버전입니다. |
| 문자열 | 서버가 게임에서 실행 중인 맵 또는 월드입니다. |
| ushort | 게임 클라이언트가 게임 서버에 연결하기 위해 사용할 수 있는 포트 번호입니다. |
StartServerQueryHandlerAsyncparametersIServerCheckManagerUpdateServerCheck()UpdateServerCheck()
다음 예시는 서버 쿼리 핸들러를 시작하는 방법을 보여 줍니다.
using System.Collections;using System.Collections.Generic;using UnityEngine;using Unity.Services.Multiplay;/// <summary>/// An example of how to use SQP from the server using the Multiplay SDK./// The ServerQueryHandler reports the given information to the Multiplay Service./// </summary>public class Example_ServerQueryHandler : MonoBehaviour{ private const ushort k_DefaultMaxPlayers = 10; private const string k_DefaultServerName = "MyServerExample"; private const string k_DefaultGameType = "MyGameType"; private const string k_DefaultBuildId = "MyBuildId"; private const string k_DefaultMap = "MyMap"; public ushort currentPlayers; private IServerQueryHandler m_ServerQueryHandler; private async void Start() { m_ServerQueryHandler = await MultiplayService.Instance.StartServerQueryHandlerAsync(k_DefaultMaxPlayers, k_DefaultServerName, k_DefaultGameType, k_DefaultBuildId, k_DefaultMap); } private void Update() { m_ServerQueryHandler.UpdateServerCheck(); } public void ChangeQueryResponseValues(ushort maxPlayers, string serverName, string gameType, string buildId) { m_ServerQueryHandler.MaxPlayers = maxPlayers; m_ServerQueryHandler.ServerName = serverName; m_ServerQueryHandler.GameType = gameType; m_ServerQueryHandler.BuildId = buildId; } public void PlayerCountChanged(ushort newPlayerCount) { m_ServerQueryHandler.CurrentPlayers = newPlayerCount; }}
서버 이벤트 구독
SubscribeToServerEventsAsyncOnAllocateOnDeallocateOnErrorusing System;using System.Collections;using System.Collections.Generic;using Unity.Services.Multiplay;using UnityEngine;/// <summary>/// An example of how to access and react to multiplay server events./// </summary>public class Example_ServerEvents : MonoBehaviour{ private MultiplayEventCallbacks m_MultiplayEventCallbacks; private IServerEvents m_ServerEvents; /// <summary> /// This should be done early in the server's lifecycle, as you'll want to receive events as soon as possible. /// </summary> private async void Start() { // We must first prepare our callbacks like so: m_MultiplayEventCallbacks = new MultiplayEventCallbacks(); m_MultiplayEventCallbacks.Allocate += OnAllocate; m_MultiplayEventCallbacks.Deallocate += OnDeallocate; m_MultiplayEventCallbacks.Error += OnError; m_MultiplayEventCallbacks.SubscriptionStateChanged += OnSubscriptionStateChanged; // We must then subscribe. m_ServerEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(m_MultiplayEventCallbacks); }// Handle Multiplay events.}
Multiplay Hosting 이벤트 처리
MultiplayEventCallbacksOnAllocateOnDeallocateOnErrorusing System;using System.Collections;using System.Collections.Generic;using Unity.Services.Multiplay;using UnityEngine;/// <summary>/// An example of how to access and react to multiplay server events./// </summary>public class Example_ServerEvents : MonoBehaviour{ private MultiplayEventCallbacks m_MultiplayEventCallbacks; private IServerEvents m_ServerEvents; /// <summary> /// This should be done early in the server's lifecycle, as you'll want to receive events as soon as possible. /// </summary> private async void Start() { // We must first prepare our callbacks like so: m_MultiplayEventCallbacks = new MultiplayEventCallbacks(); m_MultiplayEventCallbacks.Allocate += OnAllocate; m_MultiplayEventCallbacks.Deallocate += OnDeallocate; m_MultiplayEventCallbacks.Error += OnError; m_MultiplayEventCallbacks.SubscriptionStateChanged += OnSubscriptionStateChanged; // We must then subscribe. m_ServerEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(m_MultiplayEventCallbacks); } /// <summary> /// Handler for receiving the allocation multiplay server event. /// </summary> /// <param name="allocation">The allocation received from the event.</param> private void OnAllocate(MultiplayAllocation allocation) { // Here is where you handle the allocation. // This is highly dependent on your game, however this would typically be some sort of setup process. // Whereby, you spawn NPCs, setup the map, log to a file, or otherwise prepare for players. // After you the allocation has been handled, you can then call ReadyServerForPlayersAsync()! } /// <summary> /// Handler for receiving the deallocation multiplay server event. /// </summary> /// <param name="deallocation">The deallocation received from the event.</param> private void OnDeallocate(MultiplayDeallocation deallocation) { // Here is where you handle the deallocation. // This is highly dependent on your game, however this would typically be some sort of teardown process. // You might want to deactivate unnecessary NPCs, log to a file, or perform any other cleanup actions. } /// <summary> /// Handler for receiving the error multiplay server event. /// </summary> /// <param name="error">The error received from the event.</param> private void OnError(MultiplayError error) { // Here is where you handle the error. // This is highly dependent on your game. You can inspect the error by accessing the error.Reason and error.Detail fields. // You can change on the error.Reason field, log the error, or otherwise handle it as you need to. } /// <summary> /// /// </summary> /// <param name="state"></param> private void OnSubscriptionStateChanged(MultiplayServerSubscriptionState state) { switch (state) { case MultiplayServerSubscriptionState.Unsubscribed: /* The Server Events subscription has been unsubscribed from. */ break; case MultiplayServerSubscriptionState.Synced: /* The Server Events subscription is up to date and active. */ break; case MultiplayServerSubscriptionState.Unsynced: /* The Server Events subscription has fallen out of sync, the subscription tries to automatically recover. */ break; case MultiplayServerSubscriptionState.Error: /* The Server Events subscription has fallen into an errored state and won't recover automatically. */ break; case MultiplayServerSubscriptionState.Subscribing: /* The Server Events subscription is trying to sync. */ break; } }}
Allocate
public event Action<MultiplayAllocation> Allocate;
AllocateMultiplayAllocationDeallocate
public event Action<MultiplayDeallocation> Deallocate;
DeallocateMultiplayDeallocationError
public event Action<MultiplayError> Error;
ErrorMultiplayErrorSubscriptionStateChange
public event Action<MultiplayServerSubscriptionState> SubscriptionStateChanged;
SubscriptionStateChangedMultiplayServerSubscriptionStateUnity 에디터로 리소스 배포
Multiplay 패키지와 함께 설치되는 Multiplay Authoring 모듈을 사용하면 필요에 따라 Unity 에디터에서 직접 리소스를 저작하고 수정할 수 있습니다. 그런 다음 Deployment 패키지를 사용하여 Unity 에디터에서 Unity Dashboard에 리소스를 업로드할 수 있습니다. Unity 에디터 내에 있는 Multiplay 구성을 사용하면 사용자는 소스 컨트롤을 클라우드에 있는 버전이 아닌 SSOT(single source of truth)로 취급하여 롤백, 이분화, 기타 일반적인 연산 등의 작업을 간소화할 수 있습니다.Multiplay 설정
Unity 에디터에서 Multiplay를 사용하려면 다음을 수행합니다.필수 패키지 설치
에디터에서 Multiplay 구성을 생성하려면 다음 패키지를 설치합니다.- Deployment
- Multiplay:
- Unity 6 이상: Multiplayer
- Unity 2022 LTS 이하: Multiplay
위 패키지를 설치한 후 다음 단계에 따라 사용 가능한 패키지 목록에 패키지를 추가합니다.
- Unity 에디터에서 Window > Package Manager를 선택합니다.
- + > Install package by name을 선택합니다.
- 를 입력합니다.
com.unity.services.deployment - Install을 선택합니다.
- Multiplay에도 같은 단계를 반복합니다.
- Unity 6 이상:
com.unity.services.multiplayer - Unity 2022 LTS 이하:
com.unity.services.multiplay
- Unity 6 이상:
프로젝트 연결
Unity Gaming Services 프로젝트를 Unity 에디터와 연결합니다. Unity Dashboard에서 UGS 프로젝트 ID를 확인할 수 있습니다.- Unity 에디터에서 Edit > Project Settings > Services를 선택합니다.
- 프로젝트를 연결합니다.
- 프로젝트에 Unity 프로젝트 ID가 없는 경우 다음 단계를 진행합니다.
- Create a Unity Project ID > Organizations를 선택한 다음 드롭다운 메뉴에서 조직을 선택합니다.
- Create project ID를 선택합니다.
- Unity 프로젝트 ID가 있는 경우 다음 단계를 진행합니다.
- Use an existing Unity project ID를 선택합니다.
- 드롭다운 메뉴에서 조직과 프로젝트를 선택합니다.
- Link project ID를 선택합니다.
UnityEditor.CloudProjectSettings.projectIdUnity 에디터에서 저작
Multiplay Authoring 모듈을 사용하면 Unity 에디터 내에서 바로 Multiplay 구성을 생성, 편집, 배포할 수 있습니다.구성 생성
Multiplay Authoring 모듈을 사용하여 Multiplay 구성을 생성하려면 다음 단계를 수행합니다.- Unity 에디터에서 프로젝트(Project) 창을 오른쪽 클릭하고 Create > Services > Multiplay Config를 선택합니다.
- 리소스 파일의 이름을 지정합니다.
- Enter 키를 누릅니다.
구성 편집
현재 한 가지 메서드로 기존 구성을 편집할 수 있습니다.- Project 탭에서 기존 리소스를 더블 클릭한 다음 텍스트 에디터를 선택하여 구성을 편집합니다.
리소스 파일 콘텐츠
Deployment 창에서 리소스를 확인하고 파일 확장자에 따라 유형을 할당합니다. 구성은yaml.gshnew_multiplay_config.gsh구성은 파일 내 3개의 구성 요소를 설명합니다.version: 1.0builds: my build: # replace with the name for your build executableName: Build.x86_64 # the name of your build executable buildPath: Builds/Multiplay # the location of the build filesbuildConfigurations: my build configuration: # replace with the name for your build configuration build: my build # replace with the name for your build queryType: sqp # sqp or a2s, delete if you do not have logs to query binaryPath: Build.x86_64 # the name of your build executable commandLine: -port $$port$$ -queryport $$query_port$$ -log $$log_dir$$/Engine.log # launch parameters for your server variables: {} cores: 1 # number of cores per server speedMhz: 750 # launch parameters for your server memoryMiB: 800 # launch parameters for your serverfleets: my fleet: # replace with the name for your fleet buildConfigurations: - my build configuration # replace with the names of your build configuration regions: North America: # North America, Europe, Asia, South America, Australia minAvailable: 0 # minimum number of servers running in the region maxServers: 1 # maximum number of servers running in the region