我在游戏中使用高分列表刷新/重新加载弹出窗口时出现问题。所有在Unity 5模拟器中工作正常但在Windows Phone设备上没有。
问题:当我玩完游戏后,它会在云端上传分数。然后当我进入高分菜单(游戏在这里下载高分)时,Windows Phone上没有最新的高分列表(在统一模拟器中,所有它都是正确的)。有趣的是,当我关闭游戏并再次运行并进入高分菜单时,高分列表是最新的。游戏得分正确上传,我在服务器网站上查了一下。
有什么方法可以解决这个问题吗?
编辑: 我使用dreamlo.com存储分数。 通过这种方式,我可以上传/下载梦想中的分数:
using UnityEngine;
using System.Collections;
public class Highscores : MonoBehaviour {
const string privateCode = "myprivatecode (random characters)";
const string publicCode = "mypubliccode(random characters)";
const string webURL = "http://dreamlo.com/lb/";
DisplayHighscores highscoresDisplay;
public Highscore[] highscoresList;
static Highscores instance;
void Awake(){
highscoresDisplay = GetComponent<DisplayHighscores> ();
instance = this;
}
public static void AddNewHighscore(string username, int score){
instance.StartCoroutine(instance.UploadNewHighscore(username,score));
}
IEnumerator UploadNewHighscore(string username, int score)
{
WWW www = new WWW (webURL + privateCode + "/add/" + WWW.EscapeURL (username) + "/" + score);
yield return www;
if (string.IsNullOrEmpty (www.error)){
print ("Uploaded Successful");
DownloadHighscores();
}
else {
print ("Error uploading: " + www.error);
}
}
public void DownloadHighscores(){
StartCoroutine ("DownloadHighscoreFromDatabase");
}
IEnumerator DownloadHighscoreFromDatabase()
{
WWW www = new WWW (webURL + publicCode + "/pipe/");
yield return www;
if (string.IsNullOrEmpty (www.error)) {
FormatHighscores (www.text);
highscoresDisplay.OnHighscoresDownloaded(highscoresList);
}
else {
print ("Error downloading: " + www.error);
}
}
void FormatHighscores(string textStream){
string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoresList = new Highscore[entries.Length];
for (int i = 0; i <entries.Length; i ++) {
string[] entryInfo = entries[i].Split (new char[] {'|'});
string username = entryInfo[0];
int score = int.Parse(entryInfo[1]);
highscoresList[i] = new Highscore(username, score);
print(highscoresList[i].username + ": " + highscoresList[i].score);
}
}
}
public struct Highscore{
public string username;
public int score;
public Highscore(string _username, int _score){
username = _username;
score = _score;
}
}
高分显示该课程:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DisplayHighscores : MonoBehaviour {
public Text[] highscoreText;
Highscores highscoreManager;
void Start () {
for (int i = 0; i<highscoreText.Length; i++) {
highscoreText[i].text = i+1 + ". Fetching...";
}
highscoreManager = GetComponent<Highscores>();
StartCoroutine ("RefreshHighscores");
}
public void OnHighscoresDownloaded(Highscore[] highscoreList){
for (int i = 0; i < highscoreText.Length; i++) {
highscoreText[i].text = i+1 + ". ";
if(highscoreList.Length > i){
highscoreText[i].text += highscoreList[i].username + " - " + highscoreList[i].score;
}
}
}
IEnumerator RefreshHighscores(){
while (true) {
highscoreManager.DownloadHighscores();
yield return new WaitForSeconds(30);
}
}
}
答案 0 :(得分:0)
如果您使用WWW类从服务器检索分数,它可能会将结果存储在缓存中。
我在Windows Phone上遇到了同样的问题,我的解决方法是使用GET在您的网址中使用随机数。
以下是一个例子:
string get_url = "http:yoururl.com/?rnd=" + Random.value;
WWW hs_get = new WWW(get_url);
yield return hs_get;
这种方式不应该存储在你的缓存中。