字段赋值映射,NGUI、UGUI事件管理

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值