//角色
public GameObject m_Role;
//血条
public RectTransform m_RoleHP;
public Canvas m_Canvas;
//UI相机
public Camera m_UICamera;
//血条偏移量 血条在头上
public Vector3 m_RoleOffset;
// Update is called once per frame
void Update()
{
switch (m_Canvas.renderMode)
{
case RenderMode.ScreenSpaceOverlay:
ScreenSpaceOverlay();
break;
case RenderMode.ScreenSpaceCamera:
ScreenSpaceCamera();
break;
default:
break;
}
}
public void ScreenSpaceOverlay()
{
Vector3 rolePos = Camera.main.WorldToScreenPoint(m_Role.transform.position);
m_RoleHP.position = rolePos + m_RoleOffset;
}
public void ScreenSpaceCamera()
{
Vector2 rolePos = Camera.main.WorldToScreenPoint(m_Role.transform.position);
Vector2 targetPos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(m_Canvas.transform as RectTransform, rolePos, m_UICamera, out targetPos))
{
m_RoleHP.anchoredPosition = targetPos;
}
}
Unity UI跟随3D物体
最新推荐文章于 2024-01-03 11:04:15 发布