DTO
Define data transfer objects to transfer data between client and server.
읽는 시간 1분최근 업데이트: 한 달 전
모듈에서 DTO(데이터 전송 오브젝트)를 정의할 수 있습니다. DTO는 클라이언트와 서버 간에 데이터를 전송하는 데 사용됩니다. 예를 들어 DTO는 모듈 데이터를 JSON으로 직렬화한 후 클라이언트 측에서 동일한 구조로 역직렬화하려는 경우에 유용할 수 있습니다.
필수 조건
DTO를 시작하려면 먼저 Cloud Code 모듈을 생성해야 합니다.DTO 관리
모듈 엔드포인트 함수의 데이터 유형을 캡처하는 DTO를 정의할 수 있습니다.DTO 프로젝트 생성 및 구성
시작하려면 DTO를 저장할 C# 프로젝트를 만들고 메인 프로젝트에서 해당 프로젝트에 대한 레퍼런스를 추가합니다. 프로젝트에서 레퍼런스를 관리하는 방법에 관한 Microsoft 기술 자료를 참고하십시오. 모듈에 관한 자세한 내용은 모듈 구조를 참고하십시오. 그다음 Unity 에디터에서 지원되는 런타임 .NET 버전(netstandard<project_name>.csprojTargetFrameworknetstandard.2.1<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.1</TargetFramework> <ImplicitUsings>disable</ImplicitUsings> <RootNamespace>DTOSample</RootNamespace> </PropertyGroup></Project>
모듈에 DTO 추가
모듈에 DTO를 추가하려면 프로젝트에서 새 클래스를 정의하여 DTO를 저장해야 합니다. 이는 다음 예시와 유사할 수 있습니다.namespace DTOSample{ public class DiceRollDto { public DiceRollDto(int roll, int sides) { Roll = roll; Sides = sides; } public int Roll { get; set; } public int Sides { get; set; } }}
모듈 로직에 DTO 사용
모듈 함수가 포함된 메인 프로젝트에서 게임 로직을 정의하고, 정의된 DTO를 함수 반환 유형으로 사용합니다. 간단한 모듈 엔드포인트의 예시는 다음과 같습니다.using DTOSample;using Unity.Services.CloudCode.Core;namespace Sample;public class HelloWorld{ [CloudCodeFunction("RollDice")] public async Task<DiceRollDTO> RollDice(int diceSides) { var random = new Random(); var roll = random.Next(1, diceSides); return new DiceRollDTO(roll, diceSides); }}
DLL 추출
응답 유형과 일치하는 DTO를 Unity 프로젝트에서 사용하려면 모듈 C# 프로젝트에서 DLL을 추출해야 합니다. 이 단계에서는 나중에 모듈 함수를 호출할 수 있도록 모듈을 배포해야 합니다. 매뉴얼 패키지의 경우 어셈블리를 생성하는 방법에 관한 자세한 내용은 코드 패키징을 참고하십시오. 모듈을 배포할 때 어셈블리를 생성하면 기본적으로 모듈 프로젝트의bin/Debug/Release/net6.0/linux-x64/publish├─ Main.csproj└─ bin └─ Debug └─ Release └─ net6.0 └─ linux-x64 └─ publish └─ Main.dll └─ Main.pdb └─ DTOs.dll └─ DTOs.pdb ...
DTOs.dllUnity 프로젝트로 DLL 임포트
게임에 외부 DLL을 사용하려면 Unity 프로젝트 내부의AssetsUnity MonoBehaviour 스크립트에서 DTO 재사용
Cloud Code SDK를 호출하고 동일한 DTO를 사용하여 응답을 역직렬화할 수 있습니다.다음의 성공적인 응답 예시를 레퍼런스로 사용합니다.using DTOSample;using UnityEngine;using Unity.Services.Authentication;using Unity.Services.CloudCode;using Unity.Services.Core;public class Test : MonoBehaviour{ // Call this method to roll the dice (use a button) public async void Awake() { await UnityServices.InitializeAsync(); // Sign in anonymously to the Authentication service if (!AuthenticationService.Instance.IsSignedIn) await AuthenticationService.Instance.SignInAnonymouslyAsync(); // Call out to the Roll Dice script in Cloud Code var response = await CloudCodeService.Instance.CallModuleEndpointAsync<DiceRollDto>("Main", "RollDice", new Dictionary<string, object>() { {"diceSides", 6} }); // Log the response of the script in console Debug.Log($"You rolled {response.Roll} / {response.Sides}"); }}
자세한 내용은 Unity Runtime에서 모듈 실행을 참고하십시오."You rolled 5 / 6"