【发布时间】:2019-05-29 04:39:14
【问题描述】:
首先 - 我是 Unity 的新手。
我正在尝试从我的 firebase 数据库中检索一些数据,将数据存储在一个数组/字典列表中,然后使用该数组/列表来显示来自我的服务器的数据。
所以...我的尝试方式:
1:创建字典数组来保存我的数据:
[System.Serializable]
public class Global
{
public static Dictionary<string, object>[] offers;
}
2:处理来自数据库的数据,并将其存储在数组中:
void Handle_ChildAdded(object sender, ChildChangedEventArgs e)
{
if (e.DatabaseError != null)
{
Debug.LogError(e.DatabaseError.Message);
return;
}
// Do something with the data in args.Snapshot
if (e.Snapshot.Value != null)
{
var dict = e.Snapshot.Value as Dictionary<string, object>;
if (dict != null)
{
Debug.Log(dict);
Global.offers = new Dictionary<string, object>[Global.storesCount+1];
Global.offers[Global.storesCount] = dict;
Global.storesCount++;
hasHaded = true;
}
}
}
所以现在我将数据库中的所有快照都保存在 Global.offers 数组中。我应该得到的快照效果如下所示:
是时候显示我的数组中的数据了
到目前为止一切正常 - 因为现在我需要显示我刚刚存储在 Global.offers 数组中的数据。 我尝试通过循环来做到这一点。我遍历数组并从我的数据库中搜索键并在游戏对象的预制件中实例化数据像这样:
for (int i = 0; i < Global.storesCount; i++)
{
Transform scrollViewObj = Instantiate(prefab, new Vector3(0, (downSize * i) - firstY, 0), Quaternion.identity);
scrollViewObj.transform.SetParent(scrollContent.transform, false);
scrollViewObj.transform.Find("Overskift").gameObject.GetComponent<Text>().text = Global.offers[i]["Store"] as string;
scrollViewObj.transform.Find("Text (1)").gameObject.GetComponent<Text>().text = Global.offers[i]["Headline"] as string;
scrollViewObj.transform.Find("Text (2)").gameObject.GetComponent<Text>().text = "Din pris: " + Global.offers[i]["Price"] as string + " kr.";
scrollViewObj.transform.Find("Text (3)").gameObject.GetComponent<Text>().text = "Spar: " + Global.offers[i]["AndresPris"] as string + " kr.";
}
这就是我遇到麻烦的地方。出于某种原因,Global.offers[i]["Store"] as string == null,当然意味着我无法实例化该对象。我收到此错误:
NullReferenceException:对象引用未设置为对象的实例 LoadOffers.Start () (在 Assets/Scripts/LoadOffers.cs:36)
这太奇怪了,因为当我尝试调试时,我得到了一些相互矛盾的结果:
数组的长度是19,所以不为空。
当我尝试 Debug.Log 数组时,我得到:
System.Collections.Generic.Dictionary`2[System.String,System.Object][]
但是当我使用以下键搜索值时:
Debug.Log(Global.offers[0]["Store"]);
我得到的都是空的。我在价值观之后搜索错误吗?或者其他人可以看到我做错了什么吗?
【问题讨论】:
标签: c# firebase unity3d firebase-realtime-database