Unity3D 在代码中设置RectTransform的锚点和位置


unity自己出了UGUI之后,方便UI界面的编辑使用,再也不用其他插件了,最近看到了RectTransform的两个方法,使用起来还是蛮方便的,因为在做UI的list的时候,经常要做的是在代码里面生成列表的元素,所以就用到了我下面说的两个方法:

 
 SetInsetAndSizeFromParentEdge
 SetSizeWithCurrentAnchors


第一个是根据边来设置坐标,有四个值:Top、Bottom、Left、Right。

第二个是根据方向设置坐标,有两个值:Horizontal、Vertical。

先来说第一个函数,有三个参数:

第一个是一个Edge类型的值,需要指定以父对象的哪个边为基准(也就是上面四个值之中的一个);

第二个参数,是离指定边的距离;

第三个参数,是本身的宽度或者高度;

这样通过设置上下边和左右边来设置子物体的位置和宽高就可以了。


第二个函数,有两个参数:

第一个是Axis类型的值,需要指定一个方向,水平或者垂直;

第二个参数,是本身的宽高;

我这里写了几个静态函数,可以参考下用法:

/// <summary>
    /// 调整 RectTransform 组件中的 Left、Bottom 属性
    /// </summary>
    /// <param name="rt">引用目标 RectTransform 对象</param>
    /// <param name="left">Left值</param>
    /// <param name="bottom">Bottom值</param>
    public static void LeftBottom(RectTransform rt, float left, float bottom) {
        // float value1 = rt.offsetMax.x + left;
        // float value2 = rt.offsetMax.y + bottom;
 
        // rt.offsetMin = new Vector2(left, bottom);
        // rt.offsetMax = new Vector2(rt.offsetMax.x + value1, rt.offsetMax.y + value2);
 
        Vector2 size = rt.rect.size;
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x);
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y);
    }
    /// <summary>
    /// 调整 RectTransform 组件中的 Left、Top 属性
    /// </summary>
    /// <param name="rt"></param>
    /// <param name="left">Left值</param>
    /// <param name="top">Top值</param>
    public static void LeftTop(RectTransform rt, float left, float top) {
        // float value1 = rt.offsetMin.y - top;
        // float value2 = rt.offsetMax.x + left;
        // Debug.Log(value1);
        // Debug.Log(value2);
 
        // rt.offsetMin = new Vector2(left, rt.offsetMin.y + value1);
        // rt.offsetMax = new Vector2(rt.offsetMax.x + value2, -top);
 
        Vector2 size = rt.rect.size;
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, left, size.x);
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y);
    }
    /// <summary>
    /// 调整 RectTransform 组件中的 Right、Bottom 属性
    /// </summary>
    /// <param name="rt"></param>
    /// <param name="right">Right值</param>
    /// <param name="bottom">Bottom值</param>
    public static void RightBottom(RectTransform rt, float right, float bottom) {
        // float value1 = rt.offsetMin.x - right;
        // float value2 = rt.offsetMax.y + bottom;
 
        // rt.offsetMin = new Vector2(rt.offsetMin.x + value1, bottom);
        // rt.offsetMax = new Vector2(-right, rt.offsetMax.y + value2);
 
        Vector2 size = rt.rect.size;
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x);
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, bottom, size.y);
    }
    /// <summary>
    /// 调整 RectTransform 组件中的 Right、Top 属性
    /// </summary>
    /// <param name="rt"></param>
    /// <param name="right">Right值</param>
    /// <param name="top">Top值</param>
    public static void RightTop(RectTransform rt, float right, float top) {
        // float value1 = rt.offsetMin.x - right;
        // float value2 = rt.offsetMin.y - top;
 
        // rt.offsetMin = new Vector2(rt.offsetMin.x + value1, rt.offsetMin.y + value2);
        // rt.offsetMax = new Vector2(-right, -top);
 
        Vector2 size = rt.rect.size;
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, right, size.x);
        rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, top, size.y);
    }
    public static void Center(RectTransform rt, float x, float y){
        Vector2 size = rt.rect.size;
        rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
        rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
        rt.localPosition = new Vector2(x, y);
    }


另外,有个朋友提出另一种方法,通过设置offsetMin和offsetMax来达到目的,这个比较复杂,他自己写了四个基本类型的(分别是距离四个边的位置),可以根据这个自己再进行拓展:

/// <summary>
    /// 调整 RectTransform 组件中的 Left 属性
    /// </summary>
    /// <param name="rt">引用目标 RectTransform 对象</param>
    /// <param name="left">Left值</param>
    public static void Left(RectTransform rt, float left) {
        if (left < rt.offsetMin.x) {
            float value = rt.offsetMin.x - left;
            
            rt.offsetMin = new Vector2(left, rt.offsetMin.y);
            rt.offsetMax = new Vector2(rt.offsetMax.x - value, rt.offsetMax.y);
        } else if (left > rt.offsetMin.x) {
            float value = left - rt.offsetMin.x;
            
            rt.offsetMin = new Vector2(left, rt.offsetMin.y);
            rt.offsetMax = new Vector2(rt.offsetMax.x + value, rt.offsetMax.y);
        }
    }
 
    /// <summary>
    /// 调整 RectTransform 组件中的 Right 属性
    /// </summary>
    /// <param name="rt">引用目标 RectTransform 对象</param>
    /// <param name="left">Right值</param>
    public static void Right(RectTransform rt, float right) {
        if (right < -rt.offsetMax.x) {
            float value = -rt.offsetMax.x - right;
            
            rt.offsetMin = new Vector2(rt.offsetMin.x + value, rt.offsetMin.y);
            rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
        } else if (right > -rt.offsetMax.x) {
            float value = right + rt.offsetMax.x;
 
            rt.offsetMin = new Vector2(rt.offsetMin.x - value, rt.offsetMin.y);
            rt.offsetMax = new Vector2(-right, rt.offsetMax.y);
        }
    }
 
    /// <summary>
    /// 调整 RectTransform 组件中的 Top 属性
    /// </summary>
    /// <param name="rt">引用目标 RectTransform 对象</param>
    /// <param name="left">Top值</param>
    public static void Top(RectTransform rt, float top) {
        if (top < -rt.offsetMax.y) {
            float value = -rt.offsetMax.y - top;
 
            rt.offsetMin = new Vector2(rt.offsetMin.x, rt.offsetMin.y + value);
            rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
        } else if (top > -rt.offsetMax.y) {
            float value = top + rt.offsetMax.y;
 
            rt.offsetMin = new Vector2(rt.offsetMin.x, rt.offsetMin.y - value);
            rt.offsetMax = new Vector2(rt.offsetMax.x, -top);
        }
    }
 
    /// <summary>
    /// 调整 RectTransform 组件中的 Bottom 属性
    /// </summary>
    /// <param name="rt">引用目标 RectTransform 对象</param>
    /// <param name="left">Bottom值</param>
    public static void Bottom(RectTransform rt, float bottom) {
        if (bottom < rt.offsetMin.y) {
            float value = rt.offsetMin.y - bottom;
            
            rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
            rt.offsetMax = new Vector2(rt.offsetMax.x, rt.offsetMax.y - value);
        } else if (bottom > rt.offsetMin.y) {
            float value = bottom - rt.offsetMin.y;
 
            rt.offsetMin = new Vector2(rt.offsetMin.x, bottom);
            rt.offsetMax = new Vector2(rt.offsetMax.x, rt.offsetMax.y + value);
        }
    }


原文链接:https://blog.csdn.net/pz789as/article/details/78790677

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值