【Unity】坐标转换(屏幕坐标、世界坐标、UI坐标)

目录

[一、3D世界坐标转UI锚点坐标(3D pos -> UI anchorPos)](#一、3D世界坐标转UI锚点坐标(3D pos -> UI anchorPos))

[二、任意UI世界坐标转指定UI世界坐标(UI pos -> UI pos)](#二、任意UI世界坐标转指定UI世界坐标(UI pos -> UI pos))

[三、3D世界坐标转指定UI世界坐标(3D pos -> UI pos)](#三、3D世界坐标转指定UI世界坐标(3D pos -> UI pos))

[四、UI世界坐标转指定Z轴的3D世界坐标(UI pos -> 3D pos)](#四、UI世界坐标转指定Z轴的3D世界坐标(UI pos -> 3D pos))


一、3D世界坐标转UI锚点坐标(3D pos -> UI anchorPos)

cs 复制代码
//3D世界坐标 -> rt空间下的UGUI锚点坐标 anchoredPosition
public static Vector2 World2UIPos(Camera uiCamera, Vector3 worldPos, RectTransform rt)
{
    Camera worldCamera = Camera.main; //worldPos世界坐标物体所在的世界摄像机
    Vector2 screenPoint = worldCamera.WorldToScreenPoint(worldPos);
    Vector2 anchoredPosition;
    RectTransformUtility.ScreenPointToLocalPointInRectangle(rt, screenPoint, uiCamera, out anchoredPosition);
    return anchoredPosition;
}

参数1:uiCamera UI摄像机(渲染Canvas的摄像机)

参数2:worldPos 世界坐标

参数3:rt (RectTransform) 转化到rt节点下的UI锚点坐标

用法:rectTransform.anchoredPosition3D = World2UIPos(uiCamera, 3dTransorm.position, rt);

3dTransorm 物体世界坐标转到rt节点下的UI锚点坐标,rectTransform是位于rt节点下的UI物体。
注意:anchoredPosition3D是一个3D锚点坐标,使用它是确保Z值是0

用途:当需要在3D物体处生成一个UI物体时。

二、任意UI世界坐标转指定UI世界坐标(UI pos -> UI pos)

cs 复制代码
//UGUI世界坐标 -> rt空间下的UGUI世界坐标
public static Vector3 UIWorld2UIWorldPos(Camera uiCamera, Vector3 worldPos, RectTransform rt)
{
    Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, worldPos);
    Vector3 uiPos;
    RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, screenPoint, uiCamera, out uiPos);
    return uiPos;
}

参数1:uiCamera UI摄像机(渲染Canvas的摄像机)

参数2:worldPos 任意UI物体世界坐标

参数3:rt (RectTransform) 转化到rt节点下的UI世界坐标

用法:uiB.transform.position = UIWorld2UIWorldPos(uiCamera, uiA.transform.position, rt);

将uiA世界坐标转到rt节点下的UI世界坐标并赋予uiB,uiB是位于rt节点下的UI物体。

用途:当UI物体需要移动到另一个UI物体位置时,且它们的父节点不一样时。(若父节点相同就能直接用position)

三、3D世界坐标转指定UI世界坐标(3D pos -> UI pos)

cs 复制代码
//3D世界坐标 -> UGUI世界坐标
public static Vector3 World2UIWorldPos(Camera uiCamera, Vector3 worldPos, RectTransform rt)
{
    Camera worldCamera = Camera.main; //worldPos世界坐标物体所在的世界摄像机
    Vector2 screenPoint = worldCamera.WorldToScreenPoint(worldPos);
    Vector3 uiPos;
    RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, screenPoint, uiCamera, out uiPos);
    return uiPos;
}

参数1:uiCamera UI摄像机(渲染Canvas的摄像机)

参数2:worldPos 任意3D物体世界坐标(非UI物体)

参数3:rt (RectTransform) 转化到rt节点下的UI世界坐标

用法:uiA.transform.position = World2UIWorldPos(uiCamera, 3dTransorm.position, rt);

3dTransorm 物体世界坐标转到rt节点下的UI世界坐标,uiA是位于rt节点下的UI物体。

用途:当需要在3D物体处生成一个UI物体时。

四、UI世界坐标转指定Z轴的3D世界坐标(UI pos -> 3D pos)

cs 复制代码
//UGUI世界坐标 -> 3D世界坐标
public static Vector3 UIPos2WorldPos(Camera uiCamera, Vector3 uiPos, float z)
{
    Vector3 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, uiPos);
    screenPoint.z = z;
    Vector3 worldPos;
    Camera worldCamera = Camera.main;
    worldPos = worldCamera.ScreenToWorldPoint(screenPoint);
    return worldPos;
}

参数1:uiCamera UI摄像机(渲染Canvas的摄像机)

参数2:uiPos任意UI物体世界坐标(非3D物体)

参数3:z 指定转后的世界坐标Z轴

用法:3dGo.transform.position = UIPos2WorldPos(uiCamera, uiA.transform.position, 10);

将uiA世界坐标转到距离摄像机10个单位的世界坐标

用途:3D物体位置与UI物体位置有相对位置关系,UI物体会随着用户不同分辨率而改变位置后,3D物体也要跟随着变化位置时使用,但Z轴依旧保持不变即可。(Z值就是原本3D物体距离摄像机的数值,把3D物体直接放到Camera物体节点下然后看3D物体的Z值是多少就填多少

注意:上面提及的uiCamera (UI摄像机)是Canvas 渲染模式为Screen Space - Camera下时指定的Render Camera摄像机。参数1:uiCamera UI摄像机(渲染Canvas的摄像机)

若没有这个摄像机,填null即可,如果填null有问题,就还是要去设置一个UI摄像机。

相关推荐
YigAin1 小时前
Unity中的Lock,到底在锁什么,什么时候该用?
unity
Var_al1 小时前
抖小Unity WebGL分包命令行工具实践指南
unity·游戏引擎·webgl
天人合一peng3 小时前
unity 通过代码修改button及其名字字体的属性
unity·游戏引擎
GLDbalala7 小时前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎
心疼你的一切10 小时前
Unity异步编程神器:Unitask库深度解析(功能+实战案例+API全指南)
深度学习·unity·c#·游戏引擎·unitask
呆呆敲代码的小Y12 小时前
【Unity 实用工具篇】 | Book Page Curl 快速实现翻书效果
游戏·unity·游戏引擎·u3d·免费游戏·翻书插件
AC梦1 天前
unity中如何将UI上的字高清显示
ui·unity
小贺儿开发1 天前
Unity3D 智慧城市管理平台
数据库·人工智能·unity·智慧城市·数据可视化
June bug2 天前
【领域知识】休闲游戏一次发版全流程:Google Play + Apple App Store
unity
星夜泊客2 天前
C# 基础:为什么类可以在静态方法中创建自己的实例?
开发语言·经验分享·笔记·unity·c#·游戏引擎