voidShowSelectUI(){uintmaxNumToDisplay=5;boolallowCreateNew=false;boolallowDelete=true;ISavedGameClientsavedGameClient=PlayGamesPlatform.Instance.SavedGame;savedGameClient.ShowSelectSavedGameUI("Select saved game",maxNumToDisplay,allowCreateNew,allowDelete,OnSavedGameSelected);}publicvoidOnSavedGameSelected(SelectUIStatusstatus,ISavedGameMetadatagame){if(status==SelectUIStatus.SavedGameSelected){// handle selected game save}else{// handle cancel or error}}
저장된 게임 열기
저장된 게임에 데이터를 읽거나 쓰려면 저장된 게임을 열어야 합니다. 저장된 게임 상태는 기기에 로컬로 캐시되며 클라우드에 저장되므로 저장된 데이터의 상태에서는 충돌이 발생할 수 있습니다.
기기가 상태를 클라우드에 저장하려고 시도하지만 현재 클라우드에 있는 데이터를 다른 기기에서 작성한 경우 충돌이 발생합니다. 이러한 충돌은 저장된 게임 데이터를 열 때 해결해야 합니다.
충돌 해결을 처리하는 두 가지 개방형 메서드가 있습니다. 첫 번째 메서드 OpenWithAutomaticConflictResolution은 표준 해결 전략 유형을 허용하고 충돌을 자동으로 해결합니다. 다른 메서드 OpenWithManualConflictResolution은 콜백 메서드를 허용하여 충돌의 수동 해결을 허용합니다.
voidOpenSavedGame(stringfilename){ISavedGameClientsavedGameClient=PlayGamesPlatform.Instance.SavedGame;savedGameClient.OpenWithAutomaticConflictResolution(filename,DataSource.ReadCacheOrNetwork,ConflictResolutionStrategy.UseLongestPlaytime,OnSavedGameOpened);}publicvoidOnSavedGameOpened(SavedGameRequestStatusstatus,ISavedGameMetadatagame){if(status==SavedGameRequestStatus.Success){// handle reading or writing of saved game.}else{// handle error}}
저장된 게임 작성
저장된 게임 파일이 열리면 파일을 작성하여 게임 상태를 저장할 수 있습니다.
이를 위해 CommitUpdate를 호출하면 됩니다. CommitUpdate에 다음과 같은 네 가지 매개변수가 있습니다.
콜백(공개 호출 중 하나에 전달된 콜백)에 전달된, 저장된 게임 메타데이터
메타데이터에 적용할 업데이트
데이터의 실제 바이트 배열
커밋이 완료될 때 호출할 콜백
voidSaveGame(ISavedGameMetadatagame,byte[]savedData,TimeSpantotalPlaytime){ISavedGameClientsavedGameClient=PlayGamesPlatform.Instance.SavedGame;SavedGameMetadataUpdate.Builderbuilder=newSavedGameMetadataUpdate.Builder();builder=builder.WithUpdatedPlayedTime(totalPlaytime).WithUpdatedDescription("Saved game at "+DateTime.Now());if(savedImage!=null){// This assumes that savedImage is an instance of Texture2D// and that you have already called a function equivalent to// getScreenshot() to set savedImage// NOTE: see sample definition of getScreenshot() method belowbyte[]pngData=savedImage.EncodeToPNG();builder=builder.WithUpdatedPngCoverImage(pngData);}SavedGameMetadataUpdateupdatedMetadata=builder.Build();savedGameClient.CommitUpdate(game,updatedMetadata,savedData,OnSavedGameWritten);}publicvoidOnSavedGameWritten(SavedGameRequestStatusstatus,ISavedGameMetadatagame){if(status==SavedGameRequestStatus.Success){// handle reading or writing of saved game.}else{// handle error}}publicTexture2DgetScreenshot(){// Create a 2D texture that is 1024x700 pixels from which the PNG will be// extractedTexture2DscreenShot=newTexture2D(1024,700);// Takes the screenshot from top left hand corner of screen and maps to top// left hand corner of screenShot texturescreenShot.ReadPixels(newRect(0,0,Screen.width,(Screen.width/1024)*700),0,0);returnscreenShot;}
저장된 게임 읽기
저장된 게임 파일이 열리면 파일을 읽어 게임 상태를 로드할 수 있습니다. 이를 위해 ReadBinaryData를 호출하면 됩니다.
voidLoadGameData(ISavedGameMetadatagame){ISavedGameClientsavedGameClient=PlayGamesPlatform.Instance.SavedGame;savedGameClient.ReadBinaryData(game,OnSavedGameDataRead);}publicvoidOnSavedGameDataRead(SavedGameRequestStatusstatus,byte[]data){if(status==SavedGameRequestStatus.Success){// handle processing the byte array data}else{// handle error}}
저장된 게임 삭제
저장된 게임 파일이 열리면 파일을 삭제할 수 있습니다. 이를 위해 Delete를 호출하면 됩니다.
voidDeleteGameData(stringfilename){// Open the file to get the metadata.ISavedGameClientsavedGameClient=PlayGamesPlatform.Instance.SavedGame;savedGameClient.OpenWithAutomaticConflictResolution(filename,DataSource.ReadCacheOrNetwork,ConflictResolutionStrategy.UseLongestPlaytime,DeleteSavedGame);}publicvoidDeleteSavedGame(SavedGameRequestStatusstatus,ISavedGameMetadatagame){if(status==SavedGameRequestStatus.Success){ISavedGameClientsavedGameClient=PlayGamesPlatform.Instance.SavedGame;savedGameClient.Delete(game);}else{// handle error}}
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-07-27(UTC)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-27(UTC)"],[],[],null,["# Saved games in Unity games\n\nThis topics describes how to use saved games for Play Games Services in Unity\ngames.\n\nBefore you start\n----------------\n\n- Set up your project and the Google Play Games plugin for Unity. For details,\n see the [Get started guide](/games/pgs/unity/unity-start).\n\n- Enabled saved games. See the\n [saved games](/games/pgs/console/enable-features#enable_saved_games) for\n details.\n\nDisplay the saved games UI\n--------------------------\n\nThe standard UI for selecting or creating a saved game entry is displayed by\ncalling: \n\n void ShowSelectUI() {\n uint maxNumToDisplay = 5;\n bool allowCreateNew = false;\n bool allowDelete = true;\n\n ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;\n savedGameClient.ShowSelectSavedGameUI(\"Select saved game\",\n maxNumToDisplay,\n allowCreateNew,\n allowDelete,\n OnSavedGameSelected);\n }\n\n public void OnSavedGameSelected (SelectUIStatus status, ISavedGameMetadata game) {\n if (status == SelectUIStatus.SavedGameSelected) {\n // handle selected game save\n } else {\n // handle cancel or error\n }\n }\n\nOpen a saved game\n-----------------\n\nIn order to read or write data to a saved game, the saved game needs to be\nopened. Since the saved game state is cached locally on the device and saved to\nthe cloud, it is possible to encounter conflicts in the state of the saved data.\nA conflict happens when a device attempts to save state to the cloud but the\ndata currently on the cloud was written by a different device. These conflicts\nneed to be resolved when opening the saved game data.\n\nThere are 2 open methods that handle conflict resolution, the first\n**OpenWithAutomaticConflictResolution** accepts a standard resolution strategy\ntype and automatically resolves the conflicts. The other method,\n**OpenWithManualConflictResolution** accepts a callback method to allow the\nmanual resolution of the conflict.\n\nSee [ISavedGameClient](/games/services/unity/v2/api/interface/google-play-games/basic-api/saved-game/i-saved-game-client) for more details\non these methods. \n\n void OpenSavedGame(string filename) {\n ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;\n savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,\n ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);\n }\n\n public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {\n if (status == SavedGameRequestStatus.Success) {\n // handle reading or writing of saved game.\n } else {\n // handle error\n }\n }\n\nWrite a saved game\n------------------\n\nOnce the saved game file is opened, it can be written to save the game state.\nThis is done by calling **CommitUpdate**. There are four parameters to\nCommitUpdate:\n\n1. the saved game metadata passed to the callback passed to one of the Open calls.\n2. the updates to make to the metadata.\n3. the actual byte array of data\n4. a callback to call when the commit is complete.\n\n void SaveGame (ISavedGameMetadata game, byte[] savedData, TimeSpan totalPlaytime) {\n ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;\n\n SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.Builder();\n builder = builder\n .WithUpdatedPlayedTime(totalPlaytime)\n .WithUpdatedDescription(\"Saved game at \" + DateTime.Now());\n if (savedImage != null) {\n // This assumes that savedImage is an instance of Texture2D\n // and that you have already called a function equivalent to\n // getScreenshot() to set savedImage\n // NOTE: see sample definition of getScreenshot() method below\n byte[] pngData = savedImage.EncodeToPNG();\n builder = builder.WithUpdatedPngCoverImage(pngData);\n }\n SavedGameMetadataUpdate updatedMetadata = builder.Build();\n savedGameClient.CommitUpdate(game, updatedMetadata, savedData, OnSavedGameWritten);\n }\n\n public void OnSavedGameWritten (SavedGameRequestStatus status, ISavedGameMetadata game) {\n if (status == SavedGameRequestStatus.Success) {\n // handle reading or writing of saved game.\n } else {\n // handle error\n }\n }\n\n public Texture2D getScreenshot() {\n // Create a 2D texture that is 1024x700 pixels from which the PNG will be\n // extracted\n Texture2D screenShot = new Texture2D(1024, 700);\n\n // Takes the screenshot from top left hand corner of screen and maps to top\n // left hand corner of screenShot texture\n screenShot.ReadPixels(\n new Rect(0, 0, Screen.width, (Screen.width/1024)*700), 0, 0);\n return screenShot;\n }\n\nRead a saved game\n-----------------\n\nOnce the saved game file is opened, it can be read to load the game state. This\nis done by calling **ReadBinaryData**. \n\n void LoadGameData (ISavedGameMetadata game) {\n ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;\n savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);\n }\n\n public void OnSavedGameDataRead (SavedGameRequestStatus status, byte[] data) {\n if (status == SavedGameRequestStatus.Success) {\n // handle processing the byte array data\n } else {\n // handle error\n }\n }\n\nDelete a saved game\n-------------------\n\nOnce the saved game file is opened, it can be deleted. This is done by calling\n**Delete**. \n\n void DeleteGameData (string filename) {\n // Open the file to get the metadata.\n ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;\n savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,\n ConflictResolutionStrategy.UseLongestPlaytime, DeleteSavedGame);\n }\n\n public void DeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {\n if (status == SavedGameRequestStatus.Success) {\n ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;\n savedGameClient.Delete(game);\n } else {\n // handle error\n }\n }"]]