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.
最后整理一下场景,运行,上效果:
阅读全文
1 0
- NGUI学习笔记(一):UIGrid的自定义 Sort
- Unity3D学习笔记——NGUI之UIGrid
- NGUI下UIGrid的刷新
- NGUI-UIGrid
- u3d之NGUI的UIGrid初探
- Unity一NGUI学习笔记
- 【Unity3D】【NGUI】UIGrid
- unity开发 --------- NGUI (UIGrid)
- NGUI之UIGrid
- NGUI之UIGrid & UITable
- unity NGUI的UIGrid与UITable的区别
- Unity3D中NGUI学习笔记(一):对NGUI的基本认识
- NGUI控件说明(中文) UIGrid
- NGUI所见即所得之UIGrid & UITable
- NGUI UIScrollView UIGrid性能优化
- NGUI控件说明(中文) UIGrid
- NGUI所见即所得之UIGrid & UITable
- NGUI控件说明(中文) UIGrid
- php ci ajax
- Python机器学习应用 | 降维——PCA方法及其应用
- 比赛记录contest13
- kafka:一个分布式消息系统
- JSON简介
- NGUI学习笔记(一):UIGrid的自定义 Sort
- 在部署服务器上面调试程序
- C#要点
- count(*) over(partition by…的用法记录
- js面向对象---基本的概念、属性、方法
- 1013. Battle Over Cities (25)
- java反射Field的getModifiers()方法 返回值
- powerdesign pdm文件转excel
- 【GDB】gdbserver的使用,远程调试开发板