NGUI学习笔记(一):UIGrid的自定义 Sort

来源:互联网 发布:淘宝美工的工作内容 编辑:程序博客网 时间:2024/06/05 00:35

最近学习了一下NGUI的Grid

UIGrid.cs 脚本提供里对子元素的多种排序方式:

    public enum Sorting    {        None,        Alphabetic,        Horizontal,        Vertical,        Custom,    }

None表示按照元素的ChildIndex进行排序
Alphabetic表示按照元素的名字的字母表排序进行排序
Horizontal以及Vertical表示按照元素的localPosition进行排序
而最后一种则是十分贴心的自定义排序,前提是自行实现一种排序的比较算法
这里使用的算法是冒泡排序,需要设定的就是如何比较两个Item

下面是我写的Item类型

using UnityEngine;using System.Collections;using System;public class Item:MonoBehaviour{    public int quality;    public int cost;    public PartEnum.part part = PartEnum.part.none;    UILabel qualityLbl;    UILabel costLbl;    UILabel partLbl;    void Start()    {        qualityLbl = transform.FindChild("Sprite/QualityLbl").GetComponent<UILabel>();        costLbl = transform.FindChild("Sprite/CostLbl").GetComponent<UILabel>();        partLbl = transform.FindChild("Sprite/PartLbl").GetComponent<UILabel>();        Initialize ();    }    void Initialize()    {        quality = UnityEngine.Random.Range (0, 12);        cost = UnityEngine.Random.Range (300, 9000);        part = (PartEnum.part)UnityEngine.Random.Range (0, 3);        qualityLbl.text += quality.ToString ();        costLbl.text += cost.ToString ();        partLbl.text += Enum.GetName(typeof(PartEnum.part),part);    }}

用到的枚举

using UnityEngine;using System.Collections;using System;public static class PartEnum {    public enum part    {        none = -1,        head = 0,        body = 1,        foot = 2,    };}

正式的排序脚本

using UnityEngine;using System.Collections;using System;using System.Collections.Generic;using System.IO;using UnityEditor.VersionControl;using UObject = UnityEngine.Object;public class GridSort : MonoBehaviour {    public enum SortType    {        quality =0,        cost =1,        part =2,        none =3,    };    public SortType sortType = SortType.none;    int sortIndex = 0;    UILabel sortLabel;    UIButton sortBtn;    UIGrid uiGrid;    GameObject gridGO;    void Start () {        Initialize ();    }    void Initialize()    {        gridGO = GameObject.Find ("ItemScroll View");        uiGrid = gridGO.GetComponent<UIGrid>();        uiGrid.onCustomSort = CustomSort;        GameObject sortBtnObj = GameObject.Find ("SortBtn");        //UIEventListener.Get (sortBtnObj).onClick += new UIEventListener.VoidDelegate (OnObjBtnSortClick);        sortBtn = sortBtnObj.GetComponent<UIButton> ();        sortBtn.onClick.Add(new EventDelegate(OnUIBtnSortClick));        sortLabel = sortBtnObj.GetComponentInChildren<UILabel> ();    }    int CustomSort(Transform t1,Transform  t2)    {        Item a = t1.GetComponent<Item> ();        Item b = t2.GetComponent<Item> ();        switch (sortType)         {        case SortType.none:            return 0;        case SortType.part:            return a.part.CompareTo (b.part);        case SortType.quality:            return a.quality.CompareTo (b.quality);        case SortType.cost:            return a.cost.CompareTo (b.cost);        default:            return 0;        }    }    void OnObjBtnSortClick(GameObject go)    {        sortIndex++;        int newSortIndex = sortIndex % 4;        sortType =(SortType) Enum.ToObject (typeof(SortType), newSortIndex);        switch (sortType)         {        case SortType.cost:            sortLabel.text = "按花费排序";            break;        case SortType.quality:            sortLabel.text = "安品质排序";            break;        case SortType.part:            sortLabel.text = "按部位排序";            break;        case SortType.none:            sortLabel.text = "默认排序";            break;        }        uiGrid.repositionNow = true;        uiGrid.Reposition ();    }    void OnUIBtnSortClick()    {        sortIndex++;        int newSortIndex = sortIndex % 4;        sortType =(SortType) Enum.ToObject (typeof(SortType), newSortIndex);        switch (sortType)         {            case SortType.cost:                sortLabel.text = "按花费排序";                break;            case SortType.quality:                sortLabel.text = "安品质排序";                break;            case SortType.part:                sortLabel.text = "按部位排序";                break;            case SortType.none:                sortLabel.text = "默认排序";                break;        }        uiGrid.repositionNow = true;        uiGrid.Reposition ();    }}

这里要提一下监听UIButton有很多种方法,个人比较喜欢添加UIEventListener来实现.
使用UIEvenListener来监听的话,其onClick这个list里面添加的代理的签名返回值为空,但是有一个传入参数为GameObject类型.
而UIButton自带的onClick:

    /// <summary>    /// Click event listener.    /// </summary>    public List<EventDelegate> onClick = new List<EventDelegate>();

需要添加的代理类型为EventDelete,有三种创建方式:

    public EventDelegate () { }    public EventDelegate (Callback call) { Set(call); }    public EventDelegate (MonoBehaviour target, string methodName) { Set(target, methodName); }

我用的是第二种,我发现这个Callback返回值也为空,但是却不需要传入参数.
所以为了验证2种监听方法,我写了两个相似的函数OnObjBtnSortClick和OnUIBtnSortClick.

最后整理一下场景,运行,上效果:

GirdSort的运行效果]![这里写图片描述

原创粉丝点击