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

  • 相关阅读:
    iOS证书的使用
    ios设备管理
    矩阵的相关问题(旋转矩阵&螺旋矩阵)
    flex实现多列布局效果&对角线布局
    peerdependencies
    数组和对象遍历方法对比
    async和defer
    Promise.all并发限制
    electron+react开发属于自己的桌面应用
    webpack代码切割
  • 原文地址:https://www.cnblogs.com/james1207/p/3395264.html
Copyright © 2011-2022 走看看