血条UI操作
渲染模式改为世界空间
导入面板组件并通过画布调整大小
在面板上导入文本组件,显示玩家名称(旧版)
为了使血条填满,Fill Area 数值全部置为0
改变背景颜色和填充物颜色,并设置血条方向
改变数值方便计算
绑定文本组件和滑动条组件
复活
创建一个空物体,加Respawn标签,意为复活点
可以在此处旋转标记,该处标记只有程序员能看见
绑定复活效果特效
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class PlayerHealth : MonoBehaviour
{public Text nameText;//玩家名称public Slider healthSlider;public float maxHp = 100;public float minHp = 0;public float currentHp;//当前的血量public Transform RespawnPoint;//重生点public GameObject respawnVFX;//重生特效void Start(){if (RespawnPoint == null){RespawnPoint = GameObject.FindGameObjectWithTag("Respawn").transform;}if (nameText == null || healthSlider == null||respawnVFX==null){Debug.LogError("请检查文本或滑动条组件或重生特效粒子拽了没");}if (nameText != null){nameText.text = "玩家小组名称";}currentHp = maxHp;//开始满血if (healthSlider != null){healthSlider.maxValue = maxHp;healthSlider.value = currentHp;}}public void TakeDamage(float damage){if (currentHp == 0){return;}if (currentHp > damage){currentHp -= damage;}else{currentHp = 0;}if (currentHp == 0){print("死掉咯");//隐藏玩家gameObject.SetActive(false);//延迟调用//Invoke("PlayerRespawn", 5f);PlayerRespawn();}healthSlider.value = currentHp;}//void ShowPlayer(){gameObject.SetActive(true);}//玩家死亡重生后public void PlayerRespawn(){ currentHp = 100;//同步UI数据healthSlider.value = currentHp;//玩家瞬移到重生点,并实例化重生特效if (RespawnPoint != null){transform.position = RespawnPoint.position;if (respawnVFX != null){//实例化特效粒子GameObject respawnEffect = Instantiate(respawnVFX, transform.position, Quaternion.identity);Invoke("ShowPlayer", 2f);Destroy(respawnEffect,4f);}}}}