转载自:http://www.unitymanual.com/blog-1591-2492.html
一 Scroll View 创建
1.在UI Root路径下,新建Panel1
2.在Panel下用菜单创建一个Scroll View,然后调节大小,位置,注意:宽度和高度要超过Item的宽高
3.在ScrollView下用菜单创建一个UIGrid, 然后调节宽度和高度要超过Item的宽高,Sorting属性设置为None;
Item创建
1.在Panel1下创建Sprite, ,注意Item预制物不要用Panel为父层,应该用Sprite.
2.为Sprite 添加box collider,和脚本Drag Scroll View . 详解:Drag Scroll View可以用鼠标或是Touch Event(触摸事件)控制 Scroll View.
3.复制 一行 相同的Sprite,并把它们排列整齐,就可以通过拖拽来使它们左右的滚动.
代码对其方式
UIGrid.Reposition();
UIScrollView.ResetPosition();
//上面代码对不齐,就用这个协程.
代码委流程
1.首先Panel物体是隐藏
2.根据数据创建Item挂到Grid上,Item显示
3.在显示Panel后物体的时候,用协成排序.
public void ShowPanel()
{
gameObject.SetActive(true);
StartCoroutine(WaitAndSort());
}
IEnumerator WaitAndSort()
{
yield return new WaitForSeconds(0.01f);
UIGrid1.Reposition();
yield return new WaitForSeconds(0.0001f);
Scrollview1.ResetPosition();
yield return new WaitForSeconds(0.01f);
}