我已成功使用Unity中的Google Play服务登录,但我现在遇到一个问题,即我有一个包含如下数据的类:
public static class GameData
{
public static Dictionary<string, object> Name_Dic = new Dictionary<string, object>()
{
{"laura",""},
{"steph ",""},
{"Ryan",""},
};
public static Dictionary<string, string> Dialogs_Dic = new Dictionary<string, string>()
{
{"A1", "Hello"},
{"A2", "Nice"},
{"A3", "Test"},
};
public const int nbrTotal_Int = 2;
public const int TestNumber_Int = 5;
}
我必须将这些数据保存到Google Cloud,然后从云端加载它们,但我很困惑我不明白我是否必须将GameManager类中的字典转换为String然后转换为Bytes [] in为了将它们保存在Google Cloud中,或者我可以做些什么。和int变量我应该将它们转换为字符串然后转换为bytes []。我已经搜索到互联网,我无法找到解释或教程,在官方文档或文档中的示例中,我不知道我该怎么办。拜托,请帮帮我吧。非常感谢
您好, 感谢您的回答,我写了一个代码,让我将这些变量保存到Google Cloud,这里是字典和整数序列化的一部分。这是正确的吗?谢谢你的回答。
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Connect : MonoBehaviour
{
public Dictionary Name_Dic = new Dictionary()
{
{"laura",""},
{"steph ",""},
{"Ryan",""},
};
public Dictionary Dialogs_Dic = new Dictionary()
{
{"A1", "Hello"},
{"A2", "Nice"},
{"A3", "Test"},
};
public int nbrTotal_Int = 2;
public int TestNumber_Int = 5;
public void WriteFunction() {
ISavedGameMetadata currentGame = null;
Action<SavedGameRequestStatus, ISavedGameMetadata> writeCallback =
(SavedGameRequestStatus status, ISavedGameMetadata game) => {
Debug.Log(" Saved Game Write: " + status.ToString());
};
// CALLBACK: Handle the result of a binary read
Action<SavedGameRequestStatus, byte[]> readBinaryCallback =
(SavedGameRequestStatus status, byte[] data) =>
{
Debug.Log(" Saved Game Binary Read: " + status.ToString());
if (status == SavedGameRequestStatus.Success)
{
try {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/gameInfo.dat",
FileMode.Open);
GameData data = new GameData();
// Two Dictionary
data.Name_Dic = Name_Dic;
data.Dialogs_Dic = Dialogs_Dic;
// Two Int
data.nbrTotal_Int = nbrTotal_Int;
data.TestNumber_Int = TestNumber_Int;
bf.Serialize(file, data);
file.Close();
} catch (Exception e) {
Debug.Log(" Saved Game Write: convert exception");
}
WriteSavedGame(currentGame, data, writeCallback);
}
};
}
}
[Serializable]
public class GameData
{
public Dictionary Name_Dic = new Dictionary()
{
{"laura",""},
{"steph ",""},
{"Ryan",""},
};
public Dictionary Dialogs_Dic = new Dictionary()
{
{"A1", "Hello"},
{"A2", "Nice"},
{"A3", "Test"},
};
public int nbrTotal_Int = 2;
public int TestNumber_Int = 5;
}``