主要内容:实现点击物体后,销毁物体,记录销毁数量,进度条做出相应改变(如下图)
面板与代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChangeValue : MonoBehaviour { Text text ; int a = 0; Image amount; GameObject cubes; // Start is called before the first frame update void Start() { text = transform.Find("Text").GetComponent<Text>(); text.text = a + ""; amount = transform.Find("Blood/BloodValue").GetComponent<Image>(); amount.fillAmount = 0; cubes = GameObject.Find("GameObject"); } // Update is called once per frame void Update() { if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo)) { Destroy(hitInfo.transform.gameObject); text.text = ++a +""; amount.fillAmount = amount.fillAmount + ((1.0f / cubes.GetComponentsInChildren<Transform>().Length)); } } } }
代码解释:利用射线检测获取物体信息。