在實際項目中使用UIGrid來排序是非常方便的,但它原有的幾個排序不能滿足我們的需要, 仔細查看源碼,作者已經爲我們留下了接口
下面 將要代碼實現按照我們自定義的排序方式進行排序,已item 上掛的腳本上面的ID(int)變量來排序,
新建一個UI_MyGrid腳本掛在UIGrid上
using UnityEngine;
using System.Collections;
public class UI_MyGrid :UIGrid {
// Use this for initialization
void Start () {
}
protected override void Sort (System.Collections.Generic.List<Transform> list)
{
if (sorting == Sorting.Custom)
list.Sort (this.SortByLevelID);
}
int SortByLevelID(Transform a,Transform b)
{
return a.GetComponent<checkIditem>().ID.CompareTo(b.GetComponent<checkIditem>().ID);
}
// Update is called once per frame
void Update() {
if(Input.GetKeyDown(KeyCode.A))
this.Reposition();
}
}