unity ngui
设置点击事件
ngui控件.gameObject.AddComponent<UIEventListener>();
//比如 public UIToggle mToggle;
//mToggle.gameObject.AddComponent<UIEventListener>();
UIEventListener.Get(mToggle.gameObject).onClick = ClickToggle;
private void ClickToggle(GameObject go){
Debug.Log (go.name);
}
动态给EventDelegate添加参数
EventDelegate.Add(List,方法名字);
1、创建 UI Root
NGUI Create 2D UI 出现 UI Root
UI Root 包含Camera
1.在UI Root (2D)对象上有个UIRoot脚本。这个脚本会重新调整游戏对象符合你的屏幕高度,有自动和手动选择高度。让你制定小部件在像素中的坐标,并且和游戏世界中的剩余对象相比依旧相对来说较小。
2.Camera对象包含Camera和UICamera脚本。UICamera脚本包含NGUI的事件系统(event system)。
2、在Camera下面点击 NGUI Create Panel
Panel对象有UIPanel脚本,UIPanel是一个容器,它将包含所有UI小部件,并负责将所包含的部件组合优化,以减少绘制命令的调用。
3、 选中Panel NGUI Open Widget Wizard()....控件奇才?什么鬼
添加控件
4、Sprite
Sprite 中的UI Sprite包含两个属性 Atlas 与 Sprite
Sprite包含在Atlas中
更换Sprite中的图片要先确定Atlas然后选择其中的Sprite
mSprite.spriteName = "brick";
5、Texture 动态添加资源、更换图片
6、Toggle
mToggle.GetComponent<UIToggle>().group = 1;
7、Progress Bar 进度条
8、Slider 滑动条
设置滑动事件
if (mSlider != null) {
mSlider.GetComponent<UISlider>().thumb.gameObject.AddComponent<UIEventListener>();
UIEventListener.Get(mSlider.GetComponent<UISlider>().thumb.gameObject).onDragEnd = DragSlider;
}
private void DragSlider(GameObject go){
Debug.Log (go.name +": "+ mSlider.value);
}
9、Popup List
if (mPopupList != null) {
mPopupList.Clear();
mPopupList.AddItem("First Item");
mPopupList.AddItem("Second Item");
EventDelegate.Add(mPopupList.onChange,ClickPopupList);
}
public void ClickPopupList(){
Debug.Log (mPopupList.value);
}
10、Scroll View
Scroll View包括一个含有UI Grid(Script)组件的Grid
Grid的每一个子项需要添加组件 Box Collider 和 UI Drag Scroll View(Script)
动态添加子项
var GameObject ngui_grid = GameObject.Find ("UIGrid");//Scroll View 的子物体Grid
UIGrid ngui_ui_grid = ngui_grid.GetComponent<UIGrid>();//Grid 的UI Grid的组件
GameObject _gridItem = NGUITools.AddChild(ngui_grid, (GameObject)(Resources.Load("GridItemPrefab")));//添加的子项
ngui_ui_grid.repositionNow = true; //加入这个标志,可以让元素添加之后,Grid对元素进行重新排列
//或者 ngui_ui_grid.Reposition();
// 动态改变子项的显示属性。
Transform[] allChildren = _gridItem.GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren)
{
if(child.gameObject.tag == "GridItemIcon")
{
UISprite _ItemIcon = child.gameObject.GetComponent<UISprite>();
_ItemIcon.spriteName = item.Icon.name;
}
else if(child.gameObject.tag == "GridItemName")
{
UILabel _ItemName = child.gameObject.GetComponent<UILabel>();
_ItemName.text = item.Name+" "+itemslot.Index;
}
else if(child.gameObject.tag == "GridItemPrice")
{
UILabel _ItemPrice = child.gameObject.GetComponent<UILabel>();
_ItemPrice.text = item.Price+"$";
}
}