首页 > 其他分享 >Unity anchoredPosition转localPosition

Unity anchoredPosition转localPosition

时间:2023-10-26 17:12:29浏览次数:31  
标签:anchorMinPos anchoredPosition Vector2 transform parentRect Unity pivot size loca

参考 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);
}

标签:anchorMinPos,anchoredPosition,Vector2,transform,parentRect,Unity,pivot,size,loca
From: https://www.cnblogs.com/lunoctis/p/17789777.html

相关文章

  • Unity Addressable资源管理方案实战详解
    Unity推出了全新的Addressable的资源管理方案, 全网一夜间觉得不用Addressable感觉自己的资源管理方案会低一个档次,本节我们将详细的分析Addressable资源管理系统。本节主要从以下3个点来进行分析:(1) Addressable的本质是什么?AssetsBundle是否过时了?(2) Assetsbundle使用实......
  • 在使用 Unity 2022 打包安卓项目时,遇到 gradle 无法访问或下载超级慢最终超时出错的问
    一般表现是打包最后一步会等待超长时间,最后报错,错误信息:PickedupJAVA_TOOL_OPTIONS:-Dfile.encoding=UTF-8FAILURE:Buildfailedwithanexception.*Whatwentwrong:Aproblemoccurredconfiguringrootproject'Gradle'.>Couldnotresolveallartifactsfor......
  • Unity Shader入门
    ShaderLab首先我们创建一个URP工程,然后复制这个地址里的shader。Unity中的shader以ShaderLab的格式编写。下面是上面地址复制的ShaderObject//ShaderLab代码以Shader声明开始。这个路径决定了Material面板中UnityShader的名字和位置。Shader.Find也会使用这个路径Shader"......
  • 在Houdini中创建布料,并导入到Unity中
    在Houdini中创建一个具有物理效果和贴图的布料,导入到Unity中,实现一个效果良好的、可以与模型互动、有贴图的静态布料模型。参考视频:Houdini+Unity2021制作布料全流程!_哔哩哔哩_bilibili1、创建节点首先创建一个obj文件:随后右键这个节点,创建一个DigitalAssret。进入Typ......
  • Unity DOTS系列之BlobAsset核心机制分析
     最近DOTS发布了正式的版本,我们来分享一下DOTS里面BlobAsset机制,方便大家上手学习掌握UnityDOTS开发。BlobAsset概叙DOTS提供了BlobAsset机制来把数据生成高效的二进制数据。BlobAsset的数据是不可变的。BlobAsset只支持非托管类型数据。支持Burst编译器编译出来的类型。同......
  • Unity游戏排行榜的制作与优化
    游戏排行榜是一个很重要的功能,在弱联网的单机游戏与网络游戏中排行榜都是非常重要的,今天我们来详细的讲解游戏排行榜的制作方案,主要有4个点:  游戏排行榜排序核心算法的实现 排序在游戏开发中是一种十分重要的算法,特别是对于海量的数据,高效的排序算法,是核心与关键,排行......
  • Unity DOTS系列之Filter Baking Output与Prefab In Baking核心分析
     最近DOTS发布了正式的版本,我们来分享一下DOTS里面Baking核心机制,方便大家上手学习掌握UnityDOTS开发。今天给大家分享的Baking机制中的FilterBakingOutput与PrefabInBaking。FilterBakingOutput机制在默认情况下,Baking会为每个GameObject生成的Entity与Component,......
  • Unity 3D定点数物理引擎实战系列
    1.1 BEPUphysicsint3D定点数物理引擎介绍 帧同步的游戏中如果用物理引擎,为了保证不同设备上的结果一致,需要采用定点数来计算迭代游戏过程中的物理运算。也就是我们通常说的定点数物理引擎(确定性物理引擎)。本系列教程给大家详细的讲解如何在你的项目中内置一个确定性物理引......
  • Unity 3D定点数物理引擎实战系列2
    1.2 BEPUphysicsint定点数3D物理引擎使用上一节給大家介绍了BEPUphysicsint的一些基本的情况,这节课我们来介绍它的基本使用,本节主要从以下5个方面来介绍:(1) 创建一个物理世界Space,并开启模拟迭代;(2) 添加一个物理物体Entity到世界;(3) 物理引擎的基本处理与操作使用;(......
  • Unity ILRuntime 实战教程系列
    1.1 Unity 搭建ILRuntime开发环境 Unity热更新目前主流的方案有; Lua, ILRuntime, puerts, huatuo方案。前两个大家都比较熟悉了,puerts是基于TypeScript开发的热更新,huatuo是基于C#的方案。后两个大家会比较陌生。本系列分享基于ILRuntime来做热更新。 ILRuntime热更......