본문 바로가기
유니티 개발일지

유니티 게임 개발 : 유저의 데이터를 자동으로 구글 클라우드에 저장하고 불러오는 방법

by 곰스타일 2023. 9. 26.
728x90
반응형
SMALL

만약 유저가 로그인한 구글 플레이 계정의 구글 클라우드에 데이터를 저장하고 불러오고 싶다면, Google Play 게임 서비스의 Saved Games 기능을 활용할 수 있습니다. 이를 통해 유저의 게임 데이터를 자동으로 구글 클라우드에 저장하고 불러올 수 있습니다.

아래는 이를 수행하는 기본적인 단계입니다:

  1. Google Play 게임 서비스 설정:
    • Google Play 콘솔에 로그인하고 프로젝트를 선택합니다.
    • 왼쪽 메뉴에서 "게임 센터" 섹션으로 이동합니다.
    • "게임 상태"를 선택하고 "게임 저장소"를 활성화합니다.
  2. 유저 데이터 저장:
    • 게임 데이터를 저장할 때, ISaveGameClient 인터페이스를 사용하여 구글 플레이 게임 서비스에 데이터를 저장합니다.
using GooglePlayGames;
using GooglePlayGames.BasicApi.SavedGame;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.Text;

public class SaveGameManager : MonoBehaviour
{
    private ISavedGameClient savedGameClient;

    private void Start()
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.Activate();

        savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    }

    public void SaveGame(string fileName, string data)
    {
        byte[] byteData = Encoding.UTF8.GetBytes(data);
        savedGameClient.OpenWithAutomaticConflictResolution(fileName, DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
    }

    private void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game)
    {
        if (status == SavedGameRequestStatus.Success)
        {
            savedGameClient.WriteGame(game, Encoding.UTF8.GetBytes("YourGameData"), OnGameSaved);
        }
        else
        {
            Debug.LogError("Failed to open saved game.");
        }
    }

    private void OnGameSaved(SavedGameRequestStatus status, ISavedGameMetadata game)
    {
        if (status == SavedGameRequestStatus.Success)
        {
            Debug.Log("Game saved successfully.");
        }
        else
        {
            Debug.LogError("Failed to save game.");
        }
    }
}

3. 데이터 불러오기:

  • 게임 시작 시, 저장된 데이터를 불러올 수 있습니다.
public void LoadGame(string fileName)
{
    savedGameClient.OpenWithAutomaticConflictResolution(fileName, DataSource.ReadCacheOrNetwork,
        ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpenedForLoading);
}

private void OnSavedGameOpenedForLoading(SavedGameRequestStatus status, ISavedGameMetadata game)
{
    if (status == SavedGameRequestStatus.Success)
    {
        savedGameClient.ReadBinaryData(game, OnGameLoaded);
    }
    else
    {
        Debug.LogError("Failed to open saved game for loading.");
    }
}

private void OnGameLoaded(SavedGameRequestStatus status, byte[] data)
{
    if (status == SavedGameRequestStatus.Success)
    {
        string loadedData = Encoding.UTF8.GetString(data);
        Debug.Log("Loaded data: " + loadedData);
    }
    else
    {
        Debug.LogError("Failed to load game.");
    }
}
 

 

위 코드에서 SaveGame 및 LoadGame 함수에서 사용되는 fileName은 유저마다 고유한 파일명이어야 합니다.

이제 이러한 코드를 사용하여 유저가 로그인한 구글 플레이 계정의 구글 클라우드에 데이터를 저장하고 불러올 수 있게 됩니다.


구글 플레이 게임 서비스를 사용하여 유저 데이터를 저장하려면 Google Play 게임 서비스 SDK를 사용해야 합니다. 이를 통해 Saved Games API를 활용하여 게임 데이터를 구글 클라우드에 저장하고 불러올 수 있습니다.

아래는 유저 데이터를 저장하고 불러오는 간단한 예제입니다.

 

using GooglePlayGames;
using GooglePlayGames.BasicApi.SavedGame;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    private ISavedGameClient savedGameClient;

    private void Start()
    {
        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
        PlayGamesPlatform.InitializeInstance(config);
        PlayGamesPlatform.Activate();

        savedGameClient = PlayGamesPlatform.Instance.SavedGame;
    }

    public void SaveGame(string fileName, string data)
    {
        byte[] byteData = System.Text.Encoding.UTF8.GetBytes(data);

        savedGameClient.OpenWithAutomaticConflictResolution(fileName,
            DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime,
            (status, metadata) => OnSavedGameOpenedForSave(status, metadata, byteData));
    }

    private void OnSavedGameOpenedForSave(SavedGameRequestStatus status, ISavedGameMetadata game, byte[] data)
    {
        if (status == SavedGameRequestStatus.Success)
        {
            savedGameClient.CommitUpdate(game, new SavedGameMetadataUpdate.Builder().Build(), data, OnGameSaved);
        }
        else
        {
            Debug.LogError("Failed to open saved game for saving.");
        }
    }

    private void OnGameSaved(SavedGameRequestStatus status, ISavedGameMetadata game)
    {
        if (status == SavedGameRequestStatus.Success)
        {
            Debug.Log("Game saved successfully.");
        }
        else
        {
            Debug.LogError("Failed to save game.");
        }
    }

    public void LoadGame(string fileName)
    {
        savedGameClient.OpenWithAutomaticConflictResolution(fileName,
            DataSource.ReadCacheOrNetwork,
            ConflictResolutionStrategy.UseLongestPlaytime,
            (status, metadata) => OnSavedGameOpenedForLoad(status, metadata));
    }

    private void OnSavedGameOpenedForLoad(SavedGameRequestStatus status, ISavedGameMetadata game)
    {
        if (status == SavedGameRequestStatus.Success)
        {
            savedGameClient.ReadBinaryData(game, OnGameLoaded);
        }
        else
        {
            Debug.LogError("Failed to open saved game for loading.");
        }
    }

    private void OnGameLoaded(SavedGameRequestStatus status, byte[] data)
    {
        if (status == SavedGameRequestStatus.Success)
        {
            string loadedData = System.Text.Encoding.UTF8.GetString(data);
            Debug.Log("Loaded data: " + loadedData);
        }
        else
        {
            Debug.LogError("Failed to load game.");
        }
    }
}

 

위 코드에서 SaveGame 및 LoadGame 함수에서 사용되는 fileName은 유저마다 고유한 파일명이어야 합니다.

이제 이 코드를 사용하여 유저가 로그인한 구글 플레이 계정의 구글 클라우드에 데이터를 저장하고 불러올 수 있습니다.

728x90
반응형
LIST

댓글