不同于把血条做好放到模型上方,然后制作Prefab文件,最后从Resources资源处调用。 我们先生成模型到Scene中,然后再生成血条,通过计算血条和模型的位置让血条放到模型身上。 最终的一个效果(Cube是五个模型,Blood是五个血条):[

](http://www.wjgbaby.com/wp-content/uploads/2018/02/18022101-300x213.png)
](http://www.wjgbaby.com/wp-content/uploads/2018/02/18022101-300x213.png)
血条脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BloodBar : MonoBehaviour {

private Transform m\_Transform;
private Transform cube\_Transform;

private int hp = 100;
private bool isLife = true;
private Image m\_Bar;

public bool IsLife {
    get { return isLife; }
}

void Start () {
    m\_Transform = gameObject.GetComponent<Transform>();
    m\_Bar = m\_Transform.Find("Bar").GetComponent<Image>();
}

void Update () {
    Follow();
}

private void Follow() {
    //3D坐标转换为2D坐标,WorldToScreenPoint:世界位置转换为屏幕位置
    Vector2 cubeV2Pos = RectTransformUtility.WorldToScreenPoint(Camera.main, cube\_Transform.position);
    m\_Transform.position = cubeV2Pos + new Vector2(0, 100);
}

public void SetPlayer(Transform PlayerTransform) {
    cube\_Transform = PlayerTransform;
}

public void Damage(int v) {
    hp -= v;
    if(hp <= 0) {
        isLife = false;
        GameObject.Destroy(gameObject);
        GameObject.Destroy(cube\_Transform.gameObject);
    }
    else {
        m\_Bar.fillAmount = hp / 100.0f;
    }
}

}

模型移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeMove : MonoBehaviour {

private Transform m\_Transform;
private Rigidbody m\_Rigidbody;

void Start () {
    m\_Transform = gameObject.GetComponent<Transform>();
    m\_Rigidbody = gameObject.GetComponent<Rigidbody>();
}

void Update () {
    Move();
}

private void Move() {
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");
    Vector3 dir = new Vector3(h, 0, v);
    m\_Rigidbody.MovePosition(m\_Transform.position + dir \* 0.2f);
}

}

管理器脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MonsterManager : MonoBehaviour {

private GameObject prefab\_Blood;
private GameObject prefab\_Cube;

private Transform m\_Transform;
private Transform bloodParent\_Transform;

private List<BloodBar> bloodBarList;

void Start () {
    prefab\_Blood = Resources.Load<GameObject>("Blood");
    prefab\_Cube = Resources.Load<GameObject>("Cube");

    m\_Transform = gameObject.GetComponent<Transform>();
    bloodParent\_Transform = GameObject.Find("Canvas/BloodParent").GetComponent<Transform>();

    bloodBarList = new List<BloodBar>();

    CreateMonsters();
}

void Update() {
    if(Input.GetKeyDown(KeyCode.Space)) {
        if(bloodBarList.Count > 0) {
            bloodBarList\[0\].Damage(Random.Range(10, 30));

            //移除List列表第一个元素,第二个元素会自动补上去
            if(bloodBarList\[0\].IsLife == false) {
                bloodBarList.Remove(bloodBarList\[0\]);
            }
        }

        else {
            Debug.Log("小怪已经全部死亡...");
        }
    }

}

private void CreateMonsters() {
    for (int i = 0; i < 5; i++) {
        float x = Random.Range(-9.0f, 9.0f);
        float z = Random.Range(-9.0f, 9.0f);
        Vector3 pos = new Vector3(x, 0.5f, z);

        Transform cube = GameObject.Instantiate<GameObject>(prefab\_Cube, m\_Transform).GetComponent<Transform>();
        cube.position = pos;
        BloodBar blood = GameObject.Instantiate<GameObject>(prefab\_Blood, bloodParent\_Transform).GetComponent<BloodBar>();
        blood.SetPlayer(cube);
        bloodBarList.Add(blood);
    }

}

}