高分列表在 Windows Phone 没有刷新
High score list doesn't refresh on Windows Phone
我的游戏中 refreshing/reloading 高分列表弹出窗口有问题。在 unity 5 模拟器中一切正常,但在 Windows Phone 设备上不正常。
问题:当我玩完游戏后,它会在云端上传分数。然后,当我进入高分菜单(游戏在此处下载高分)时,Windows Phone 上没有最新的高分列表(在统一模拟器中,一切都是正确的)。有趣的是,当我关闭游戏并再次 运行 并转到高分菜单时,高分列表是最新的。游戏分数上传正确,我在服务器网站上查看了。
有什么办法可以解决这个问题吗?
编辑:
我正在使用 dreamlo.com 来存储分数。
这样我 upload/download 得分 to/from dreamloo:
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;
}
}
高分显示class:
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);
}
}
}
如果您使用 WWW class 从服务器检索您的分数,它可能会将结果存储在缓存中。
我在 Windows Phone 上遇到了同样的问题,我的解决方案是使用 GET 在 URL 中使用随机数。
这是一个例子:
string get_url = "http:yoururl.com/?rnd=" + Random.value;
WWW hs_get = new WWW(get_url);
yield return hs_get;
这样它就不会存储在您的缓存中。
我的游戏中 refreshing/reloading 高分列表弹出窗口有问题。在 unity 5 模拟器中一切正常,但在 Windows Phone 设备上不正常。
问题:当我玩完游戏后,它会在云端上传分数。然后,当我进入高分菜单(游戏在此处下载高分)时,Windows Phone 上没有最新的高分列表(在统一模拟器中,一切都是正确的)。有趣的是,当我关闭游戏并再次 运行 并转到高分菜单时,高分列表是最新的。游戏分数上传正确,我在服务器网站上查看了。
有什么办法可以解决这个问题吗?
编辑: 我正在使用 dreamlo.com 来存储分数。 这样我 upload/download 得分 to/from dreamloo:
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;
}
}
高分显示class:
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);
}
}
}
如果您使用 WWW class 从服务器检索您的分数,它可能会将结果存储在缓存中。
我在 Windows Phone 上遇到了同样的问题,我的解决方案是使用 GET 在 URL 中使用随机数。
这是一个例子:
string get_url = "http:yoururl.com/?rnd=" + Random.value;
WWW hs_get = new WWW(get_url);
yield return hs_get;
这样它就不会存储在您的缓存中。