2016-12-23 4 views
1

私は長いボタンを押すためのスクリプトを作成しています。長いボタンを押しても問題ありません。長いボタンが押された場合、そのボタンをクリックしないようにしたいのです。UnityはClick Onボタンを押さないでください

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; 
    } 
} 

ボタンスクリプトはブラックボックス化されており、ユニティエンジンに組み込まれているようです。

私は、3D世界へのイベントの伝播を防ぐ方法はあると知っていますが、ボタンスクリプトの伝播を防ぐ方法はありますか?

答えて

0

私はそれを理解しました。イベントシステムの統一的な制限のため、そうするには2つのスクリプトが必要です。まず、長いボタン押下スクリプト:これは完全にモバイル上でテストするためにまだ持っているエディタで働く

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); 
     } 
    } 
} 

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; 
    } 
} 

次に、あなたは古い基本的なボタンを無効にするために新しいボタンのスクリプトが必要になります、潜在的にユニティedidorのオンポイントのクリックが呼び出されますが、それはマウス上から起動することができます...これはイベントの伝播を防ぐことができますこの間、

関連する問題