每次将先前的数据插入Unity3d中的数据库

时间:2015-01-20 16:11:03

标签: unity3d

我想在Unity3d游戏结束时保存分数和用户名。我使用PlayerPrefs来获取数据。我从GUI.TextField获取用户名。然后我将数据插入到mysql数据库中。但每次将以前的数据插入数据库。我该如何解决?

PlayerPrefs.SetInt("score",databasescore);
PlayerPrefs.SetString("username",username);
PlayerPrefs.Save();

我使用addscore.php和display.php。我的HighScoreController.cs是:

using UnityEngine;
using System.Collections;

public class HighScoreController : MonoBehaviour
{
    public static string hs;
    public string addScoreURL = "http://localhost:8000/addscore.php?";
    public string highscoreURL = "http://localhost:8000/display.php?";

    void Start()
    {

        StartCoroutine (PostScores (PlayerPrefs.GetString ("username"), PlayerPrefs.GetInt ("score")));
        StartCoroutine (GetScores ());
        PlayerPrefs.DeleteAll();
    }

    public IEnumerator PostScores(string username, int score)
    {
                        string post_url = addScoreURL + "username=" + username + "&score=" + score;
                        WWW hs_post = new WWW (post_url);
                        yield return hs_post; // Wait until the download is done

                        if (hs_post.error != null) {
                                print ("THERE WAS AN ERROR GETTıNG THE HıGH SCORE: \n" + hs_post.error);
                        }               
    }

    public IEnumerator GetScores()
    {
        WWW hs_get = new WWW(highscoreURL);
        yield return hs_get;

        if (hs_get.error != null)
        {
            print("THERE WAS AN ERROR GETTıNG THE HıGH SCORE: \n" + hs_get.error);
        }
        else
        {
            hs = hs_get.text;
            PlayerPrefs.SetString("scores", hs);
            Debug.Log(hs);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我认为存在算法问题。

GetScores

 PlayerPrefs.SetString("scores", hs);

但还没有,

PlayerPrefs.Save();