# 시작 시퀀스를 처리하기 위해 서비스를 초기화합니다.

> Unity Gaming Services, Authentication, In-App Purchasing을 (IAP) 초기화하여 소비자 간 직접 (D2C) 결제 제공업체와 연동할 수 있습니다.

IAP의 시작 시퀀스를 처리하기 위해 UGS(Unity Gaming Services)를 초기화하는 스크립트를 생성합니다. 이 시작 스크립트는 다음을 수행할 수 있습니다.

1. Unity Gaming Services를 초기화합니다.
2. Authentication 이벤트 핸들러를 설정합니다.
3. 로그인.
4. IAP를 초기화하고 원격 카탈로그를 가져옵니다. 자세한 내용은 [원격 카탈로그 페치](./fetch-remote-catalog.md)를 참고하십시오.

> **Note:**
>
> UGS를 초기화하기 전에 [환경을 설정](workflow#configure-your-environment-in-the-unity-editor)합니다.

초기화 스크립트에서 인스펙터에서 `PurchaseManager` 컴포넌트를 할당해야 합니다. `PurchaseManager` 컴포넌트는 다음 [Remote Catalog 페치](./fetch-remote-catalog.md) 단계의 일부로 생성됩니다.

## 서비스 초기화 예제 스크립트##initialize-services-example-script

다음 예제 스크립트의 이름은 `ServiceOrchestrator`입니다.

> **Important:**
>
> 세션 토큰이 손실된 후 구매가 지속되지 않으므로 익명 로그인을 인증 방법으로 사용하지 않는 것이 좋습니다. 마찰이 없는 플랫폼별 제공자가 앱에서 실현할 수 없는 경우 [애니메이션 인증 및 연결](/authentication/anonymous-auth-and-linking.md)을 참조하십시오.

```csharp
using System;
using System.Threading.Tasks;
using Unity.Services.Authentication;
using Unity.Services.Core;
using UnityEngine;
// Ensure you have reference to your PurchaseManager namespace if applicable

public class ServiceOrchestrator : MonoBehaviour
{
    // Assign this in the Inspector
    public PurchaseManager purchaseManager;

    async void Awake()
    {
        try {
        {
            // 1. Unity Gaming Services 초기화
            await Unity.Services.Core.UnityServices.InitializeAsync();


            // 2. Auth 이벤트 핸들러 설정
            SetupEvents();


            // 3. 로그인
            await SignUpAnonymouslyAsync();


            // 4. IAP 초기화 및 페치 카탈로그
            await purchaseManager.InitializeIAP();
        }
        catch(기타 e)
        {
            Debug.LogException(e);
        }
    }

    // Setup authentication event handlers if desired
    void SetupEvents() 
    {
        AuthenticationService.Instance.SignedIn += () => {
            // Shows how to get a playerID
            Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
            // Shows how to get an access token
            Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}");
        };

        AuthenticationService.Instance.SignInFailed += (err) => {
            Debug.LogError(err);
        };

        AuthenticationService.Instance.SignedOut += () => {
            Debug.Log("Player signed out.");
        };

        AuthenticationService.Instance.Expired += () => {
            Debug.Log("Player session could not be refreshed and expired.");
        };
    }

    // Sign in
    async Task SignUpAnonymouslyAsync()
    {
        try {
        {
            await AuthenticationService.Instance.SignInAnonymouslyAsync();
            Debug.Log("Sign in anonymously succeeded!");
            Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}");
        }
        catch(AuthenticationException ex)
        {
            // Compare error code to AuthenticationErrorCodes
            Debug.LogException(ex);
        }
        catch(RequestFailedException ex)
        {
            // Compare error code to CommonErrorCodes
            Debug.LogException(ex);
        }
    }
}
```

## 다음 단계##next-steps

이 페이지는 IAP를 사용하여 D2C 결제 제공업체를 설정하는 워크플로의 일부입니다. 이 워크플로를 계속하려면 다음 옵션 중 하나를 선택합니다.

[Integrate D2C payment providers](./workflow.md#initialize-services-to-handle-the-start-up-sequence): Integrate D2C payment providers with IAP 워크플로 페이지로 돌아갑니다.
[Fetch your Remote Catalog](./fetch-remote-catalog.md): 워크플로의 다음 단계로 이동하여 D2C 결제 제공업체를 설정합니다.
