Skip to content

Instantly share code, notes, and snippets.

@WestHillApps
Last active January 2, 2016 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WestHillApps/8329597 to your computer and use it in GitHub Desktop.
Save WestHillApps/8329597 to your computer and use it in GitHub Desktop.
NGUIのボタン押しっぱなし判定用
using UnityEngine;
using System.Collections;
using System;
/// <summary>
/// NGUIのボタン押しっぱなし判定用
/// </summary>
[RequireComponent(typeof(UIButton))]
public class HoldPressBtn : MonoBehaviour
{
// 押しっぱなし判定の間隔(この間隔毎にActionHoldPressが呼ばれる)
[SerializeField]
float intervalAction = 0.2f;
// 押下開始時にもActionHoldPressを呼び出すフラグ
[SerializeField]
bool callActionFirstPress;
// 次の押下判定時間
float nextTime = 0f;
// 押しっぱなし時に呼び出すAction
Action actionHoldPress;
// 押下中フラグ
public bool pressed {
get;
private set;
}
void Update ()
{
if (pressed && nextTime < Time.realtimeSinceStartup) {
nextTime = Time.realtimeSinceStartup + intervalAction;
if (actionHoldPress != null) {
actionHoldPress ();
}
}
}
void OnPress (bool pressed)
{
this.pressed = pressed;
nextTime = Time.realtimeSinceStartup + intervalAction;
if (pressed && callActionFirstPress && actionHoldPress != null) {
actionHoldPress ();
}
}
public void SetActionHoldPress(Action action) {
actionHoldPress = action;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment