码迷,mamicode.com
首页 > 其他好文 > 详细

UGUI之RectTransform排版

时间:2017-09-09 17:20:57      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:需要   contex   添加   grid   children   context   ati   技术   文本   

 

往Scroll View里面添加子元素,并整齐排列

Text last =Instantiate(text,transform.position,transform.rotation).GetComponent<Text>();//生成预制text
last.text = nowText;//设置新对象的文本
Text[] texts = this.transform.GetComponentsInChildren<Text>();//获取该Scroll View下有多少个text,用于计算新产生text的y值
last.transform.parent=context.transform;//设置text父节点为Scroll View里面的context

//设置text的锚点框为横向拉伸对其父节点
last.GetComponent<RectTransform> ().anchorMin = new Vector2 (0, 1);//左下
last.GetComponent<RectTransform> ().anchorMax = new Vector2 (1, 1);//右上

//设置text的大小
last.GetComponent<RectTransform> ().offsetMax = new Vector2 (2, 0);//左下偏移量 
last.GetComponent<RectTransform> ().offsetMin = new Vector2 (2, -20);//右上偏移量

last.GetComponent<RectTransform> ().anchoredPosition = new Vector2 (0, -10-(texts.Length)*20);//设置锚点位置(当锚点是一个点的时候就是该点,本例中该锚点框是一条线,所以该点指的这条锚点线的中间,同理如果是矩形时那么对应的也是该矩形中心)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

还可以直接在context上面加一个GridLayoutGroup组件

技术分享

只需要把对应的每个cell的大小 边距 配置好 直接往context里面添加子物体也能达到同样的效果。

代码就简化为:

Text last =Instantiate(text,transform.position,transform.rotation).GetComponent<Text>();
last.text = nowText;

last.transform.parent=context.transform;

所有的排版设置全部都交给GridLayoutGroup完成了

UGUI之RectTransform排版

标签:需要   contex   添加   grid   children   context   ati   技术   文本   

原文地址:http://www.cnblogs.com/xcg2017/p/7498556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!