欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【Unity】Unity 获取边位置

程序员文章站 2022-04-03 12:29:03
...

【Unity】Unity 获取边位置

using UnityEngine;

/// <summary>
/// RectTransform拓展类
/// </summary>
internal static class RectTransformExt
{
    /// <summary>
    /// 返回左边的位置
    /// </summary>
    public static float GetAnchoredPositionLeft( this RectTransform self )
    {
        return self.anchoredPosition.x - self.sizeDelta.x * self.pivot.x * self.localScale.x;
    }

    /// <summary>
    /// 返回右边的位置
    /// </summary>
    public static float GetAnchoredPositionRight( this RectTransform self )
    {
        return self.anchoredPosition.x + self.sizeDelta.x * ( 1 - self.pivot.x ) * self.localScale.x;
    }

    /// <summary>
    /// 返回下边的位置
    /// </summary>
    public static float GetAnchoredPositionBottom( this RectTransform self )
    {
        return self.anchoredPosition.y - self.sizeDelta.y * self.pivot.y * self.localScale.y;
    }

    /// <summary>
    /// 返回上边的位置
    /// </summary>
    public static float GetAnchoredPositionTop( this RectTransform self )
    {
        return self.anchoredPosition.y + self.sizeDelta.y * ( 1 - self.pivot.y ) * self.localScale.y;
    }

    /// <summary>
    /// 返回边缘矩形
    /// </summary>
    public static Rect GetAnchoredEdge( this RectTransform self )
    {
        var rect = new Rect
        {
            xMin = self.GetAnchoredPositionLeft(),
            xMax = self.GetAnchoredPositionRight(),
            yMin = self.GetAnchoredPositionBottom(),
            yMax = self.GetAnchoredPositionTop(),
        };

        return rect;
    }
}

|ू・ω・` )最后附上 -> 个人博客地址