Questo argomento descrive come utilizzare i giochi salvati per i servizi per i giochi di Play nei giochi Unity.
Prima di iniziare
Configura il progetto e il plug-in Google Play Giochi per Unity. Per maggiori dettagli, consulta la guida introduttiva.
Partite salvate attivate. Per maggiori dettagli, consulta la sezione Partite salvate.
Visualizzare la UI delle partite salvate
L'interfaccia utente standard per selezionare o creare una voce di salvataggio viene visualizzata chiamando:
void ShowSelectUI() {
uint maxNumToDisplay = 5;
bool allowCreateNew = false;
bool allowDelete = true;
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.ShowSelectSavedGameUI("Select saved game",
maxNumToDisplay,
allowCreateNew,
allowDelete,
OnSavedGameSelected);
}
public void OnSavedGameSelected (SelectUIStatus status, ISavedGameMetadata game) {
if (status == SelectUIStatus.SavedGameSelected) {
// handle selected game save
} else {
// handle cancel or error
}
}
Aprire una partita salvata
Per leggere o scrivere dati in una partita salvata, quest'ultima deve essere aperta. Poiché lo stato del gioco salvato viene memorizzato nella cache localmente sul dispositivo e salvato nel cloud, è possibile riscontrare conflitti nello stato dei dati salvati. Si verifica un conflitto quando un dispositivo tenta di salvare lo stato nel cloud, ma i dati attualmente nel cloud sono stati scritti da un altro dispositivo. Questi conflitti devono essere risolti all'apertura dei dati di gioco salvati.
Esistono due metodi aperti che gestiscono la risoluzione dei conflitti. Il primo, OpenWithAutomaticConflictResolution, accetta un tipo di strategia di risoluzione standard e risolve automaticamente i conflitti. L'altro metodo, OpenWithManualConflictResolution accetta un metodo di callback per consentire la risoluzione manuale del conflitto.
Per ulteriori dettagli su questi metodi, consulta ISavedGameClient.
void OpenSavedGame(string filename) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, OnSavedGameOpened);
}
public void OnSavedGameOpened(SavedGameRequestStatus status, ISavedGameMetadata game) {
if (status == SavedGameRequestStatus.Success) {
// handle reading or writing of saved game.
} else {
// handle error
}
}
Scrivere un salvataggio
Una volta aperto, il file dei dati salvati può essere scritto per salvare lo stato del gioco. Per farlo, chiama CommitUpdate. Esistono quattro parametri per CommitUpdate:
- i metadati della partita salvata passati al callback passato a una delle chiamate Open.
- gli aggiornamenti da apportare ai metadati.
- l'array di byte effettivo dei dati
- un callback da chiamare al termine del commit.
void SaveGame (ISavedGameMetadata game, byte[] savedData, TimeSpan totalPlaytime) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
SavedGameMetadataUpdate.Builder builder = new SavedGameMetadataUpdate.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 below
byte[] pngData = savedImage.EncodeToPNG();
builder = builder.WithUpdatedPngCoverImage(pngData);
}
SavedGameMetadataUpdate updatedMetadata = builder.Build();
savedGameClient.CommitUpdate(game, updatedMetadata, savedData, OnSavedGameWritten);
}
public void OnSavedGameWritten (SavedGameRequestStatus status, ISavedGameMetadata game) {
if (status == SavedGameRequestStatus.Success) {
// handle reading or writing of saved game.
} else {
// handle error
}
}
public Texture2D getScreenshot() {
// Create a 2D texture that is 1024x700 pixels from which the PNG will be
// extracted
Texture2D screenShot = new Texture2D(1024, 700);
// Takes the screenshot from top left hand corner of screen and maps to top
// left hand corner of screenShot texture
screenShot.ReadPixels(
new Rect(0, 0, Screen.width, (Screen.width/1024)*700), 0, 0);
return screenShot;
}
Leggere una partita salvata
Una volta aperto, il file di salvataggio può essere letto per caricare lo stato del gioco. Questa operazione viene eseguita chiamando ReadBinaryData.
void LoadGameData (ISavedGameMetadata game) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.ReadBinaryData(game, OnSavedGameDataRead);
}
public void OnSavedGameDataRead (SavedGameRequestStatus status, byte[] data) {
if (status == SavedGameRequestStatus.Success) {
// handle processing the byte array data
} else {
// handle error
}
}
Eliminare una partita salvata
Una volta aperto, il file di salvataggio può essere eliminato. Questo viene fatto chiamando Delete.
void DeleteGameData (string filename) {
// Open the file to get the metadata.
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution(filename, DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime, DeleteSavedGame);
}
public void DeleteSavedGame(SavedGameRequestStatus status, ISavedGameMetadata game) {
if (status == SavedGameRequestStatus.Success) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.Delete(game);
} else {
// handle error
}
}