嗯,我是团结的新人。我遇到了显示高分的问题。每次射击敌人时都会显示得分。我想在每次比赛结束时显示高分,并且每次我获得新的高分时都能够更新。使用GUI文本的得分系统。以下示例。
分:
排行榜:
要显示分数,我正在使用此脚本
using UnityEngine;
public class HealthScript : MonoBehaviour
{
public int hp = 1;
private GUIText scoreReference;
public bool isEnemy = true;
public void Damage(int damageCount)
{
hp -= damageCount;
if (hp <= 0)
{
// Dead!
Destroy(gameObject);
scoreReference.text = (int.Parse(scoreReference.text) + 1).ToString();
}
}
void Start()
{
scoreReference = GameObject.Find("Score").guiText;
}
// . . .
}
我有一些想法来检索得分的值,但它不会显示。请帮帮我..谢谢
答案 0 :(得分:1)
当玩家死亡时,我们会将isDead boolaen设为 true ,当isDead为true时,我们会显示得分
int score;
int highscore;
bool isDead=false;
// initilizing
void OnGUI () {
if(isDead) //make this true when player dies
GUI.Label (new Rect (0,0,100,50),score.ToString());
}
void Awake(){
highscore=PlayerPrefs.GetInt("highscore");
}
public void Damage(int damageCount)
{
hp -= damageCount;
if (hp <= 0)
{
// Dead!
Destroy(gameObject);
score++; //increase score
if(score>highscore)
highscore=score;
}
}
public void onGameEnds(){
PlayerPrefs.SetInt("highscore",highscore);
}
答案 1 :(得分:0)
这将是好的
get { if (_highscore == -1)
_highscore = PlayerPrefs.GetInt("Highscore", 0);
return _highscore;
}
set {
if (value > _highscore) {
_highscore = value;
highscoreReference.text = _highscore.ToString();
PlayerPrefs.SetInt("Highscore", _highscore);
}
}
}