zoukankan      html  css  js  c++  java
  • Unity 事件2

    UIMouseEvent.cs:

    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”,结果如下:

  • 相关阅读:
    Node.js连接MySQL数据库及构造JSON的正确姿势
    Lyx输入中文与代码高亮
    查看文件编码 + 查看文件扩展名 + 文件编码转换
    系统重装/装Anaconda后,Windows开始菜单缺少快捷方式解决方案
    Python动态网页爬虫-----动态网页真实地址破解原理
    多线程爬虫
    openpyxl基本操作
    BitMap原理
    Trie(前缀树/字典树)及其应用
    tensorflow和pytorch教程
  • 原文地址:https://www.cnblogs.com/james1207/p/3395264.html
Copyright © 2011-2022 走看看