Unity UGUI HUD 怪物血条实现

首先做一个血条,创建一个名为Follow3DObject的脚本添加到血条控件上。

Follow3DObject.cs的代码如下:

using UnityEngine;
using System.Collections;

public class Follow3DObject : MonoBehaviour
{

    public Transform target;
    public Vector3 offset = new Vector3(0, 1, 0);

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (target != null)
        {
            transform.position = Camera.main.WorldToScreenPoint(target.position + offset);
        }
    }
}

将上面的脚本的target设置成对应的怪物,就可以看到血条跟着怪物移动了。

 

再给一个血条排序的脚本,这里是简单的根据Z轴的坐标来对血条进行排序的。实际场景下可能需要根据摄像机看到的怪物的顺序来进行排序,只要替换一下排序算法就行了。

using UnityEngine;
using System.Collections;
using Boo.Lang;

public class SortHUD : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        var list = new List<Transform>();
        foreach (Transform t in transform)
        {
            list.Add(t);
        }

        list.Sort((a, b) =>
        {
            return a.position.z.CompareTo(b.position.z);
        });

        for (int i = 0; i < list.Count; i++)
        {
            list[i].SetSiblingIndex(i);
        }
    }
}

 

posted on 2015-03-20 15:21  veboys  阅读(13006)  评论(0编辑  收藏  举报

导航