UI主要控制显示分数,以及提醒游戏结束&是否继续游戏
Steps:
1. Create a new Empty GameObject
2. Add a GUIText component
3. Update the GUIText’s transform position to place it in the Screen
4. Update the GUIText, text with “Score Text”
两个概念:
Screen Space :主要是 基于pixels,比如现在的游戏分辨率为 600 *900
Viewport Space: 主要讲的是UI ,0-1 value (0,0) at left bottom (1,1) at right up
使用pixel 可以微调界面文字的位置(use pixel offset to mini-adjust the place)
GameController 中声明分数更新相关函数:
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
在distroyByContact 中,使用单例模式,找到gameControllerObject 的instance,后面使用单例的函数 AddScore
void Start ()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
其中,引用的GameContollerObject 变量中声明为 private