从unity发布到安卓版本时,总会碰到页面布局偏差的问题,
今天无意间学习到了自动布局系统layout elements(布局元素)和layout controllers(布局控制器)
layout elements(布局元素)
一个布局元素用下列属性来定义自身:
Minimun width(最小宽度)
Minimum height(最小高度)
PReferred width(首选宽度)
Preferred height(首选高度)
Flexible width(灵活宽度)
Flexible height(灵活高度)
Layout Group是用来控制子布局元素的大小和位置的控制器
Spacing 表示 间距。Child Alignment 表示对齐方式。Child Force Expand 表示 自适应 宽 和高
做一下记录,可能不是很全面,只是就我目前的情况做了说明。
达到的效果:在页面的最下面添加一个简介,和一个文字滚动。
修改了一下Canvas的cancas scaler下UI scale mode的属性
然后创建了一个panel,在属性栏中添加了layout element(页面元素)和vertical layout group(垂直控制器) 我选择了下方部分区域
然后在父类panel下创建了一个子类panel用来显示简介,同样添加了layout element(页面元素)和horizontal layout group(垂直控制器)
在下面创建了个text,将想要展示的文字信息写进去。
父类下面又创建了一个panel2用来显示滚动文字,这里就不添加水平控制器了因为子类text要水平滚动,所以我给取消掉了
最后摆放一下text的位置,将脚本挂在text下,就可以实现跑马灯式的循环播放文字信息。
代码如下:
public class MoveText : MonoBehaviour{ public float MoveSpeed = 2; private Text m_text = null; void Start() { m_text = GetComponent<Text>(); TextRectTransform = this.GetComponent<RectTransform>(); originalX = TextRectTransform.position.x; m_preferredWidth = m_text.preferredWidth; } private float originalX; private float m_preferredWidth; private RectTransform TextRectTransform; private int tempCount = 0; private void Update() { TextRectTransform.position = new Vector3(TextRectTransform.position.x - MoveSpeed, TextRectTransform.position.y, TextRectTransform.position.z); if (TextRectTransform.position.x <= -m_preferredWidth) { TextRectTransform.position = new Vector3(originalX, TextRectTransform.position.y, TextRectTransform.position.z); } }}效果亲测有用。
新闻热点
疑难解答