【问题标题】:How to access RectTransform's left, right, top, bottom positions via code?如何通过代码访问 RectTransform 的左、右、上、下位置?
【发布时间】:2015-08-27 06:27:16
【问题描述】:

我有一个设置了渲染模式世界空间的 UI 画布。对于属于此画布的所有 UI 元素,我在编辑器的 RectTransform 组件中看到了“left”、“right”、“top”和“bottom”变量。有什么方法可以通过代码访问这些变量?

【问题讨论】:

    标签: unity3d unity3d-gui


    【解决方案1】:

    那些是

    RectTransform rectTransform;
    
    /*Left*/ rectTransform.offsetMin.x;
    /*Right*/ rectTransform.offsetMax.x;
    /*Top*/ rectTransform.offsetMax.y;
    /*Bottom*/ rectTransform.offsetMin.y;
    

    【讨论】:

      【解决方案2】:
      RectTransform rt = GetComponent<RectTransform>();
      float left   =  rt.offsetMin.x;
      float right  = -rt.offsetMax.x;
      float top    = -rt.offsetMax.y;
      float bottom =  rt.offsetMin.y;
      

      TL;DR

      根据 Inspector 中显示的值,我的分析是,如果相应的边界位于由 RectTransform 的锚点形成的矩形中,则 LeftRightTopBottom 是正数。

      offsetMin.x as LeftoffsetMin.y as Bottom 始终满足此评估,但 offsetMax.x as RightoffsetMax.y as Top 没有。

      我只是取了 offsetMax 的相反值以使其符合要求(基本空间修改)。

      【讨论】:

      • 这并不总是有效。它取决于锚点等...确实 Unity 需要提供一种方法来确保您的代码能够正常工作,而不是与其他变量混淆。
      【解决方案3】:

      以上两个答案都在正确的轨道上,因此我一直在拖延我的项目,但在床上编程时发现。 offsetMin 和 offsetMax 是您所需要的,但它们不是全部,您需要包含 rect 变换中的锚点:

      public RectTransform recTrans;
      
      // Use this for initialization
      void Start () {
          Vector2 min = recTrans.anchorMin;
          min.x *= Screen.width;
          min.y *= Screen.height;
      
          min += recTrans.offsetMin;
      
          Vector2 max = recTrans.anchorMax;
          max.x *= Screen.width;
          max.y *= Screen.height;
      
          max += recTrans.offsetMax;
      
          Debug.Log(min + " " + max);
      }
      

      如果你把它插入一个虚拟类,无论你使用什么锚,它都应该给你正确的读数。

      它的工作方式应该是显而易见的,但一个小小的解释不会有坏处。

      偏移量是指最小值和最大值与矩形变换中心点的位置差异,将它们添加到锚边界可以得到正确的矩形变换的最小值和最大值。虽然锚的最小值和最大值已标准化,但您需要通过乘以屏幕尺寸来缩小它们。

      【讨论】:

      • 屏幕尺寸?即使在世界空间画布上也是如此吗?我怀疑(通常)乘以正确的东西是画布大小。
      • 现在,为什么 unity 不默认提供这个?还是他们与 stackoverflow 有合同协议?
      【解决方案4】:

      您还可以更轻松地在两行中设置所有值。只需获取 Vector2 的 offsetMin 并设置两个参数(第一个是 Left,第二个是底部)。然后对 offsetMax 做同样的事情(第一个是正确的,第二个是顶部)。记住 offsetMax 是相反的,所以如果你想设置 Right = 20 那么你需要在脚本中设置值 -20。

      下面的代码设置值:

      左 = 10,

      底部 = 20,

      右 = 30,

      顶部 = 40

      GameObject.GetComponent<RectTransform> ().offsetMin = new Vector2 (10,20);
      GameObject.GetComponent<RectTransform> ().offsetMax = new Vector2 (-30,-40);
      

      我希望它会帮助别人;)

      【讨论】:

        【解决方案5】:

        我需要经常调整这些属性,所以我添加了一种更短、更方便的方法来设置它们。

        我为 RectTransform 编写了一个扩展,您可以在其中非常简单地设置这些属性:

        public static class Extensions
        {
            public static void SetPadding(this RectTransform rect, float horizontal, float vertical) {
                rect.offsetMax = new Vector2(-horizontal, -vertical);
                rect.offsetMin = new Vector2(horizontal, vertical);
            }
        
            public static void SetPadding(this RectTransform rect, float left, float top, float right, float bottom)
            {
                rect.offsetMax = new Vector2(-right, -top);
                rect.offsetMin = new Vector2(left, bottom);
            }
        }
        

        要使用扩展,您只需像这样调用方法:

        var rect = YourGameObject.GetComponent<RectTransform>()
        
        // with named parameters
        rect.SetPadding(horizontal: 100, vertical: 40);
        
        // without named parameters (shorter)
        rect.SetPadding(100, 40);
        
        // explicit, with named parameters
        buyButton.SetPadding(left: 100, top: 40, right: 100, bottom: 40);
        
        // explicit, without named parameters (shorter)
        buyButton.SetPadding(100, 40, 100, 40);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-05-01
          • 2015-11-30
          • 2014-06-01
          • 2011-05-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多