动态修改RectTransform的值

首先,可以阅读下这篇文章,里面将这个组件讲的很透彻。


用uGUI的时候。经常需要动态改变RectTransform的值,

 

当选择不同的布局。你会发现这里的值是不同的


offsetMin   是vector2(left,bottom);


offsetMax 是vector2(right,top); //如果Anchors max=(1,1),right,top要传入负数;


1.改变RectTransform的top和right
GetComponent().offsetMax = new Vector2(left, top);


2.改变RectTransform的bottom和left
GetComponent().offsetMin = new Vector2(right, bottom);


3.改变RectTransform的width,height
GetComponent().sizeDelta = new Vector2(width, height);


4.改变RectTransform的pos
GetComponent().anchoredPosition3D = new Vector3(posx,posy,posz);
GetComponent().anchoredPosition = new Vector2(posx,posy);


你可能感兴趣的:(Unity之UGUI)