u3d 制作ugui 血条

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

public class TestCHpBar : MonoBehaviour {


    private RectTransform rectTrans;

    public Transform target;
    public Vector3 offsetPos;


    void Start () {

        rectTrans = GetComponent();
    }

    // Update is called once per frame
    void Update () {
        if (target == null) return;

        var col = target.GetComponent();
        var topAhcor = new Vector3(col.bounds.center.x, col.bounds.max.y, col.bounds.center.z);
  
        Vector3 tarPos = topAhcor;

        var viewPos = Camera.main.WorldToViewportPoint(tarPos); 

        Vector2 screenPos;

        if (viewPos.z > 0f && viewPos.x > 0f && viewPos.x < 1f && viewPos.y > 0f && viewPos.y < 1f)
        {

            //获取屏幕坐标
            screenPos = Camera.main.WorldToScreenPoint(tarPos + offsetPos); //加上头顶偏移量
        }

你可能感兴趣的:(u3d)