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,其他的可以参考自行添加