代码之家  ›  专栏  ›  技术社区  ›  johnny 5

Unity防止点击按钮

  •  3
  • johnny 5  · 技术社区  · 8 年前

    我正在为长时间按下按钮创建脚本。长按钮按下有效,唯一的问题是,如果长按钮按下触发,我想阻止点击按钮。

    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.EventSystems;
    using System.Collections;
    
    public class LongPressButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
    {
        [Tooltip("How long must pointer be down on this object to trigger a long press")]
        public float durationThreshold = 1.0f;
    
        public UnityEvent onLongPress = new UnityEvent();
    
        private bool isPointerDown = false;
        private bool longPressTriggered = false;
        private float timePressStarted;
    
        private void Update()
        {
            if (isPointerDown && !longPressTriggered)
            {
                if (Time.time - timePressStarted > durationThreshold)
                {
                    longPressTriggered = true;
                    onLongPress.Invoke();
                }
            }
        }
    
        public void OnPointerDown(PointerEventData eventData)
        {
            timePressStarted = Time.time;
            isPointerDown = true;
            longPressTriggered = false;
        }
    
        public void OnPointerUp(PointerEventData eventData)
        {
            isPointerDown = false;
        }
    
    
        public void OnPointerExit(PointerEventData eventData)
        {
            isPointerDown = false;
        }
    }
    

    按钮脚本是黑框的,看起来像是构建到unity引擎中。

    我知道有一些方法可以防止事件传播到3d世界,但有没有一种方法可以防止在按钮脚本上传播?

    1 回复  |  直到 8 年前
        1
  •  0
  •   johnny 5    8 年前

    我已经知道了,由于事件系统的统一性限制,您需要两个脚本来完成。首先是长按钮按下脚本:

    using UnityEngine;
    using UnityEngine.Events;
    using UnityEngine.EventSystems;
    using System.Collections;
    using UnityEngine.UI;
    
    public class LongPressButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
    {
        [Tooltip("How long must pointer be down on this object to trigger a long press")]
        public float durationThreshold = 1.0f;
    
        public UnityEvent onLongPress = new UnityEvent(); 
    
        private bool isPointerDown = false;
        private bool longPressTriggered = false;
        private float timePressStarted;
    
        public bool WasLongPressTriggered
        {
            get;
            protected set;
        }
    
        private void Update()
        {
            if (isPointerDown && false == longPressTriggered)
            {
                if (Time.time - timePressStarted > durationThreshold)
                {
                    longPressTriggered = true;
                    WasLongPressTriggered = true;
                    onLongPress.Invoke();
                }
            }
        }
    
        public void OnPointerDown(PointerEventData eventData)
        {
            timePressStarted = Time.time;
            isPointerDown = true;
            longPressTriggered = false;
            WasLongPressTriggered = false;
        }
    
        public void OnPointerUp(PointerEventData eventData)
        {
            isPointerDown = false;
        }
    
    
        public void OnPointerExit(PointerEventData eventData)
        {
            isPointerDown = false;
        }
    }
    

    然后,您将需要一个新的按钮脚本来覆盖旧的基本按钮:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    public class Button2 : Button
    {
        public bool PreventPropagationOnLongPress;
    
        public override void OnPointerClick(PointerEventData eventData)
        {
            var longPress = GetComponent<LongPressButton>();
            if (longPress == null)
            {
                base.OnPointerClick(eventData);
            }
            if(false == longPress.WasLongPressTriggered)
            {
                base.OnPointerClick(eventData);
            }
        }
    }
    

    这在编辑器中起作用。它尚未在移动设备上进行全面测试,可能在unity edidor中调用点击,但在移动设备中,它可能会在鼠标向上时触发。目前,这将防止事件传播,