unity NGUI实现血条跟随

1用NGUI制作血条,并把制作好的血条做成预设体。

 2.给cube子物体GameObject 挂下面代码。

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

public class genshun : MonoBehaviour {

    public GameObject prefab;//血条的Prefab
    GameObject hud;//实例化出来的血条
    Vector3 pos;

    void Start()
    {
        pos = transform.position;
        hud = GameObject.Instantiate(prefab, pos, Quaternion.identity) as GameObject;
        hud.name = transform.parent.name + "HP";
        

    }

    void Update()
    {
        if (hud)
        {
            hud.transform.position = WorldToUI(transform.position);
        }
    }

    public Vector3 WorldToUI(Vector3 point)
    {
        Vector3 pt = Camera.main.WorldToScreenPoint(point);//将世界坐标转换成屏幕坐标
        pt.z = 0;
        Vector3 ff = NGUITools.FindCameraForLayer(hud.layer).ScreenToWorldPoint(pt);//将屏幕坐标转换成NGUI坐标
        return ff;
    }
}

猜你喜欢

转载自blog.csdn.net/LiPing122335/article/details/123168892