【Unity】Unity 获取边位置

【Unity】Unity 获取边位置

using UnityEngine;

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

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

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

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

    /// 
    /// 返回边缘矩形
    /// 
    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;
    }
}

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

你可能感兴趣的:(Unity)