获取并将时间分数显示在记分板(统一游戏)中?

时间:2018-06-17 12:13:53

标签: unity3d

我一直试图设置计时器和分数计数器 - 分数等于经过的时间,时间越短越好。 我有计时器和分数,但是我无法得到它们并将它们显示在得分最高的记分牌中(得分越高的时间越短)。

这是我为乐谱经理编写的代码:

public class ScoreMnager : MonoBehaviour {

    public Text scoreText;
    public Text HighScoreText;

    public float scoreCount;
    public float HighScoreCount;

    public float pointsPerSecond;

    public bool scoreIncreasing;


    // Use this for initialization
    void Start () {
        if(PlayerPrefs.GetFloat("HighScore")!= null){
            HighScoreCount = PlayerPrefs.GetFloat("HighScore");
        }
    }

    // Update is called once per frame
    void Update() {

        if (scoreIncreasing) { 
            scoreCount += pointsPerSecond * Time.deltaTime;
        }
        if(scoreCount>HighScoreCount){
            HighScoreCount = scoreCount;
            PlayerPrefs.SetFloat("HighScore", HighScoreCount);
        }

        scoreText.text = "Timer: " + Mathf.Round(scoreCount);
        HighScoreText.text = "Your Score: " + Mathf.Round(HighScoreCount);

    }
}

这是显示分数的代码,但不起作用:

public bool levelComplete;
public string highscorePos;
public float score;
public int temp;

private int time = 0;
private float startTime;

private bool finished = false;

private ScoreMnager theScoreManager;

void Start()
{
    theScoreManager = FindObjectOfType<ScoreMnager>();
    startTime = Time.time;
}

void OnLevelComplete()
{
    if (ToneButton.CheckForCombination())
    {
        finished = true;
        //levelComplete = true;
        score = PlayerPrefs.GetFloat("HighScore");
        //values from your scoring logic
    }
    for (int i = 1; i <= 5; i++) //for top 5 highscores
    {
        if (PlayerPrefs.GetInt("highscorePos" + i) < score)     
        {
            temp = PlayerPrefs.GetInt("highscorePos" + i);      
            PlayerPrefs.SetFloat("highscorePos" + i, score);     
            if (i < 5)                                        
            {
                int j = i + 1;
                PlayerPrefs.SetInt("highscorePos" + j, temp);
            }
        }
    }
}

void OnGUI()
{
    if (levelComplete)
    {
        for (int i = 1; i <= 5; i++)
        {
            GUI.Box(new Rect(100, 75 * i, 150, 50), "Position " + i + ". " + PlayerPrefs.GetInt("highscorePos" + i));
        }
    }
}

0 个答案:

没有答案