保存的游戏 dom 无法正常工作。 ConflictResolutionStrategy、DataSource 或 SavedGameMetadataUpdate 的问题

Saved Games dom't work right. Problem with ConflictResolutionStrategy, DataSource or SavedGameMetadataUpdate

我正在我的游戏中实现保存的游戏(成就和排行榜已经运行良好)。

这是我的问题: 当我第一次将数据保存到云端时,一切正常,我可以成功地从云端读取数据。但是如果我再次将数据写入云端并尝试从云端读取数据,我会得到我的旧数据。

我想我的问题是 ConflictResolutionStrategy。我实际上并不知道这些策略是如何工作的,但我尝试了很多不同的方法并得到了相同的结果。另外,我不知道 DataSource 做了什么,但我也尝试了 DataSource.ReadNetworkOnlyDataSource.ReadCacheOrNetwork,但它没有帮助我。

我的代码:

            private static bool isProcessing;
            private const string dataName = "bests";

            public static void WriteDataToCloud()
            {
                OpenConnection(dataName, WriteOnConnectionOpen);
            }

            private static void ReadDataFromCloud()
            {
                OpenConnection(dataName, ReadOnConnectionOpen);
            }

            private static void OpenConnection(string name,
                Action<SavedGameRequestStatus, ISavedGameMetadata> onConnectionOpen)
            {
                if (!Social.localUser.authenticated) return;
                isProcessing = true;
                Debug.Log("Opening connection");
                PlayGamesPlatform.Instance.SavedGame.OpenWithAutomaticConflictResolution(name,
                    DataSource.ReadCacheOrNetwork,
                    ConflictResolutionStrategy.UseMostRecentlySaved, onConnectionOpen);
            }

            private static void WriteOnConnectionOpen(SavedGameRequestStatus status, ISavedGameMetadata metadata)
            {
                if (status != SavedGameRequestStatus.Success) return;
                Debug.Log("Connection opened to write");
                int[] localData = //my data
                Debug.Log(localData.Aggregate("", (current, i) => current + (i + " ")));
                byte[] data = new byte[localData.Length * sizeof(int)];
                Buffer.BlockCopy(localData, 0, data, 0, data.Length);
                SavedGameMetadataUpdate updatedMetadata = new SavedGameMetadataUpdate.Builder()
                    .WithUpdatedDescription("Saved at " + DateTime.Now.ToString(CultureInfo.InvariantCulture))
                    .Build();

                PlayGamesPlatform.Instance.SavedGame.CommitUpdate(metadata, updatedMetadata, data,
                    (savedGameRequestStatus, savedGameMetadata) =>
                    {
                        isProcessing = false;
                        if (savedGameRequestStatus == SavedGameRequestStatus.Success)
                            Debug.Log("Written to cloud");
                    });
            }

            private static void ReadOnConnectionOpen(SavedGameRequestStatus status, ISavedGameMetadata metadata)
            {
                PlayGamesPlatform.Instance.SavedGame.ReadBinaryData(metadata,
                    (savedGameRequestStatus, data) =>
                    {
                        isProcessing = false;
                        if (status != SavedGameRequestStatus.Success) return;
                        Debug.Log("Connection opened to read");
                        PlayerPrefs.SetInt("DataWasReadFromCloud", 1);
                        int[] convertedData = new int[data.Length / sizeof(int)];
                        Buffer.BlockCopy(data, 0, convertedData, 0, convertedData.Length);
                        //my logic
                    });
            }

所以我有几个问题:

我关于 github 的问题:https://github.com/playgameservices/play-games-plugin-for-unity/issues/3051

所以我找到了这个错误的解决方案。最终我意识到 byte[] data = new byte[localData.Length * sizeof(int)]; Buffer.BlockCopy(localData, 0, data, 0, data.Length); 不能正确地从 int[] 转换为 byte[] 以及从 byte[] 转换为 int[]。我在互联网上找到了这些方法并将它们添加到我的游戏中并且一切正常:

private static int[] ConvertByteArrayToIntArray(byte[] inputElements)
{
    int[] myFinalIntegerArray = new int[inputElements.Length / 4];
    for (int cnt = 0; cnt < inputElements.Length; cnt += 4)
    {
        myFinalIntegerArray[cnt / 4] = BitConverter.ToInt32(inputElements, cnt);
    }
    return myFinalIntegerArray;
}

public static byte[] ConvertIntArrayToByteArray(int[] inputElements)
{
    byte[] myFinalBytes = new byte[inputElements.Length * 4];
    for(int cnt = 0 ; cnt<inputElements.Length; cnt ++)
    {
        byte[] myBytes = BitConverter.GetBytes(inputElements[cnt]);
        Array.Copy(myBytes, 0, myFinalBytes, cnt*4, 4);
    }
    return myFinalBytes;
}