字段赋值映射,NGUI、UGUI事件管理
来源:互联网 发布:mac用迅雷下载速度慢 编辑:程序博客网 时间:2024/06/03 19:20
using UnityEngine;using System.Collections;using System;using System.Reflection;namespace Utility.Common{public class AutoFieldsMonoBehavior : MonoBehaviour {protected FieldInfo[] m_fieldInfos;protected Component[] m_components;protected virtual void Awake (){m_fieldInfos = this.GetType().GetFields (BindingFlags.Public | BindingFlags.Instance);m_components = transform.GetComponentsInChildren<Component> ();AutoSetPubFields ();}private void AutoSetPubFields (){foreach (var item in m_fieldInfos) {if (item.GetValue (this) != null) {continue;}if (!item.FieldType.IsSubclassOf (typeof(MonoBehaviour))) {continue;}foreach (var comp in m_components) {if (item.FieldType == comp.GetType() && item.Name.Equals (comp.gameObject.name)) {item.SetValue (this, comp);break;}}}}}}public字段,并且变量命与要映射单位保持一致即可
using System.Collections;using System.Collections.Generic;using UnityEngine;namespace Utility.Common{public class NGUIMonoBehavior : AutoFieldsMonoBehavior{protected virtual void OnClickGo (GameObject go) {}protected virtual void OnHoverGo (GameObject go, bool isHover) {}protected virtual void OnPressGo (GameObject go, bool isPress) {}protected override void Awake (){base.Awake ();List<GameObject> m_scanGoList = new List<GameObject>();foreach (var item in m_fieldInfos) {if (item.FieldType.IsSubclassOf (typeof(MonoBehaviour)) && item.GetValue (this) != null) {GameObject go = (item.GetValue (this) as Component).gameObject;if (m_scanGoList.Contains (go))continue;m_scanGoList.Add (go);if (go.GetComponent<Collider> () != null) {UIEventListener listener = UIEventListener.Get (go);listener.onClick = OnEventClick;listener.onHover = OnEventHover;listener.onPress = OnEventPress;}}}}private void OnEventClick (GameObject go){this.OnClickGo (go);}private void OnEventHover (GameObject go, bool isHover){this.OnHoverGo (go, isHover);}private void OnEventPress (GameObject go, bool isPress){this.OnPressGo (go, isPress);}}}在上面的基础上对NGUI事件封装了一下
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;namespace Utility.Common{public class UGUIEventListener : EventTrigger {static public UGUIEventListener Get (GameObject go){UGUIEventListener listener = go.GetComponent<UGUIEventListener>();if (listener == null) listener = go.AddComponent<UGUIEventListener>();return listener;}public delegate void VoidDelegate (GameObject go);public delegate void BoolDelegate (GameObject go, bool state);public VoidDelegate onClick;public BoolDelegate onHover;public BoolDelegate onPress;private void OnClick (){if (onClick != null)onClick (gameObject);}private void OnHover (bool isHover){if (onHover != null)onHover (gameObject, isHover);}private void OnPress (bool isPress){if (onPress != null)onPress (gameObject, isPress);}public override void OnPointerClick( PointerEventData data ){//Debug.Log( "OnPointerClick called." );OnClick ();}public override void OnPointerDown( PointerEventData data ){//Debug.Log( "OnPointerDown called." );OnPress (true);}public override void OnPointerEnter( PointerEventData data ){//Debug.Log( "OnPointerEnter called." );OnHover (true);}public override void OnPointerExit( PointerEventData data ){//Debug.Log( "OnPointerExit called." );OnHover (false);}public override void OnPointerUp( PointerEventData data ){//Debug.Log( "OnPointerUp called." );OnPress (false);}#region NOT_USE/*public override void OnBeginDrag( PointerEventData data ){Debug.Log( "OnBeginDrag called." );}public override void OnCancel( BaseEventData data ){Debug.Log( "OnCancel called." );}public override void OnDeselect( BaseEventData data ){Debug.Log( "OnDeselect called." );}public override void OnDrag( PointerEventData data ){Debug.Log( "OnDrag called." );}public override void OnDrop( PointerEventData data ){Debug.Log( "OnDrop called." );}public override void OnEndDrag( PointerEventData data ){Debug.Log( "OnEndDrag called." );}public override void OnInitializePotentialDrag( PointerEventData data ){Debug.Log( "OnInitializePotentialDrag called." );}public override void OnMove( AxisEventData data ){Debug.Log( "OnMove called." );}public override void OnScroll( PointerEventData data ){Debug.Log( "OnScroll called." );}public override void OnSelect( BaseEventData data ){Debug.Log( "OnSelect called." );}public override void OnSubmit( BaseEventData data ){Debug.Log( "OnSubmit called." );}public override void OnUpdateSelected( BaseEventData data ){Debug.Log( "OnUpdateSelected called." );}*/#endregion}}模仿NGUI的很简单
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using UnityEngine.EventSystems;namespace Utility.Common{public class UGUIMonoBehavior : AutoFieldsMonoBehavior{protected virtual void OnClickGo (GameObject go) {}protected virtual void OnHoverGo (GameObject go, bool isHover) {}protected virtual void OnPressGo (GameObject go, bool isPress) {}protected override void Awake (){base.Awake ();List<GameObject> scanGoList = new List<GameObject> ();foreach (var item in m_fieldInfos) {if (item.FieldType.IsSubclassOf (typeof(MonoBehaviour)) && item.GetValue (this) != null) {GameObject go = (item.GetValue (this) as Component).gameObject;if (scanGoList.Contains (go))continue;scanGoList.Add (go);Image img = go.GetComponent<Image> ();if (img != null && img.raycastTarget == true) {UGUIEventListener listener = UGUIEventListener.Get (go);listener.onClick = OnEventClick;listener.onHover = OnEventHover;listener.onPress = OnEventPress;}}}}private void OnEventClick (GameObject go){this.OnClickGo (go);}private void OnEventHover (GameObject go, bool isHover){this.OnHoverGo (go, isHover);}private void OnEventPress (GameObject go, bool isPress){this.OnPressGo (go, isPress);}}}
UGUI的,很NGUI类似
根据使用情况只整理了click hover press,其他的可以参考自行添加
0 0
- 字段赋值映射,NGUI、UGUI事件管理
- UGUI-仿照NGUI的事件监听机制
- unity 按钮绑定事件(NGUI UGUI)
- unity UGUI Toggle事件管理
- 将UGUI的事件封装成NGUI模式
- Unity3D NGUI,uGUI总结
- NGUI PK UGUI
- 【UI】NGUI和UGUI
- NGUI与UGUI
- UGUI与NGUI
- NGUI UGUI入门
- NGUI与UGUI
- UGUI 事件
- UGUI事件
- UGUI和NGUI多个按钮事件方法(for循环添加)
- Unity UGUI与NGUI的比较,NGUI完胜UGUI
- 【Unity&UGUI&NGUI】浅谈UGUI和NGUI两者的优劣
- UGUI与NGUI优缺点对比
- STL--Lesson001-01-STL概述
- CCF2014-09-2画图
- 对于配置了Spring编码过滤器之后,仍出现中文乱码的处理
- css用一张大图片来设置背景优缺点 实现方法
- linux 套接字函数send和recv函数(阻塞模式)
- 字段赋值映射,NGUI、UGUI事件管理
- android下拉刷新scrollview的用法pullToRefreshScrollview
- MQTT 移植STM32+GPRS(串口透传)(三)
- css 兼容ie浏览器的方法
- vue自定义指令拖拽事件
- Android点赞动画
- 三月十八小结
- 恩,今天把git和sublime结合了一下。。然后看了下《西部世界》
- JS 判断字符串包含