在下次发布时保存玩家得分并检索它? Unity3d / C#

时间:2017-09-07 03:20:55

标签: c# unity3d

我正在编写一个脚本,每秒给玩家5分;我希望它在Unity3d中使用PlayerPrefs存储得分。我写了这个剧本,目前的分数是应该的,但使用PlayerPrefs的总分似乎不起作用。

这是我的剧本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreDataManager : MonoBehaviour {

    public Text txtScore;
    public static int score;
    public Text txtHighscore;
    public static int highscore;
    int newHighscore;

    int nameSize = 15;
    private float timer;

    void Start (){

        score = PlayerPrefs.GetInt("highscore");
    }
    void Update (){

        timer += Time.deltaTime;

        if (timer > 5f) {
            score += 5;

            timer = 0;
        }

        txtHighscore.text = string.Format("TOTAL POINTS: <size=" + nameSize + "><color=#00ffd8>{0}</color></size>", PlayerPrefs.GetInt("highscore"));
        txtScore.text = string.Format("CURRENT POINTS: <size=" + nameSize + "><color=#00ffd8>{0}</color></size>", score.ToString());

        StoreHighscore (newHighscore);
    }
    void StoreHighscore (int newHighscore)
    {
        int oldHighscore = PlayerPrefs.GetInt ("highscore", 0);
        if (newHighscore > oldHighscore) 
        {
            PlayerPrefs.SetInt ("highscore", newHighscore);
            txtHighscore = txtScore;
        }
        PlayerPrefs.Save ();

        Debug.Log ("Current points: " + score.ToString ());
        Debug.Log("Total points: " + PlayerPrefs.GetInt("highscore"));
    }
}

任何帮助将不胜感激!

0 个答案:

没有答案