C#从零开始学习(用户界面)(unity Lab4)

news/2024/10/26 10:35:09/

这是书本中第四个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窗口下修改
在这里插入图片描述
增加按钮调用
在这里插入图片描述

设置好后,将这些绑定
在这里插入图片描述


http://www.ppmy.cn/news/1542087.html

相关文章

提示词高级阶段学习day3.1什么是结构化 Prompt ?

什么是结构化 Prompt &#xff1f; 结构化的思想很普遍&#xff0c;结构化内容也很普遍&#xff0c;我们日常写作的文章&#xff0c;看到的书籍都在使用标题、子标题、段落、句子等语法结构。结构化 Prompt 的思想通俗点来说就是像写文章一样写 Prompt。 为了阅读、表达的方便…

Jmeter实际应用

环境准备 JDK1.8Jmeter 5.6.3 下载地址Jmeter 插件 下载地址 放到lib/ext下 常用命令 # 启动 sh jmeter# 集群模式下启动节点&#xff0c;不启动用不了集群 sh jmeter-server#生成ssl需要的证书, 这里会要求输入个密码&#xff0c;是要在jmeter中用的 keytool -import -ali…

使用 Cursor 和 Devbox 快速开发基于 Rust 的 WASM 智能合约

本教程以一个智能合约&#xff08;使用 NEAR 的一个官方 Fungible Tokens 来实现&#xff09;的例子来介绍一下 Devbox 的强大功能&#xff0c;轻松构建环境&#xff0c;轻松发布。 NEAR 是一个去中心化的应用平台&#xff0c;使用了分片技术的区块链。 免责申明&#xff1a;本…

【C++指南】类和对象(四):类的默认成员函数——全面剖析 : 拷贝构造函数

引言 拷贝构造函数是C中一个重要的特性&#xff0c;它允许一个对象通过另一个已创建好的同类型对象来初始化。 了解拷贝构造函数的概念、作用、特点、规则、默认行为以及如何自定义实现&#xff0c;对于编写健壮和高效的C程序至关重要。 C类和对象系列文章&#xff0c;可点击下…

Go操作Redis

在现代的软件开发中&#xff0c;Redis 已经成为了一个不可或缺的组成部分&#xff0c;它以其卓越的性能和灵活性被广泛应用于缓存、消息队列、会话存储等多种场景。Go 语言&#xff0c;以其并发能力强和运行效率高的特点&#xff0c;成为了后端开发的首选语言之一。当 Go 遇上 …

闯关leetcode——203. Remove Linked List Elements

大纲 题目地址内容 解题代码地址 题目 地址 https://leetcode.com/problems/remove-linked-list-elements/description/ 内容 Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val val, and return the new …

基于GA遗传优化的CNN-GRU-SAM网络时间序列回归预测算法matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 (完整程序运行后无水印) 2.算法运行软件版本 matlab2022a 3.部分核心程序 &#xff08;完整版代码包含详细中文注释和操作步骤视频&#xff09…

webpack解决使用window.open方法打开history路由页面提示404的问题

问题: 一般情况下应该使用history.push(/ssh)打开history路由页面 但项目中使用window.open(/ssh),然后使用new WebSocket进行通信 开发环境下启动项目后,/ssh页面打开却显示cannot get /ssh,控制台提示404 排查问题: 在React开发环境中使用 window.open 打开路由页面时&a…