微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Unity 中使用 JSON 作为 Firebase 数据快照检索数据?

如何解决如何在 Unity 中使用 JSON 作为 Firebase 数据快照检索数据?

[Serializable]
    public struct InventoryData
    {
        public int a;
        public BackpackData Backpack;
        public EquipmentData Equipment;
    }

    [Serializable]
    public struct BackpackData
    {
        public int[] Amounts;
        public int[] Items;
    }

    [Serializable]
    public struct EquipmentData
    {
        public int Boots;
        public int gloves;
        public int Helmet;
        public Pocket LeftPocket;
        public int PrimaryWeapon;
        public Pocket RightPocket;
        public int SecondaryWeapon;
        public int Trousers;
        public int Vest;
    }

    [Serializable]
    public struct Pocket
    {
        public int Amount;
        public int Item;
    }

我使用 Firebase,数据返回为 dataSnaphsot。

public void Load(ICanLoadData sender,string path = "")
        {
            DataSnapshot dataSnapshot = null;
            DatabaseReference databaseReference;

            if (path == "")
            {
                databaseReference = firebaseDatabase.RootReference.Child("users")
                    .Child(FirebaseComponent.firebaseUser.UserId);
            }
            else
                databaseReference = firebaseDatabase.RootReference.Child("users")
                    .Child(FirebaseComponent.firebaseUser.UserId).Child(path);

            databaseReference.GetValueAsync().ContinueWith(task =>
            {
                if (task.IsFaulted)
                    sender.OnLoadProcessFaulted();
                if (task.IsCanceled)
                    sender.OnLoadProcessCanceled();
                if (!task.IsCompleted) return;
                dataSnapshot = task.Result;
                if (dataSnapshot.Exists)
                    sender.OnLoadProcessCompleted(dataSnapshot);
                else
                    sender.OnLoadProcessDatanotExists();
            });
        }

方法返回数据快照。该方法工作正常并返回预期数据。

我想从数据库中检索的数据结构如图所示。

public void LoadData(DataSnapshot dataSnapshot)
        {
            try
            {
                inventoryData = JsonUtility.FromJson<InventoryData>(dataSnapshot.ToString());
            }
            catch (Exception e)
            {
                Debug.Log(e.Message);
                throw;
            }
        }

这是我用来通过提供数据快照从 JSON 获取数据的方法。快照返回正确的数据。但是程序在这一行停止。发生错误并指出,json 解析错误:无效值

JSON parse error: Invalid value.
UnityEngine.Debug:Log (object)
ZombieShooter.Component.InventoryComponent:LoadData (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/InventoryComponent.cs:88)
ZombieShooter.Component.DataComponent:OnLoadProcessCompleted (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/DataComponent.cs:223)
ZombieShooter.Component.DatabaseComponent/<>c__displayClass5_0:<Load>b__0 (System.Threading.Tasks.Task`1<Firebase.Database.DataSnapshot>) (at Assets/Game Assets/Game/Components/Database/DatabaseComponent.cs:58)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

如您所见,加载数据方法的结果是正确的。

Result of Data Loading

在我看来,问题与结构类型有关。我使用嵌套数据,这种数据类型是结构。 JSON 可能无法将字符串解析为结构类型。在这种情况下,我应该使用哪种替代方案?

enter image description here

enter image description here

这就是数据在数据库中的样子。

Debug.log(dataSnapshot.ToString() 的结果

DataSnapshot { key = Inventory,value = System.Collections.Generic.Dictionary`2[System.String,System.Object] }
UnityEngine.Debug:Log (object)
ZombieShooter.Component.InventoryComponent:LoadData (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/InventoryComponent.cs:84)
ZombieShooter.Component.DataComponent:OnLoadProcessCompleted (Firebase.Database.DataSnapshot) (at Assets/Game Assets/Game/Components/DataComponent.cs:223)
ZombieShooter.Component.DatabaseComponent/<>c__displayClass5_0:<Load>b__0 (System.Threading.Tasks.Task`1<Firebase.Database.DataSnapshot>) (at Assets/Game Assets/Game/Components/Database/DatabaseComponent.cs:58)
System.Threading._ThreadPoolWaitCallback:PerformWaitCallback ()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。