已解决:看起来无法找到当前播放器。在评论下面的代码(第74行)时,它可以工作。无论如何,谢谢你的帮助!
if (!scores.Contains (player))
{
scores.Add (player);
}
所以我按照本指南https://marcelofs.com/blog/how-to-create-a-quick-top10-for-your-unity-game-with-firebase/
这非常简单易懂,我可以确认上传和下载到数据库是有效的。问题是当我尝试访问从Firebase下载并保存在C#列表中的数据时。我不是C#的专家,但我尝试通过制作游戏来学习。而且我通常会找到一个解决方案,但这一个很棘手。
所以我从这段代码中理解的是,它下载所有分数并使用foreach循环将数据保存在列表“分数”中。完成后,它将转到AddScores()并开始对列表进行排序。这就是问题,Unity说“对象引用未设置为对象的实例”。我尝试在没有排序的情况下完成所有操作,并且从列表中获取数据时,foreach循环中出现相同的错误。我知道他们无法从分数列表中获取任何数据。我真的被卡住了。
这是代码(我知道firebase网址错误):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
public class TopScores : MonoBehaviour {
//this is set by UploadPlayerScore and used to compare if the player is in the Top10 returned by the server, or otherwise add it as the 11th result
public static Score player;
public static void SetPlayer(string id, string name, int score, string category)
{
player = new Score();
player.id = id;
player.name = name;
player.score = score;
player.category = category;
player.time = new DateTime();
}
//Have a button point to this to exit
public void Quit()
{
Application.Quit();
}
//you'll need to set this in unity as the parent 'Scores' object shown above
public GameObject scorePanel;
protected List<Score> scores = new List<Score>();
protected void Start()
{
DownloadScores();
}
protected void DownloadScores()
{
UnityHTTP.Request someRequest = new UnityHTTP.Request("get", "https://myfirebase.firebaseio.com/scores.json"/*?orderBy=\"score\"&limitToLast=10"*/);
someRequest.Send((request) => {
Hashtable decoded = (Hashtable)JSON.JsonDecode(request.response.Text);
if (decoded == null)
{
Debug.LogError("server returned null or malformed response ):");
return;
}
foreach (DictionaryEntry json in decoded)
{
Hashtable jsonObj = (Hashtable)json.Value;
Score s = new Score();
s.id = (string)json.Key;
s.name = (string)jsonObj["name"];
s.score = (int)jsonObj["score"];
Debug.Log("Score " + s.score);
s.category = (string)jsonObj["cat"];
//gotta convert it!
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
s.time = dtDateTime.AddMilliseconds((long)jsonObj["time"]).ToLocalTime();
scores.Add(s);
}
AddScores();
});
}
protected void AddScores()
{
//no dupes
if (!scores.Contains(player))
{
scores.Add(player);
}
//firebase usually returns sorted content, but that's not guaranteed by the JSON format
scores.Sort((Score x, Score y) => { return x.score.CompareTo(y.score); });
//we'll be filling top to bottom
scores.Reverse();
int i = 0;
foreach (Score s in scores)
{
if (i > 10)
{
break;
}
Transform panel = scorePanel.transform.Find("Panel " + i);
Debug.Log(panel);
Transform score = panel.Find("score");
Debug.Log(s.score);
score.GetComponentInChildren<Text>().text = s.score + "";
//panel.Find(s.category).gameObject.SetActive(true);
panel.Find("name").GetComponent<Text>().text = s.name;
if (!s.Equals(player))
{
//the player might not come from the server, so 'time' will be null
panel.FindChild("time").GetComponent<Text>().text = s.time.ToString("yyyy-MM-dd");
}
i++;
}
}
public class Score
{
public string id;
public string name;
public int score;
public string category;
public DateTime time;
public override bool Equals(System.Object obj)
{
if (obj == null)
{
return false;
}
Score s = obj as Score;
if ((System.Object)s == null)
{
return false;
}
return id == s.id;
}
}
}
答案 0 :(得分:0)
注意您所关注的教程。
你有
score.GetComponentInChildren<Text>().text = s.score + "";
同时,在教程中,它说
score.GetComponent<Text>().text = s.score + "";
得分 GameObject 应附加文字。
答案 1 :(得分:0)
看起来它无法找到当前的播放器。在评论下面的代码(第74行)时,它可以工作。无论如何,谢谢你的帮助!
if (!scores.Contains (player))
{
scores.Add (player);
}