积分"/>
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积分
发布评论