UGUI实现血条跟随

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

猜你喜欢

转载自blog.csdn.net/qq_38803654/article/details/96423479