Unity 事件2
2013-10-29 21:39
162 查看
UIMouseEvent.cs:
UIEvent.cs:
UIMouseClick.cs:
最后把UIMouseClick.cs放到NGUI的button上面就可以了,这个button的名字为“quickLoginBtn”,结果如下:
using UnityEngine; using System; public abstract class UIMouseEvent : MonoBehaviour { public EventHandler<EventArgs> MouseEvent { get; set; } }
UIEvent.cs:
using UnityEngine; using System; using System.Collections.Generic; using System.Collections; public class UIEvent { public static GameObject FindChild (string objName) { GameObject obj = GameObject.Find("quickLoginBtn"); return obj; } public static void AddChildMouseClickEvent (string objName, EventHandler<EventArgs> action) { AddMouseClickEvent (FindChild (objName), action); } public static void AddMouseClickEvent (GameObject obj, EventHandler<EventArgs> action) { if (obj != null) { AddEvent<UIMouseClick> (obj, action); } Debug.Log("111111"); } public static void AddEvent<T> (GameObject obj, EventHandler<EventArgs> action) where T : UIMouseEvent { T mouseEvent = obj.GetComponent<T> (); if (null != mouseEvent) { GameObject.DestroyImmediate (mouseEvent); } mouseEvent = obj.AddComponent<T> (); mouseEvent.MouseEvent = action; Debug.Log("22222"); } public static void AttachEvent () { Debug.Log ("AttachEvent"); AddChildMouseClickEvent ("quickLoginBtn", OnClickStart); } private static void OnClickStart (object sender, EventArgs e) { Debug.Log("OnClickStart"); } }
UIMouseClick.cs:
using UnityEngine; using System; public class UIMouseClick : UIMouseEvent { void OnClick() { Debug.Log("hhhhh"); UIEvent.AttachEvent(); if(null != MouseEvent) MouseEvent(gameObject, null); } }
最后把UIMouseClick.cs放到NGUI的button上面就可以了,这个button的名字为“quickLoginBtn”,结果如下:
相关文章推荐
- Unity嵌入Android后Android视图获取不到事件焦点
- unity 3d 使用C#的事件/委托机制
- Unity之一天一个技术点(十五)---给unity3d的老版动画中,添加事件的方法
- 京城游戏人-Day14: Unity 中为 Sprite 添加点击事件
- Unity中的事件系统演进
- [置顶] unity中事件函数的执行顺序【超详细,图+文】
- 蓝鸥Unity入门鼠标事件学习笔记
- Unity事件系统
- 在Unity中使用事件/委托机制(event/delegate)进行GameObject之
- Unity笔记 UGUI (画布 锚点 事件 遮罩 )
- Unity 5事件系统
- Unity -VS - DLL 编辑后期生成事件
- Tolua使用笔记六:在lua中操作C#的委托事件与在lua中对Unity的GameObject的操作
- Unity API(一)Unity中的脚本事件及其函数
- Unity Android手机触屏事件
- Unity中消息事件的封装与运用
- Unity3D研究之在Unity中使用事件/委托机制(event/delegate)进行GameObject之间的通信引入中间层
- 如何实现Windows Phone代码与Unity相互通信(事件方式)
- Unity-3D 输入响应事件
- UNITY中的MOUSE点击事件的判断和AS3中的异同