首页 > 解决方案 > 获取并显示时间分数到记分牌(统一游戏)?

问题描述

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

这是我为分数管理器提供的代码:

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));
        }
    }
}

标签: unity3d

解决方案


推荐阅读