Last active
January 2, 2016 16:19
-
-
Save WestHillApps/8329597 to your computer and use it in GitHub Desktop.
NGUIのボタン押しっぱなし判定用
This file contains 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 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