unity3d按钮事件按下不放开的事件

UNITY3D UGUI按钮的监听事件系统 - 泰课资源分享 - 泰课在线 - 国内专业的Unity在线学习平台|Unity3d培训|Unity教程|Unity教程 Unreal 虚幻 AR|移动开发|美术CG|UI平面设计|前端开发 - Powered By EduSoho
UNITY3D UGUI按钮的监听事件系统
UNITY3D UGUI按钮的监听事件系统怎样的,怎么样才能让UGUI监听的方式和NGUI差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。
using UnityE
using System.C
using UnityEngine.EventS
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{
public delegate void VoidDelegate (GameObject go);
public VoidDelegate onC
public VoidDelegate onD
public VoidDelegate onE
public VoidDelegate onE
public VoidDelegate onUp;
public VoidDelegate onS
public VoidDelegate onUpdateS
static public EventTriggerListener Get (GameObject go)
EventTriggerListener listener = go.GetComponent&EventTriggerListener&();
if (listener == null) listener = go.AddComponent&EventTriggerListener&();
public override void OnPointerClick(PointerEventData eventData)
if(onClick != null) onClick(gameObject);
public override void OnPointerDown (PointerEventData eventData){
if(onDown != null) onDown(gameObject);
public override void OnPointerEnter (PointerEventData eventData){
if(onEnter != null) onEnter(gameObject);
public override void OnPointerExit (PointerEventData eventData){
if(onExit != null) onExit(gameObject);
public override void OnPointerUp (PointerEventData eventData){
if(onUp != null) onUp(gameObject);
public override void OnSelect (BaseEventData eventData){
if(onSelect != null) onSelect(gameObject);
public override void OnUpdateSelected (BaseEventData eventData){
if(onUpdateSelect != null) onUpdateSelect(gameObject);
& & & 然后在你的界面里面写入监听按钮的代码。
using UnityE
using System.C
using UnityEngine.UI;
using UnityEngine.EventS
using UnityEngine.E
public class UIMain : MonoBehaviour {
void Start ()
button = transform.Find("Button").GetComponent&Button&();
image = transform.Find("Image").GetComponent&Image&();
EventTriggerListener.Get(button.gameObject).onClick =OnButtonC
EventTriggerListener.Get(image.gameObject).onClick =OnButtonC
private void OnButtonClick(GameObject go){
&//在这里监听按钮的点击事件
if(go == button.gameObject){
Debug.Log ("DoSomeThings");
& & & &虽然还有一些别的监听方式,但是我觉得这种方式是最科学的,大家可根据自己项目的需求继续拓展EventTriggerListener类。
你还没有登录,请先或!
还有-5条回复,
你还没有登录,请先或!
Unity3D技术交流1群
泰课Unity3D公开课
工作时间:9:00 - 17:00
北京客服:010 -
广州客服:020 -Unity3D在Android设备上返回按钮失效解决办法
使用Unity3D开发Android程序是,在默认的情况下开发的程序不接受系统的返回按钮事件,若想要返回按钮生效,需要进行如下几步操作:
1、新建一个代码脚本,NewBehaviourScript就是我新建的代码,代码的文件名可以随便取,创建方法,在Project视窗中点击右键-&Create-&JavaScript。
2、创建Javascript后,双击脚本文件,打开编辑窗口,输入如下代码:&
#pragma strict
function Start () {
function Update () {
if(Input.GetKey(KeyCode.Escape)){
Application.Quit();
function OnGUI () {
3、将编辑好的代码脚本文件通过拖拽的方式拖拽到加载的模型上(或通过Script脚本选择脚本),模型必须是一直加载的模型,我用的是地形对象,测试好用;
4、接下来编译运行,返回按钮好用了。
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
我把image添加了按钮组件,做成预设(因为要动态创建),并且想添加ON click(),结果预设不能添加事件。请问怎么办, 如果要在脚本里动态的添加事件,能举个例子吗。
动态创建之后
用代码给你的组件添加click响应时间
gameObject.GetComponent().onClick.AddListener(Onclick);
private void Onclick()
Debug.Log("点击");
看下Simple UI里面有的吧。我比较喜欢看文档上的代码或者demo里面的代码。会感觉真实些
要回复问题请先或
浏览: 2066
关注: 4 人using UnityE
using UnityEngine.E
using UnityEngine.EventS
using System.C
public class test : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler
    public float interval = 0.1f;
    [SerializeField]
    UnityEvent m_OnLongpress = new UnityEvent();
    private bool isPointDown =
    private float lastInvokeT
    // Update is called once per frame
    void Update()
    {
        if (isPointDown)
        {
            if (Time.time - lastInvokeTime & interval)
            {
                //触发点击;
                m_OnLongpress.Invoke();
                lastInvokeTime = Time.
                Debug.Log(&长按&);
            }
        }
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        m_OnLongpress.Invoke();
        isPointDown =
        lastInvokeTime = Time.
        Debug.Log(&鼠标按下&);
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        isPointDown =
        Debug.Log(&鼠标抬起&);
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        isPointDown =
        Debug.Log(&鼠标退出&);
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        isPointDown =
        Debug.Log(&鼠标点击&);
    }
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2876次
排名:千里之外
转载:11篇
(3)(2)(9)(1)

我要回帖

更多关于 unity3d 获取按钮事件 的文章

 

随机推荐