这是书本中第四个unity Lab
在这次实验中,将学习如何搭建一个开始界面
分数系统
点击球,会增加分数
public void ClickOnBall(){Score++;}
在OneBallBehaviour类添加下列方法
void OnMouseDown(){GameController controller = Camera.main.GetComponent<GameController>();controller.ClickOnBall();Destroy(gameObject);}
GameController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class GameController : MonoBehaviour
{public GameObject OneBallPrefab;public int Score = 0;public bool GameOver = true;public int numberOfBalls = 0;public int MaximumBalls = 15;public Text ScoreText;public Button PlayAgainButton;// Start is called before the first frame updatevoid Start(){InvokeRepeating("AddBall", 1.5F, 1);}public void ClickOnBall(){Score++;numberOfBalls--;}// Update is called once per framevoid Update(){ScoreText.text = Score.ToString();}void AddBall(){if (!GameOver){Instantiate(OneBallPrefab);numberOfBalls++;if (numberOfBalls >= MaximumBalls){GameOver = true;PlayAgainButton.gameObject.SetActive(true);}}}public void StartGame(){foreach (GameObject ball in GameObject.FindGameObjectsWithTag("GameController")){Destroy(ball);}PlayAgainButton.gameObject.SetActive(false);Score = 0;numberOfBalls = 0;GameOver = false;}
}
给游戏增加界面
增加界面
在UI中显示分数
点击Hierarchy中的text,在inspector窗口下修改
增加按钮调用
设置好后,将这些绑定