参考 https://zhuanlan.zhihu.com/p/119442308

在已经有结果的情况下,先捋一下unity对相关字段的注释就能得出很多公式

(rectMinPos表示左下角在父节点坐标系中的位置,其他以"Pos"结尾的字段同理)

pivot: The normalized position in this RectTransform that it rotates around.
pivot: 旋转中心点的归一化位置(使用自己的宽高归一化)
pivotPos = rectMinPos + rect.size * pivot localPosition: Position of the transform relative to the parent transform
localPosition: 相对于父节点的位置
对于一个RectTransform自己,可以判断localPosition2D和pivotPos就是同一个点
localPosition2D = pivotPos rect.min: The position of the minimum corner of the rectangle.
rect.min: 左下角的位置(相对于谁?应该是中心点pivot坐标)
rect.max: 右上角的位置
rectMinPos = rect.min + pivotPos
rectMaxPos = rect.max + pivotPos anchorMin: The normalized position in the parent RectTransform that the lower left corner is anchored to.
anchorMin: 左下角相对于父节点(左下角)的归一化位置(使用父节点宽高归一化)
anchorMax: 右上角相对于父节点的归一化位置
anchorMin = (anchorMinPos - parentRect.min) / parentRect.size
anchorMax = (anchorMaxPos - parentRect.min) / parentRect.size
anchorMinPos = parentRect.min + anchorMin * parentRect.size
anchorMaxPos = parentRect.min + anchorMax * parentRect.size anchoredPosition: The position of the pivot of this RectTransform relative to the anchor reference point.
anchoredPosition: 中心点相对于"the anchor reference point"的位
anchoredPosition = pivotPos - (anchorMinPos + (anchorMaxPos - anchorMinPos) * pivot) offsetMin: The offset of the lower left corner of the rectangle relative to the lower left anchor
offsetMin: 左下角相对于左下锚点的偏移
offsetMax: 右上角相对于右上锚点的偏移
offsetMin = rectMinPos - anchorMinPos
offsetMax = rectMaxPos - anchorMaxPos sizeDelta: The size of this RectTransform relative to the distances between the anchors.
sizeDelta: 宽高与锚点之间宽高的差值
sizeDelta = rect.size - (anchorMaxPos - anchorMinPos)
= (rectMaxPos - rectMinPos) - (anchorMaxPos - anchorMinPos)
= (rectMaxPos - anchorMaxPos) - (rectMinPos - anchorMinPos)
= offsetMax - offsetMin

1. 从localPosition到anchoredPosition


// 计算自身的anchoredPosition(直接从RectTransform获取即可,这里只演示推导流程,没有实用性)
public static Vector2 GetAnchoredPosition(RectTransform transform)
{
/* 计算推导
anchoredPosition = pivotPos - (anchorMinPos + (anchorMaxPos - anchorMinPos) * pivot)
到这里其实就已经可以算了, 只是步骤多一点, 继续化简以达到参考文章的公式
代入 sizeDelta = rect.size - (anchorMaxPos - anchorMinPos)
anchoredPosition = pivotPos - anchorMinPos - (rect.size - sizeDelta) * pivot
代入 pivotPos = localPosition2D = rectMinPos + rect.size * pivot
anchoredPosition = rectMinPos + rect.size * pivot - anchorMinPos - rect.size * pivot + sizeDelta * pivot
= rectMinPos - anchorMinPos + sizeDelta * pivot
代入 offsetMin = rectMinPos - anchorMinPos
anchoredPosition = offsetMin + sizeDelta * pivot
*/ if (transform == null || transform.Equals(null)) return Vector2.zero;
return transform.offsetMin + sizeDelta * pivot;
} public static Vector2 LocalToAnchoredPosition(RectTransform transform, Vector2 localPosition2D)
{
if (transform == null || transform.Equals(null)) return Vector2.zero;
RectTransform parent = transform.parent?.GetComponent<RectTransform>();
if (parent == null) return Vector2.zero; // 在传入localPosition2D的情况下, 就不能使用刚才化简的结果了, 使用最早一版的公式
Rect parentRect = parent.rect;
Vector2 pivot = transform.pivot;
Vector2 anchorMin = transform.anchorMin;
Vector2 anchorMax = transform.anchorMax;
Vector2 anchorMinPos = parentRect.min + anchorMin * parentRect.size;
Vector2 anchorMaxPos = parentRect.min + anchorMax * parentRect.size; return localPosition2D - (anchorMinPos + (anchorMaxPos - anchorMinPos) * pivot);
}

2. 从anchoredPosition到localPosition

// 刚才的函数反向一下即可
public static Vector2 AnchoredToLocalPosition(RectTransform transform, Vector2 anchoredPosition)
{
if (transform == null || transform.Equals(null)) return Vector2.zero;
RectTransform parent = transform.parent?.GetComponent<RectTransform>();
if (parent == null) return Vector2.zero; Rect parentRect = parent.rect;
Vector2 pivot = transform.pivot;
Vector2 anchorMin = transform.anchorMin;
Vector2 anchorMax = transform.anchorMax;
Vector2 anchorMinPos = parentRect.min + anchorMin * parentRect.size;
Vector2 anchorMaxPos = parentRect.min + anchorMax * parentRect.size; return anchoredPosition + (anchorMinPos + (anchorMaxPos - anchorMinPos) * pivot);
}

Unity anchoredPosition转localPosition的更多相关文章

  1. Unity position和localposition

    1. position是根据世界原点为中心 2. localPosition是根据父节点为中心,如果没有父节点,localpositon和position是没有区别的 3.选中一个物体左上角Globa ...

  2. Unity的UGUI在SetParent后修改UI的localposition问题

    正常情况下,UGUI设置UI的localposition可以直接赋值 UIxxx.rectTransform.localPosition = ] / 2f, , ); 运行后在Unity的Inspec ...

  3. # Unity 游戏框架搭建 2019 (十六、十七) localPosition 简化与Transform 重置

    在上一篇我们收集了一个 屏幕分辨率检测的一个小工具.今天呢再往下接着探索. 问题 我们今天在接着探索.不管是写 UI 还是写 GamePlay,多多少少都需要操作 Transform. 而在笔者刚接触 ...

  4. RectTransform的localPosition与anchoredPosition(3D)的区别

    RectTransform继承自Transform,用于描述矩形的坐标(Position),尺寸(Size),锚点(anchor)和中心点(pivot)等信息,每个2D布局下的元素都会自动生成该组件. ...

  5. unity, surface shader access world position and localposition

    一,surface shader中访问worldposition 在surface shader中访问世界坐标,只需在Input结构体中声明float3 worldPos即可,如下:  struct  ...

  6. Unity 坐标 转换 详解 World世界坐标 Screen屏幕坐标 View视口坐标 GUI坐标 NGUI坐标 localPosition相对父级坐标

    在制作游戏中我们经常会遇到这样一个需求: 在人物模型的上面显示 名字.称号 一类的文字或者图片 如下图 人物模型属于是Camera1   UI Title信息属于NGUI Camera2 如下图 这时 ...

  7. unity, change parent and keep localPosition or worlPosition

    node.parent=othernode等价于node.setParent(othernode,true),是保持世界坐标不变. node.setParent(othernode,false)则可以 ...

  8. Unity ugui屏幕适配与世界坐标到ugui屏幕坐标的转换

    我们知道,如今的移动端设备分辨率五花八门,而开发过程中往往只取一种分辨率作为设计参考,例如采用1920*1080分辨率作为参考分辨率. 选定了一种参考分辨率后,美术设计人员就会固定以这样的分辨率来设计 ...

  9. Unity ugui拖动控件(地图模式与物件模式)

    拖动在游戏中使用频繁,例如将装备拖动到指定的快捷栏,或者大地图中拖动以查看局部信息等. Unity的EventSystems中可以直接继承几个接口来实现拖动功能,如下: namespace Unity ...

  10. Unity ugui Anchor锚点自动适配画布中的相对位置

    本随笔参考了以下博客,在此基础上进行优化和改进: https://blog.csdn.net/qq_39640124/article/details/88284191 ugui中的Anchor预设如下 ...

随机推荐

  1. Java相关小知识_6_15

    实体完整性要求每个表都有唯一标识符,每一个表中的主键字段不能为空或者重复的值. 参照完整性要求关系中不允许引用不存在的实体.设定相应的更新删除插入规则来更新参考表. Java语言使用的是Unicode ...

  2. 在DevExpress的GridView的列中,动态创建列的时候,绑定不同的编辑处理控件

    在使用DevExpress的GridView的时候,我们为了方便,往往使用一些扩展函数,动态创建GridView列的编辑控件对象,然后我们可以灵活的对内容进行编辑或者使用一些弹出的对话框窗体进行处理内 ...

  3. 玩转 PI 系列-如何在 Rockchip Arm 开发板上安装 Docker Tailscale K3s Cilium?

    概述 618 买了几个便宜的 Purple PI OH 开发板 (500 块多一点买了 3 个), 这个开发板类似树莓派,是基于 Rockchip(瑞芯微) 的 rx3566 arm64 芯片.如下: ...

  4. linux基础命令及常用命令总结

    1.ls命令 ls命令是最基础的命令之一,作用是列出当前目录下所有的文件和目录.ls命令有很多选项可以使用,比较常用的是-l选项,可以以详细信息的形式列出所有文件和目录的信息. 示例:列出当前目录下的 ...

  5. 使用 Habana Gaudi2 加速视觉语言模型 BridgeTower

    在对最先进的视觉语言模型 BridgeTower 进行微调时,使用 Optimum Habana v1.6, Habana Gaudi2 可以达到 近 3 倍于 A100 的速度.硬件加速的数据加载以 ...

  6. 利用pytorch准备数据集、构建与训练、保存与加载CNN模型

    本文的主要内容是利用pytorch框架与torchvision工具箱,进行准备数据集.构建CNN网络模型.训练模型.保存和加载自定义模型等工作.本文若有疏漏.需更正.改进的地方,望读者予以指正,如果本 ...

  7. Programming abstractions in C阅读笔记:p91-p106

    <Programming Abstractions In C>学习第45天,p91-p102,完成第二章内容学习.总结如下: 一.技术总结 1.垃圾回收 p91,"Some la ...

  8. 4.4 C++ Boost 数据集序列化库

    Boost 库是一个由C/C++语言的开发者创建并更新维护的开源类库,其提供了许多功能强大的程序库和工具,用于开发高质量.可移植.高效的C应用程序.Boost库可以作为标准C库的后备,通常被称为准标准 ...

  9. 微信小程序上传文件操作示范

    社会实践心得体会格式要求 提交的心得体会应为word文档,且图文并茂,全文段前.段后0,1.5倍行距. 题目:自拟,方正小标宋简体,小二号,加粗,居中. 个人信息:题目下方,宋体,小四号,加粗,居中, ...

  10. ENVI+ERDAS实现Hyperion叶绿素含量反演:经验比值法、一阶微分法

    本文介绍基于ENVI与ERDAS软件,依据Hyperion高光谱遥感影像,采用经验比值法.一阶微分法等,对叶绿素含量等地表参数加以反演的具体操作. 目录 1 前期准备与本文理论部分 1.1 几句闲谈 ...