这是书本中第四个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 update
void Start()
{
InvokeRepeating("AddBall", 1.5F, 1);
}
public void ClickOnBall()
{
Score++;
numberOfBalls--;
}
// Update is called once per frame
void 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窗口下修改
增加按钮调用
设置好后,将这些绑定
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » C#从零开始学习(用户界面)(unity Lab4)
发表评论 取消回复