unity开发案例RollABall(9)之设置UI积分

编程入门 行业动态 更新时间:2024-10-24 06:25:29

unity开发案例RollABall(9)之设置UI<a href=https://www.elefans.com/category/jswz/34/1768675.html style=积分"/>

unity开发案例RollABall(9)之设置UI积分

首先,我们需要再玩家脚本中定义一个score,用于存储分数。

设置分数初始值为0,每次触发碰撞事件之后,score++:

 

接着,我们需要创建一个UI界面:

设置好text的位置等等。

 接着,就是监听事件检测了。

监听事件代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class player_move : MonoBehaviour
{public int score = 0;public Text ScoreText;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){float h = Input.GetAxis("Horizontal");float v = Input.GetAxis("Vertical");Rigidbody rd = GetComponent<Rigidbody>();rd.AddForce(new Vector3(h, 0, v) * 13);}private void OnTriggerEnter(Collider other){if (other .tag == "food"){Destroy(other .gameObject);score++;ScoreText.text = "分数:" + score;}}
}

更多推荐

unity开发案例RollABall(9)之设置UI积分

本文发布于:2024-02-26 10:45:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1702218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:积分   案例   unity   UI   RollABall

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!