欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UGUI实现血条跟随

程序员文章站 2022-03-26 23:19:59
...

UGUI实现血条跟随

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

public class Flow : MonoBehaviour {
    public Camera m_camera;     // 3d摄像机
    public Transform m_target;  //ui跟随节点
    public RectTransform m_ui;  //ui血条

    private Vector3 position_sp;

    private void Awake()
    {
        if (m_camera == null)
        {
            m_camera = Camera.main;
        }
        if (m_ui == null && transform is RectTransform)
        {
            m_ui = (RectTransform)transform;
        }
    }

    private void LateUpdate()
    {
        position_sp = m_camera.WorldToScreenPoint(m_target.position);
        Format_Position(ref position_sp);
        m_ui.localPosition = position_sp;
    }

    private void Format_Position(ref Vector3 pos)
    {
        pos.x -= Content.screen_width_half;
        pos.y -= Content.screen_height_half;
        pos.x *= Content.screen_width_ratio;
        pos.y *= Content.screen_height_ratio;
    }


}

public class Content
{
    //当前UI分辨率
    public const float UI_Width = 1920f;
    public const float UI_Height = 1080f;


    public static float screen_width_half = Screen.width / 2;
    public static float screen_height_half = Screen.height / 2;


    public static float screen_width_ratio = UI_Width / Screen.width;
    public static float screen_height_ratio = UI_Height / Screen.height;
}
相关标签: Unity