问题描述
我正在我的游戏中实施存档游戏(成就和排行榜已经运行良好)。
这是我的问题: 当我第一次将数据保存到云时,一切正常,我可以成功地从云中读取它。但是,如果我再次将数据写入云端并尝试从云端读取数据,则会得到旧数据。
我想我的问题是ConflictResolutionStrategy
。我实际上不知道这些策略是如何工作的,但是我尝试了许多不同的方法并得到了相同的结果。另外,我不知道 DataSource
是做什么的,但我也试过 DataSource.ReadNetworkOnly
和 DataSource.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,data,data.Length);
SavedGameMetadataUpdate updatedMetadata = new SavedGameMetadataUpdate.Builder()
.WithUpdatedDescription("Saved at " + DateTime.Now.ToString(CultureInfo.InvariantCulture))
.Build();
PlayGamesPlatform.Instance.SavedGame.CommitUpdate(Metadata,updatedMetadata,(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,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,convertedData,convertedData.Length);
//my logic
});
}
所以我有几个问题:
- 我的代码有什么问题?
- 如果我只想将数据写入云端然后读取最后写入的数据,我需要使用什么
ConflictResolutionStrategy
? - Whitch
DataSource
更好用。ReadCacheOrNetwork
还是ReadNetworkOnly
? -
SavedGameMetadataUpdate
会影响保存吗?
我在 github 上的问题:https://github.com/playgameservices/play-games-plugin-for-unity/issues/3051
解决方法
于是我找到了这个bug的解决方案。最终我意识到 byte[] data = new byte[localData.Length * sizeof(int)]; Buffer.BlockCopy(localData,data,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,myFinalBytes,cnt*4,4);
}
return myFinalBytes;
}