unity Rectransform里几个重要成员的几何含义

转自localPosition与anchoredPosition转化

在上一篇中,我们推理出了RectTransform的几个主要成员localPosition, rect, anchor, pivot, offset, anchoredPosition的关系:

 RectTransform rect = GetComponent<RectTransform>();
     RectTransform parentRect = transform.parent.GetComponent<RectTransform>();

     Vector2 GetAnchoredPositionByLocalPosition()
    {
        // 通过OffsetMin、OffsetMax,将anchoredPosition和localPosition联系起来
        Vector2 localPosition2D = new Vector2(rect.localPosition.x, rect.localPosition.y);
        Vector2 anchorMinPos = parentRect.rect.min + Vector2.Scale(rect.anchorMin, parentRect.rect.size);
        Vector2 rectMinPos = rect.rect.min + localPosition2D;
        Vector2 offsetMin = rectMinPos - anchorMinPos;

        Vector2 anchorMaxPos = parentRect.rect.max - Vector2.Scale(Vector2.one - rect.anchorMax, parentRect.rect.size);
        Vector2 rectMaxPos = rect.rect.max + localPosition2D;
        Vector2 offsetMax = rectMaxPos - anchorMaxPos;

        Vector2 sizeDelta = offsetMax - offsetMin;

        Vector2 anchoredPosition = offsetMin + Vector2.Scale(sizeDelta, rect.pivot); 

        return anchoredPosition;
    }

在这一篇,就主要想试从这个公式,来了解RectTransform里面的几个变量的几何含义。

不过这里面的几个简单变量,如offsetMax, offsetMin就不介绍了,看下面这篇就好了,主要介绍一下我觉得有错误的sizeDelta,和主要研究对象anchoredPosition。

一、sizeDelta

在公式变形前,我们其实都已经知道了,sizeDelta = offsetMax - offsetMin,几何意义是ui本身大小 减去 锚框 大小。

那么,我们怎么用公式证明出来呢?
为了不挡住原来信息,这里的框做了一些放大缩小调整 下面这些证明过程,为了理解方便,就单取只是各个向量的x,不以二维向量形式计算(当然y轴的证明方式也是一样的)这些操作只是对 点 和 线段长度 来计算,并不是以向量的形式pivot,anchorMin 和 anchorMax 是百分比的存在offsetMin, offsetMax, rect.width, rect.height是长度,不是向量,也不是点rect.min, rect.max 是点

理解了上面几个变量表示的含义之后,操作就简单了。

sizeDelta =           offsetMax           -          offsetMin
          = (rectMaxPos - anchorMaxPos) -(rectMinPos - anchorMinPos)  // (1)代入上面的公式
          = (rectMaxPos - rectMinPos)   - (anchorMaxPos - anchorMinPos) // (2)将相关变量移到一起
          =          rectWidth            - (anchorMaxPos - anchorMinPos) // (3)ui本身最大x坐标-最小x坐标,等于ui自身的大小rect.width
          =          rectWidth            -           anchorRectWidth     // (4)同上,等处锚框长度

// sizeDelta 的x等于ui自身长度-锚框长度,证毕。
// 根据计算出来的结果,如果当4个锚点集合在一块时,anchroRectWidth = 0, 所以此时sizeDelta等于ui自身真实大小
// y轴同理。
// 当然还可以继续往第三步代入公式,可以得到sizeDelta和anchorMin, anchorMax, parentRect之间的关系,有兴趣的可以自己往下写


sizeDelta的计算方法

二、anchoredPosition

了解了sizeDelta的几何含义推导,那么现在了解一下anchoredPosition的几何含义。

第一种理解:

我们知道anchoredPosition = offsetMin + Vector2.Scale(sizeDelta, rect.pivot)
第一步,就是这个,相当于再sizeDelta这个矩形里面,再找一个跟原pivot比例一直的pivot
在这里插入图片描述第二种理解,我们知道,anchoredPosition = pivotPos - anchorPos(当所有锚点都再一个地方时)。所以此时的虚四合一锚点,在相差offsetMin + Vector2.Scale(sizeDelta, rect.pivot)的位置上。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值