我读了很多文章,但我似乎没有得到它。我现在有这个代码工作,因为我硬编码3秒延迟,所以有足够的时间进行网络通话,所以当显示分数时有数据。但我真正想要的是让网络通话结束,然后显示分数。救命 ?
IEnumerator Start()
{
client = new MobileServiceClient(_appUrl, _appKey);
table = client.GetTable<Highscore>("Highscores");
yield return StartCoroutine(ReadItems());
DisplayScores();
}
void Update()
{
}
public void btn_GoBack()
{
Application.LoadLevel("StartScene");
}
private void OnReadItemsCompleted(IRestResponse<List<Highscore>> response)
{
if (response.StatusCode == HttpStatusCode.OK)
{
Debug.Log("OnReadItemsCompleted data: " + response.Content);
List<Highscore> items = response.Data;
Debug.Log("Read items count: " + items.Count);
Scores = items;
}
else
{
Debug.Log("Error Status:" + response.StatusCode + " Uri: " + response.ResponseUri);
}
}
private IEnumerator ReadItems()
{
table.Read<Highscore>(OnReadItemsCompleted);
yield return new WaitForSeconds(3);
}
private void DisplayScores()
{
txtHighScores.text = "";
int numberOfScores = Math.Min(Scores.Count, 5);
for (int i = 0; i < numberOfScores; i++)
{
string name = Scores[i].username.ToString();
string score = Scores[i].score.ToString();
txtHighScores.text += (i + 1).ToString() + ". " +
" - " + name + "\r\n" +
score.ToString().PadLeft(4, '0');
}
}
答案 0 :(得分:2)
使用Startcorutine方法传递参数很重要, 否则会出错。 所以在你的代码中尝试我的修复。
IEnumerator Start()
{
client = new MobileServiceClient(_appUrl, _appKey);
table = client.GetTable<Highscore>("Highscores");
yield return StartCoroutine(ReadItems(2.0f)); //delay
DisplayScores();
}
IEnumerator ReadItems(float delay)
{
yield return new WaitForSeconds(delay);
table.Read<Highscore>(OnReadItemsCompleted);
}
答案 1 :(得分:1)
DisplayScores()
&#34;的方法调用来自Start()
DisplayScores()
作为方法回调的最后一行OnReadItemsCompleted
yield return new WaitForSeconds(3);