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);
        }
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM