NGUI中实时查看UIEventListener的通知对象

NGUI中有好几种方式可以获取按钮的点击事件,我最喜欢用UIEventListener(一行代码搞定),可惜这个脚本在运行时是个黑箱,我不知道它会给哪些脚本发消息。如果某个UI模块是别人写的,脚本调用又混乱的话,查起来就很麻烦了,最后忍不住了写了个UIEventListener的Inspector扩展,用来查看按钮消息传给谁,最终效果如下图所示:

是不是爽多了?废话不多说,上干货:

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

//----------------------------------------------
// Author : LZ
//----------------------------------------------

[CustomEditor(typeof(UIEventListener), true)]
public class UIEventListenerEditor : Editor
{
	public enum EEventType
	{
		OnSubmit,
		OnClick,
		OnDoubleClick,
		OnHover,
		OnPress,
		OnSelect,
		OnScroll,
		OnDragStart,
		OnDrag,
		OnDragOver,
		OnDragOut,
		OnDragEnd,
		OnDrop,
		OnKey,
		OnTooltip
	}

	public UIEventListener script;

	private GUIStyle _gs_Container;
	private GUIStyle _gs_Title;
	private GUIStyle _gs_ContentText;

	private string _functionText = "<color=#00cccc>{0}</color>.<color=#ffcc00>{1}</color>()";

	void Awake()
	{
		script = target as UIEventListener;
		InitGUIStyle();
	}

	private void InitGUIStyle()
	{
		_gs_Container = new GUIStyle("ProjectBrowserPreviewBg");
		_gs_Container.padding = new RectOffset(0, 0, 3, 5);
		_gs_Container.stretchHeight = false;

		_gs_Title = new GUIStyle("TL LogicBar 0");
		_gs_Title.padding = new RectOffset(5, 5, 0, 0);
		_gs_Title.alignment = TextAnchor.MiddleLeft;
		_gs_Title.fixedHeight = 18;
		_gs_Title.fontStyle = FontStyle.Bold;
		_gs_Title.fontSize = 12;
		_gs_Title.normal.textColor = new Color(0.9f, 0.9f, 0.9f);

		_gs_ContentText = new GUIStyle("OL EntryBackOdd");
		_gs_ContentText.padding = new RectOffset(3, 3, 1, 2);
		_gs_ContentText.border = new RectOffset(3, 3, 3, 3);
		_gs_ContentText.margin = new RectOffset(4, 4, 2, 3);
		_gs_ContentText.alignment = TextAnchor.MiddleLeft;
		_gs_ContentText.richText = true;
	}

	public override void OnInspectorGUI()
	{
		base.OnInspectorGUI();

		DrawEvents(EEventType.OnSubmit, script.onSubmit);
		DrawEvents(EEventType.OnClick, script.onClick);
		DrawEvents(EEventType.OnDoubleClick, script.onDoubleClick);
		DrawEvents(EEventType.OnHover, script.onHover);
		DrawEvents(EEventType.OnPress, script.onPress);
		DrawEvents(EEventType.OnSelect, script.onSelect);
		DrawEvents(EEventType.OnScroll, script.onScroll);
		DrawEvents(EEventType.OnDragStart, script.onDragStart);
		DrawEvents(EEventType.OnDrag, script.onDrag);
		DrawEvents(EEventType.OnDragOver, script.onDragOver);
		DrawEvents(EEventType.OnDragOut, script.onDragOut);
		DrawEvents(EEventType.OnDragEnd, script.onDragEnd);
		DrawEvents(EEventType.OnDrop, script.onDrop);
		DrawEvents(EEventType.OnKey, script.onKey);
		DrawEvents(EEventType.OnTooltip, script.onTooltip);
	}

	private void DrawEvents(EEventType eventType, System.Delegate e)
	{
		if (e == null)
		{
			return;
		}

		GUILayout.Label(eventType.ToString(), _gs_Title);
		GUILayout.BeginVertical(_gs_Container);
		{
			if (e != null)
			{
				System.Delegate[] ds = e.GetInvocationList();
				for (int i = 0; i < ds.Length; i++)
				{
					System.Delegate d = ds[i];
					if (d.Method != null)
					{
						GUILayout.BeginHorizontal();
						string s = string.Format(_functionText, d.Method.DeclaringType.ToString(), d.Method.Name);
						EditorGUILayout.TextField(s, _gs_ContentText);
						if (d.Target as Component != null)
						{
							EditorGUILayout.ObjectField((d.Target as Component).gameObject, d.Target.GetType(), false);
						}
						GUILayout.EndHorizontal();
					}
				}
			}
		}
		GUILayout.EndVertical();
	}
}
别忘了把代码放到Editor目录下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值