Skip to content

Instantly share code, notes, and snippets.

@WestHillApps
Created January 17, 2015 10:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WestHillApps/03e1d7a85856621c478b to your computer and use it in GitHub Desktop.
Save WestHillApps/03e1d7a85856621c478b to your computer and use it in GitHub Desktop.
uGUIのボタン長押し判定
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