Unity上的firebase出现序列化错误



我试图用实时数据库运行一个事务来更改对象,代码如下:

void tryBeHost()
{
string path = "weeks/" + week + "/games/" + gameId + "/gameInfo";
_dbRef.Child(path).RunTransaction(mutableData =>
{
GameInfo gameInfo = mutableData.Value as GameInfo;
if (gameInfo == null)
{
gameInfo = new GameInfo();
}
else if (gameInfo.host != null && gameInfo.host != myId)
{
return TransactionResult.Success(mutableData);
}
gameInfo.host = myId;
mutableData.Value = gameInfo;
return TransactionResult.Success(mutableData);
});
}

所以我得到了一个奇怪的错误:

事务委托中出现异常,中止事务Firebase.Database.Database异常:无法分析对象Serializables.GameInfo--->System.ArgumentException:用于转换为变量的Serializables.GameInfo类型无效在Firebase.Variant.FromObject(System.Object o([0x001df]中,位于Z:\tmp\tmp.EaS8iXpRBh\Firebase\app\client\unity\proxy\Variant.cs:117位于Firebase.Database.Internal.Utilities.MakeVariant(System.Object值([0x0000]中的Z:\tmp\tmp.sZ8vrpcx53\Firebase\Database\client\unity\proxy\Utilities。cs:25---内部异常堆栈跟踪结束---位于Firebase.Database.Internal.Utilities.MakeVariant(System.Object值([0x000d]中的Z:\tmp\tmp.sZ8vrpcx53\Firebase\Database\client\unity\proxy\Utilities。cs:27位于Firebase.Database.MutableData.set_Value(System.Object值([0x0000],位于Z:\tmp\tmp.sZ8vrpcx53\Firebase\Database\client\unity\proxy\MutableData.cs:136在InternetShit.b__10_0(Firebase.Database.MutableData MutableData([0x00045],位于/Users/sandukhan/Unity/projects/Ronda/Assets/Scripts/Internet大便。cs:73位于中的Firebase.Database.Internal.InternalTransactionHandler.DoTransaction(System.Int32回调Id,System.IntPtr可变数据([0x0022]Z: \tmp\tmp.sZ8vrpcx53\firebase\database\client\unity\proxy\InternalTransactionHandler.cs:49UnityEngine.Debug:LogWarning(对象(Firebase.Platform.FirebaseLogger:LogMessage(PlatformLogLevel,String((位于Z:/tmp/tmp.BbQyA8B710/Firebase/app/client/unity/src/unity/FirebaseLogger.cs:92(Firebase.LogUtil:LogMessage(LogLevel,String((位于Z:/tmp/tmp.EaS8iXpRBh/Firebase/app/client/unity/proxy/LogUtil.cs:68(Firebase.Database.Interal.InteralTransactionHandler:DoTransaction(Int32,IntPtr((位于Z:/tmp/tmp.sZ8vrpcx53/Firebase/Database/client/unity/proxy/InternalTransactionHandler.cs:51(Firebase.AppUtilPINVOKE:轮询回调((Firebase.AppUtil:PollCallbacks(((位于Z:/tmp/tmp.EaS8iXpRBh/Firebase/app/client/unity/proxy/AppUtil.cs:32(Firebase.Platform.FirebaseAppUtils:PollCallbacks(((位于Z:/tmp/tmp.EaS8iXpRBh/Firebase/app/client/unity/proxy/FirebaseApp犹他州cs:33(Firebase.Platform.FirebaseHandler:Update(((位于Z:/tmp/tmp.BbQyA8B710/Firebase/app/client/unity/src/unity/FirebaseHandler.cs:205(Firebase.Platform.FirebaseMonoBehavior:Update(((位于Z:/tmp/tmp.BbQyA8B710/Firebase/app/client/unity/src/unity/FirebaseMonoBBehavior.cs:45(

我的GameInfo类如下:


using System;
using Serializables;
namespace Serializables
{
[Serializable]
public class GameInfo
{
public string gameId;
public string host;
public string[] playersIds;
public string[] playersPics;
public string[] playersNames;
}
}

如果有人有解决这个问题的想法,我将感谢

我想你已经了解了基本情况:你只能向Value提交boolstringlongdoubleIDictionaryList<Object>

不过,我想在上面加上一些有趣的注释!我喜欢将Unity的JsonUtilitySetRawJsonValueAsyncGetValueAsync中的GetRawJsonValue结合使用。你的代码看起来有点像这样:

async void SendGameInfo(string path, GameInfo info) {
try {
await _database
.GetReference(path)
.SetRawJsonValueAsync(JsonUtility.ToJson(info));
Debug.Log($"Successfully wrote {info}");
} catch (AggregateException e) {
Debug.LogWarning($"Failed with {e}");
}
}
async Task<GameInfo> ReadGameInfo(string path) {
try {
var dataSnapshot = await _database
.GetReference(path)
.GetValueAsync();
return JsonUtility.FromJson<GameInfo>(info);
} catch (AggregateException e) {
Debug.LogWarning($"Failed with {e}");
return null;
}
}

此外,如果可以的话,在花时间挖掘图书馆后,我喜欢将IDictionary<string, object>视为";基本基元";实时数据库。如果您逐步完成反汇编,则在设置RawJson时,这将用作中间格式。此外,由于Transactions不提供对原始json的访问,这将为您提供一个更统一的RTDB接口(除非您的数据看起来像数组,否则您的基元是List<object>-"看起来像",这意味着您的键是数字,RTDB所知的数字范围约为半满(。

当然,团队会主动监控quickstart github页面,您可以使用新的"特征请求";模板,请求对其中任何一项进行更改(如果有帮助的话(😃.

我认为RTFM总是有效的,我发现mutableData可以接受一些特定的类型。值:bool、string、long、double、IDictionary和List{Object},其中Object是以前列出的类型之一。所以我得到了这个错误,因为我的类GameInfo不被接受,我必须将我的对象转换为IDictionary。来源:https://firebase.google.com/docs/reference/unity/class/firebase/database/mutable-data#class_firebase_1_1_database_1_1_mutable_data_1a4833f23246b3079078332d57c5649254

最新更新