当我点击附加了SaveGame()的按钮时,它会给我一个错误,但是当我第二次点击它时,一切正常。
在搜索GitHub时,我找到了这个答案"在你可以调用WriteSaveGame之前,你需要等到读取完成,因为它是异步的。所以一个简单的方法(你应该添加一些错误处理)是将read添加到readBinary()中的回调。"但它对我不起作用。
oid ReadSaveGame (string filename, Action<SavedGameRequestStatus, ISavedGameMetadata> callback) {
if (dailyreward.check ) {
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.OpenWithAutomaticConflictResolution (filename,
DataSource.ReadCacheOrNetwork,
ConflictResolutionStrategy.UseLongestPlaytime,
callback);
}
}
// Write the save game
void WriteSaveGame (ISavedGameMetadata game, byte [] savedData, Action<SavedGameRequestStatus, ISavedGameMetadata> callback) {
// Check if signed in
if (dailyreward.check == true) {
SavedGameMetadataUpdate updatedMetadata = new SavedGameMetadataUpdate.Builder ()
.WithUpdatedPlayedTime (TimeSpan.FromMinutes (game.TotalTimePlayed.Minutes + 1))
.WithUpdatedDescription ("Saved at: " + DateTime.Now)
.Build ();
ISavedGameClient savedGameClient = PlayGamesPlatform.Instance.SavedGame;
savedGameClient.CommitUpdate (game, updatedMetadata, savedData, callback);
}
}
// Save the game
public void SaveGame ()
{
// Check if signed in
if (dailyreward.check == true) {
// Save game callback
Action<SavedGameRequestStatus, ISavedGameMetadata> writeCallback =
(SavedGameRequestStatus status, ISavedGameMetadata game) => {
};
// Read binary callback
Action<SavedGameRequestStatus, byte[]> readBinaryCallback =
(SavedGameRequestStatus status, byte[] data) => {
};
// Read game callback
Action<SavedGameRequestStatus, ISavedGameMetadata> readCallback =
(SavedGameRequestStatus status, ISavedGameMetadata game) => {
// Check if read was successful
if (status == SavedGameRequestStatus.Success) {
currentGame = game;
PlayGamesPlatform.Instance.SavedGame.ReadBinaryData (game, readBinaryCallback);
}
};
// Create new save data
SaveData saveData = new SaveData
{
// saving vartiables here
};
ReadSaveGame ("MySaveGame", readCallback);
WriteSaveGame (currentGame, SaveData.ToBytes (saveData), writeCallback);
}
}
I get this error: NullReferenceException: Object reference not set to an instance of an object CloudSaver.ReadSaveGame (System.String filename, System.Action`2 callback) (at Assets/scripts/CloudSaver.cs:160) CloudSaver.SaveGame () (at Assets/scripts/CloudSaver.cs:303) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()