FirebaseのRealTimeDataBaseからデータを取得して
それをPrefabに反映させるアプリを作っています。
FirebaseのRealTimeDataBaseのドキュメントにある通り
While文でデータを取得するところまでは出来ました。
DataSnapshot snapShot = task.Result; IEnumerator<DataSnapshot> en = snapShot.Children.GetEnumerator(); string json = snapShot.GetRawJsonValue(); int i = 1; while(en.MoveNext()){ DataSnapshot data = en.Current; string address = (string)data.Child("address").Value; string name = (string)data.Child("name").Value; int total = (int)(long)data.Child("total").Value; Debug.Log("第" + i + "位: address:" + address + " name:" + name + " total:" + total); i++; } //結果(コンソール出力) //第1位: address:東京 name:山田太郎 total: 30 //第2位: address:神奈川 name:山田花子 total: 40 //第3位: address:静岡 name:佐藤健 total: 50
while文内のデータをPrefabに参照させたくて i++; 以降prefabを追加しました。
Text[] texts; DataSnapshot snapShot = task.Result; IEnumerator<DataSnapshot> en = snapShot.Children.GetEnumerator(); string json = snapShot.GetRawJsonValue(); int i = 1; while(en.MoveNext()){ DataSnapshot data = en.Current; string address = (string)data.Child("address").Value; string name = (string)data.Child("name").Value; int total = (int)(long)data.Child("total").Value; Debug.Log("第" + i + "位: address:" + address + " name:" + name + " total:" + total); i++; var item = GameObject.Instantiate(prefab) as RectTransform; item.SetParent(transform, false); texts = item.GetComponentsInChildren<Text>(); texts[0].text = "第 " + (i + 1).ToString() + " 位"; texts[1].text = (string)data.Child("name").Value; texts[2].text = (string)data.Child("address").Value; texts[3].text = ((int)data.Child("total").Value).ToString(); }
そうするとまったくPrefabは生成(表示)されません。
なぜかコンソール出力も
//結果(コンソール出力)
//第1位: address:東京 name:山田太郎 total: 30
だけが起きてそれ以降も出力されなくなりました。
ちなみに下記では問題なくPrefabは生成され表示されます。
for(int i = 0; i < 10; i++){ var item = GameObject.Instantiate(prefab) as RectTransform; item.SetParent(transform, false); texts = item.GetComponentsInChildren<Text>(); texts[0].text = "第 " + (i + 1).ToString() + " 位"; texts[1].text = (string)data.Child("name").Value; texts[2].text = (string)data.Child("address").Value; texts[3].text = ((int)data.Child("total").Value).ToString(); }
while文内でPrefabを生成することはできないのでしょうか?
何かヒントでも結構ですので分かる方お教え下さい。
環境)
PC: mac
Unity2018.3.14f1
言語:C#
あなたの回答
tips
プレビュー