Boss 血条的特点: 1.血条的位置是静态的,不会跟随Boss 改变位置; 2.Boss 的血条一般是“分层”,且有数字标示剩余的血量层数; 3.Boss 的血条也有不分层的,关键在于游戏的策划设定。 最终效果(共3层血): [Boss管理器脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossManager : MonoBehaviour {
private GameObject prefab\_Boss;
private GameObject prefab\_BossHp;
private Transform canvas\_Transform;
void Start () {
prefab\_Boss = Resources.Load<GameObject>("Boss");
prefab\_BossHp = Resources.Load<GameObject>("BossHpPanel");
canvas\_Transform = GameObject.Find("Canvas").GetComponent<Transform>();
}
void Update () {
if(Input.GetKeyDown(KeyCode.M))
{
GameObject.Instantiate<GameObject>(prefab\_Boss);
GameObject.Instantiate<GameObject>(prefab\_BossHp, canvas\_Transform);
}
}
}
Boss血量脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BossHP : MonoBehaviour {
private Transform m\_Transform;
private Image blood\_0;
private Image blood\_1;
private Image blood\_2;
private Text blood\_Count;
private List<Image> bloodLists = new List<Image>();
private int bossHp = 3000; //Boss总血量.
private float bloodHp; //单层血条的血量.
private int currentIndex; //当前血条的索引.
void Start () {
m\_Transform = gameObject.GetComponent<Transform>();
blood\_0 = m\_Transform.Find("Blood\_0").GetComponent<Image>();
blood\_1 = m\_Transform.Find("Blood\_1").GetComponent<Image>();
blood\_2 = m\_Transform.Find("Blood\_2").GetComponent<Image>();
blood\_Count = m\_Transform.Find("BossHP\_BG/Text").GetComponent<Text>();
bloodLists.Add(blood\_0);
bloodLists.Add(blood\_1);
bloodLists.Add(blood\_2);
bloodHp = bossHp / 3;
}
void Update () {
if(Input.GetKeyDown(KeyCode.K))
{
UpdateUI(Random.Range(50, 200));
}
}
private void UpdateUI(int v)
{
//死亡判断.
if(currentIndex == 2 && bloodHp <= v)
{
bloodLists\[currentIndex\].fillAmount = 0;
Debug.Log("Boss已经死亡...");
return;
}
//多层血条伤害表示.
if (bloodHp <= v)
{
bloodLists\[currentIndex\].fillAmount = 0;
float hp = v - bloodHp;
bloodHp = 1000;
bloodHp -= hp;
currentIndex++;
blood\_Count.text = "x" + (3 - currentIndex);
bloodLists\[currentIndex\].fillAmount = bloodHp / 1000;
}
//单层血条伤害表示.
bloodHp -= v;
bloodLists\[currentIndex\].fillAmount = bloodHp / 1000;
}
}