UGUI 之 使用EventTrigger 按钮监听事件系统
【摘要】 创建自己的EventTriggerListener 类,继承自EventTrigger,,,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class EventTriggerL...
创建自己的EventTriggerListener 类,继承自EventTrigger,,,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{ public delegate void VoidDelegate(GameObject go); public VoidDelegate onClick; public VoidDelegate onSelect; public VoidDelegate onBegainDrag; public VoidDelegate onDown; public VoidDelegate onUp; public VoidDelegate onPress; public VoidDelegate DesPress; static public EventTriggerListener Get(GameObject go) { EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); if (listener == null) listener = go.AddComponent<EventTriggerListener>(); return listener; } public override void OnPointerClick(PointerEventData eventData) { if (onClick != null) { onClick(gameObject); } } public override void OnSelect(BaseEventData eventData) { if (onSelect != null) onSelect(gameObject); } public override void OnBeginDrag(PointerEventData eventData) { if (onBegainDrag != null) onBegainDrag(gameObject); } //public override void OnPointerDown(PointerEventData eventData) //{ // if (onDown != null) onDown(gameObject); //} //public override void OnPointerUp(PointerEventData eventData) //{ // if (onUp != null) onUp(gameObject); //} public override void OnPointerDown(PointerEventData eventData) { if (onPress != null) { onPress(gameObject); } } public override void OnPointerUp(PointerEventData eventData) { if (DesPress != null) { DesPress(gameObject); } }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
将上面的代码放到自己的项目中,就可以做监听按钮事件了,
在有UGUI的组件的游戏物体都可以使用,,例子如下:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
public class UIMain : MonoBehaviour {
private Button button;
private Image image;
void Start ()
{
button = this.GetComponent<Button>();
image = this.GetComponent<Image>();
//添加监听
EventTriggerListener.Get(button.gameObject).onClick =OnButtonClick;
EventTriggerListener.Get(image.gameObject).onClick =OnButtonClick;
//取消监听
// Destroy(EventTriggerListener.Get(image.gameObject).GetComponent<EventTriggerListener>());
} private void OnButtonClick(GameObject go){
//在这里监听按钮的点击事件
if(go == button.gameObject){ Debug.Log ("DoSomeThings");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/82954859
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)