关于NGUI按钮事件封装的学习
学习了ngui模板,发现UISceneWidget中的OnClick等一系列事件定义了但是并没有写明如何触发,查找到UICamera中关键字
// If the touch should consider clicks, send out an OnClick notification
if (currentTouch.clickNotification != ClickNotification.None)
{
float time = RealTime.time;
Notify(currentTouch.pressed, "OnClick", null);
if (currentTouch.clickTime + 0.35f > time)
{
Notify(currentTouch.pressed, "OnDoubleClick", null);
}
currentTouch.clickTime = time;
}
可知,使用Notify来实现点击方法
static public void Notify (GameObject go, string funcName, object obj)
{
if (go != null)
{
go.SendMessage(funcName, obj, SendMessageOptions.DontRequireReceiver);
if (genericEventHandler != null && genericEventHandler != go)
{
genericEventHandler.SendMessage(funcName, obj, SendMessageOptions.DontRequireReceiver);
}
}
}
然后就是看SendMessage了,这是GameObject下的一个方法
// 摘要:
// Calls the method named methodName on every MonoBehaviour in this game object.
//
// 参数:
// methodName:
// The name of the method to call.
//
// value:
// An optional parameter value to pass to the called method.
//
// options:
// Should an error be raised if the method doesn't exist on the target object?
public void SendMessage(string methodName, [DefaultValue("null")] object value, [DefaultValue("SendMessageOptions.RequireReceiver")] SendMessageOptions options);
根据描述知道是通过方法名调用所有的GameObject下挂载的脚本中的methodName方法
所以定义了相同名称的方法,在按钮触发的时候会一起被调用