ugui在运行时改变RectTransform的大小

http://blog.csdn.net/BeiFuDeNvWang/article/details/50838266

 

在代码中动态改变RectTransform大小的方法如下所示:

1:直接对sizeDelta属性进行赋值,其中X和Y可以对应理解成width和height。sizeDelta的具体含义:若achors是一个点的话则代表宽高,否则为到锚点的距离

 

  1. var rt = gameObject.GetComponent<RectTransform>();  
  2. rt.sizeDelta = new Vector2(100, 30);  

2:使用SetSizeWithCurrentAnchors函数来进行设定,其中Horizontal和Vertical分别对应宽和高。此函数受当前锚点和中心点的影响。

 

  1. var rt = gameObject.GetComponent<RectTransform>();  
  2. rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);  
  3. rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 30);  

3:使用SetInsetAndSizeFromParentEdge函数来进行设定。此函数不受锚点和中心的影响,其中第一个参数代表对齐方式,第二个参数为距离边界的距离,第三个参数为宽度。

 

  1. var rt = gameObject.GetComponent<RectTransform>();  
  2. rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 100);  
  3. rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 30);  


在确定使用绝对位置的时候,推荐使用第三种方法。 
参考文章:http://www.manew.com/thread-41633-1-1.html

原文地址:https://www.cnblogs.com/alps/p/7824384.html