Unity NGUI implements blood bar follow

1 Use NGUI to make a blood bar, and make the prepared blood bar into a preset body.

 2. Add the following code to the cube sub-object GameObject.

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

public class genshun : MonoBehaviour {

    public GameObject prefab;//Prefab GameObject hud of blood bar
    ;//Instantiated blood bar
    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);//Convert world coordinates to screen coordinates         pt.z = 0;         Vector3 ff = NGUITools.FindCameraForLayer(hud.layer).ScreenToWorldPoint(pt );//Convert screen coordinates to NGUI coordinates         return ff;     } }





Guess you like

Origin blog.csdn.net/LiPing122335/article/details/123168892