实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),所以:
1、先将游戏场景中的世界坐标通过游戏场景Camera转化为屏幕坐标PosA(Camera.main.WorldToScreenPoint(point))
2、再通过UICamera将该屏幕坐标PosA转换为UI本地坐标PosB(RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPoint, uiCamera, out localPoint))
UGUI之所以存在UICamera是因为UGUI的Canvas的RenderMode有三种模式,一般默认为Overlay(全覆盖),另一种为Camera。
当RenderMode为Overlay时,UI坐标系和屏幕坐标系是一样的,则不需要通过UICamera来转换,直接将第一步得到的屏幕坐标PosA赋值给Hud的localPosition就可以了。
一般游戏中要实现3D物体在在UI之上时,就不能用画布全覆盖模式,再创建一个Camera来单独渲染UI,RenderMode为Camera,Clear Flags为Depth Only,利用一二步骤获得UI坐标PosB
随便找个脚本挂着
using UnityEngine;
class HudTest: MonoBehaviour
{
public RectTransform hud; //Hud
public RectTransform uiParent; //Hud所在的ui节点
public Transform followTarget; //跟随的3D物体
public Camera sceneCamera; //场景摄像机
public Camera uiCamera; //ui摄像机
public float distance = 10; //默认距离相机的距离,自己调
void Update()
{
if (followTarget == null) return;
Vector3 parentPos_3D = followTarget.position + new Vector3(0, 1, 0);
World2UI(parentPos_3D, uiParent, hud);
float newDistance = DistCameraByPos(followTarget.position);
hud.localScale = Vector3.one * distance / newDistance;
}
//世界坐标转成UI中父节点的坐标, 并赋值位置
public void World2UI(Vector3 cusWorldPos, RectTransform cusUiParent, RectTransform cusUiTarget)
{
//世界坐标转化为屏幕坐标
Vector3 _screenPos = sceneCamera.WorldToScreenPoint(cusWorldPos);
//只取屏幕坐标xy轴
Vector2 _tempV2 = new Vector2(_screenPos.x, _screenPos.y);
Vector2 _retPos;
//屏幕坐标屏幕坐标xy轴转化为UI坐标
RectTransformUtility.ScreenPointToLocalPointInRectangle(cusUiParent, _tempV2, uiCamera, out _retPos);
//赋值坐标
cusUiTarget.anchoredPosition = _retPos;
}
//跟摄像机的距离
public float DistCameraByPos(Vector3 pos)
{
return Vector3.Distance(pos, sceneCamera.transform.position);
}
}