UGUI世界坐标转换为UI本地坐标

以下是实现hud跟随3D物体的脚本,只是测试用,不是开发中的代码,脚本挂在任意游戏物体上 demo下载

using UnityEngine;

public class SceneFollowUI : MonoBehaviour
{
public RectTransform hud; //Hud
public RectTransform canvas;//UI的父节点
public Transform parent; //跟随的3D物体
public Camera uiCamera; //UICamera

Vector3 offset; //hud偏移量
Vector3 cachePoint;
float originalDistance;
float factor = 1;
bool visiable = true;

void Start()
{
offset = hud.localPosition - WorldPointToUILocalPoint(parent.position);
cachePoint = parent.position;
originalDistance = GetCameraHudRootDistance();
UpdateVisible();
}

void LateUpdate()
{
if (cachePoint != parent.position)
{
float curDistance = GetCameraHudRootDistance();
factor = originalDistance / curDistance;
UpdatePosition(); //更新Hud位置
UpdateScale(); //更新Hud的大小
UpdateVisible(); //更新Hud是否可见,根据需求设置:factor或者根据和相机距离设置,一定范围内可见,相机视野范围内可见 等等
}
}


private void UpdateVisible()
{

}

private void UpdatePosition()
{
hud.localPosition = WorldPointToUILocalPoint(parent.position) + offset * factor;
cachePoint = parent.position;
}

private void UpdateScale()
{
hud.localScale = Vector3.one * factor;
}

private float GetCameraHudRootDistance()
{
return Vector3.Distance(Camera.main.transform.position, parent.position);
}

private Vector3 WorldPointToUILocalPoint(Vector3 point)
{
Vector3 screenPoint = Camera.main.WorldToScreenPoint(point);
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPoint, uiCamera, out localPoint);
return localPoint;
}
}

转载于:https://www.cnblogs.com/hyhy904/p/11044477.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值