您的位置:首页 > 产品设计 > UI/UE

ugui在运行时改变RectTransform的大小

2017-11-13 01:05 363 查看
http://blog.csdn.net/BeiFuDeNvWang/article/details/50838266

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

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

[html] view plain copy print?

var rt = gameObject.GetComponent<RectTransform>();

rt.sizeDelta = new Vector2(100, 30);

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

[html] view plain copy print?

var rt = gameObject.GetComponent<RectTransform>();

rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);

rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 30);

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

[html] view plain copy print?

var rt = gameObject.GetComponent<RectTransform>();

rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 100);

rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 30);

在确定使用绝对位置的时候,推荐使用第三种方法。

参考文章:http://www.manew.com/thread-41633-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: