c# - 保存的游戏无法正常工作。ConflictResolutionStrategy、DataSource 或 SavedGameMetadataUpdate 的问题
问题描述
我正在我的游戏中实现已保存的游戏(成就和排行榜已经正常工作)。
这是我的问题:当我第一次将数据保存到云端时,一切都很好,我可以成功地从云端读取它。但是,如果我再次将数据写入云并尝试从云中读取数据,我会得到我的旧数据。
我想我的问题是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, 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
});
}
所以我有几个问题:
- 我的代码有什么问题?
ConflictResolutionStrategy
如果我只想将数据写入云然后读取最后写入的数据,我需要使用什么?- Whit
DataSource
更好用。ReadCacheOrNetwork
还是ReadNetworkOnly
? SavedGameMetadataUpdate
影响保存吗?
我在 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[]
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;
}
推荐阅读
- c# - (NugetPackage) AdvancedDataGridView 排序/过滤列表
- javascript - 为什么console.log($event) 在单击和keyup(角度6)事件后触发仅显示“受信任”属性?
- java - WildFly 13 网络应用程序未显示登录表单
- php - 检查上传的 Microsoft Office 文件中的宏(在 PHP 或其他中)
- google-cloud-datastore - 如何在GQL中按时间显示状态小于2的记录
- python - TensorFlow 变量未正确恢复
- c# - Rebus:是否应该将 IdempotencyData 与 IdempotentSagaData 实例一起持久化?
- c# - 一个模型上的设置器取决于另一个模型的值
- mysql - SQL:按时间范围内的百分比变化查询
- android - com.common.api.ApiException: 8: 与 Google Play 服务的连接丢失