1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.EventSystems;
4 public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
5 public delegate void VoidDelegate (GameObject go);
6 public VoidDelegate onClick;
7 public VoidDelegate onDown;
8 public VoidDelegate onEnter;
9 public VoidDelegate onExit;
10 public VoidDelegate onUp;
11 public VoidDelegate onSelect;
12 public VoidDelegate onUpdateSelect;
13
14 static public EventTriggerListener Get (GameObject go)
15 {
16 EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
17 if (listener == null) listener = go.AddComponent<EventTriggerListener>();
18 return listener;
19 }
20 public override void OnPointerClick(PointerEventData eventData)
21 {
22 if(onClick != null) onClick(gameObject);
23 }
24 public override void OnPointerDown (PointerEventData eventData){
25 if(onDown != null) onDown(gameObject);
26 }
27 public override void OnPointerEnter (PointerEventData eventData){
28 if(onEnter != null) onEnter(gameObject);
29 }
30 public override void OnPointerExit (PointerEventData eventData){
31 if(onExit != null) onExit(gameObject);
32 }
33 public override void OnPointerUp (PointerEventData eventData){
34 if(onUp != null) onUp(gameObject);
35 }
36 public override void OnSelect (BaseEventData eventData){
37 if(onSelect != null) onSelect(gameObject);
38 }
39 public override void OnUpdateSelected (BaseEventData eventData){
40 if(onUpdateSelect != null) onUpdateSelect(gameObject);
41 }
42 }
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.UI;
4 using UnityEngine.EventSystems;
5 using UnityEngine.Events;
6 public class UIMain : MonoBehaviour {
7 Button button;
8 Image image;
9 void Start ()
10 {
11 button = transform.Find("Button").GetComponent<Button>();
12 image = transform.Find("Image").GetComponent<Image>();
13 EventTriggerListener.Get(button.gameObject).onClick =OnButtonClick;
14 EventTriggerListener.Get(image.gameObject).onClick =OnButtonClick;
15 }
16
17 private void OnButtonClick(GameObject go){
18 //在这里监听按钮的点击事件
19 if(go == button.gameObject){
20 Debug.Log ("DoSomeThings");
21 }
22 }
23 }
链接: http://www.xuanyusong.com/archives/3325