首页 > 解决方案 > 保存的游戏无法正常工作。ConflictResolutionStrategy、DataSource 或 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

标签: c#androidunity3dgoogle-play-services

解决方案


所以我找到了这个错误的解决方案。最终我意识到这byte[] data = new byte[localData.Length * sizeof(int)]; Buffer.BlockCopy(localData, 0, data, 0, data.Length);不适用于从int[]tobyte[]和 from byte[]to转换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;
}

推荐阅读