这是书本中第四个unity Lab
在这次实验中,将学习如何搭建一个开始界面
分数系统
点击球,会增加分数
publicvoidClickOnBall(){
Score++;}
在OneBallBehaviour类添加下列方法
voidOnMouseDown(){GameController controller = Camera.main.GetComponent<GameController>();
controller.ClickOnBall();Destroy(gameObject);}
GameController
usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.UI;publicclassGameController:MonoBehaviour{publicGameObject OneBallPrefab;publicint Score =0;publicbool GameOver =true;publicint numberOfBalls =0;publicint MaximumBalls =15;publicText ScoreText;publicButton PlayAgainButton;// Start is called before the first frame updatevoidStart(){InvokeRepeating("AddBall",1.5F,1);}publicvoidClickOnBall(){
Score++;
numberOfBalls--;}// Update is called once per framevoidUpdate(){
ScoreText.text = Score.ToString();}voidAddBall(){if(!GameOver){Instantiate(OneBallPrefab);
numberOfBalls++;if(numberOfBalls >= MaximumBalls){
GameOver =true;
PlayAgainButton.gameObject.SetActive(true);}}}publicvoidStartGame(){foreach(GameObject ball in GameObject.FindGameObjectsWithTag("GameController")){Destroy(ball);}
PlayAgainButton.gameObject.SetActive(false);
Score =0;
numberOfBalls =0;
GameOver =false;}}
给游戏增加界面
增加界面
在UI中显示分数
点击Hierarchy中的text,在inspector窗口下修改
增加按钮调用
设置好后,将这些绑定
版权归原作者 Hikigaya Komachi 所有, 如有侵权,请联系我们删除。