本文主要是介绍unity开发 --------- NGUI (UIGrid),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
unity开发 --------- NGUI
UIGrid可以实现多个gameobject自动排序。可以设定其排序方向、每个元素的宽度,高度等。
public Arrangement arrangement = Arrangement.Horizontal;public int maxPerLine = 0;public float cellWidth = 200f;public float cellHeight = 200f;public bool repositionNow = false;public bool sorted = false;public bool hideInactive = true;
最重要的是,此脚本添加了[ExecuteInEditMode]属性,所以他能帮助我们在Edit中实现gameobject自动排序,无需等到执行时才排。
注意:
①就是UIGrid是根据gameobject的名字来排序的
②这里的排序,指的是在scene中items的位置的顺序。与Hierarchy中的属性无关。
下面是执行排序的函数
public void Reposition (){if (!mStarted){repositionNow = true;return;}Transform myTrans = transform;int x = 0;int y = 0;if (sorted){List<Transform> list = new List<Transform>();for (int i = 0; i < myTrans.childCount; ++i){Transform t = myTrans.GetChild(i);if (t && (!hideInactive || NGUITools.GetActive(t.gameObject))) list.Add(t);}//先用list来实现排序,然后再根据list中的顺序依次排开元素list.Sort(SortByName);for (int i = 0, imax = list.Count; i < imax; ++i){Transform t = list[i];if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;float depth = t.localPosition.z;t.localPosition = (arrangement == Arrangement.Horizontal) ?new Vector3(cellWidth * x, -cellHeight * y, depth) :new Vector3(cellWidth * y, -cellHeight * x, depth);if (++x >= maxPerLine && maxPerLine > 0){x = 0;++y;}}}else{for (int i = 0; i < myTrans.childCount; ++i){Transform t = myTrans.GetChild(i);if (!NGUITools.GetActive(t.gameObject) && hideInactive) continue;float depth = t.localPosition.z;t.localPosition = (arrangement == Arrangement.Horizontal) ?new Vector3(cellWidth * x, -cellHeight * y, depth) :new Vector3(cellWidth * y, -cellHeight * x, depth);if (++x >= maxPerLine && maxPerLine > 0){x = 0;++y;}}}UIDraggablePanel drag = NGUITools.FindInParents<UIDraggablePanel>(gameObject);if (drag != null) drag.UpdateScrollbars(true);}
这篇关于unity开发 --------- NGUI (UIGrid)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!