Created
January 17, 2015 10:41
-
-
Save westhillapps/03e1d7a85856621c478b to your computer and use it in GitHub Desktop.
uGUIのボタン長押し判定
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.EventSystems; | |
public class UGuiLongPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler | |
{ | |
/// <summary> | |
/// 押しっぱなし時に呼び出すイベント | |
/// </summary> | |
public UnityEvent onLongPress = new UnityEvent (); | |
/// <summary> | |
/// 押しっぱなし判定の間隔(この間隔毎にイベントが呼ばれる) | |
/// </summary> | |
public float intervalAction = 0.2f; | |
// 押下開始時にもイベントを呼び出すフラグ | |
public bool callEventFirstPress; | |
// 次の押下判定時間 | |
float nextTime = 0f; | |
/// <summary> | |
/// 押下状態 | |
/// </summary> | |
public bool pressed | |
{ | |
get; | |
private set; | |
} | |
void Update () | |
{ | |
if (pressed && nextTime < Time.realtimeSinceStartup) { | |
onLongPress.Invoke (); | |
nextTime = Time.realtimeSinceStartup + intervalAction; | |
} | |
} | |
public void OnPointerDown (PointerEventData eventData) | |
{ | |
pressed = true; | |
if (callEventFirstPress) { | |
onLongPress.Invoke (); | |
} | |
nextTime = Time.realtimeSinceStartup + intervalAction; | |
} | |
public void OnPointerUp (PointerEventData eventData) | |
{ | |
pressed = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment