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;
}
推荐阅读
-
Jquery实现侧边栏跟随滚动条固定(兼容IE6)
-
Unity UGUI 血条实现
-
Jquery实现侧边栏跟随滚动条固定(兼容IE6)
-
基于JS实现二维码图片固定在右下角某处并跟随滚动条滚动
-
Unity学习笔记(09):UGUI的Image节点和Sprite精灵图、实现进度条加载动画
-
博客侧边栏模块跟随滚动条滑动固定效果的实现方法(js+jquery等)_javascript技巧
-
博客侧边栏模块跟随滚动条滑动固定效果的实现方法(js+jquery等)_javascript技巧
-
Jquery实现侧边栏跟随滚动条固定(兼容IE6)_jquery
-
Jquery实现侧边栏跟随滚动条固定(兼容IE6)_jquery
-
详解原生JS是实现控制多个滚动条同步跟随滚动