Skip to content

Instantly share code, notes, and snippets.

@enpel
Created April 3, 2015 11:50
Show Gist options
  • Save enpel/4bd075fffaed56db724c to your computer and use it in GitHub Desktop.
Save enpel/4bd075fffaed56db724c to your computer and use it in GitHub Desktop.
NGUI UIButton をロングタップ対応
[RequireComponent(typeof(UIButtonLongTapComponent))]
public class ButtonComponent : MonoBehaviour
{
void Awake()
{
this.gameObject.GetComponent<UIButtonLongTapComponent>().SetButtonAction(OnShortTapButton, OnLongTapItemButton, true);
}
public void OnShortTapButton()
{
// Tap時のしょり
}
public void OnLongTapItemButton()
{
// ロングタップ時の処理
}
}
using UnityEngine;
using System.Collections;
using System;
[RequireComponent(typeof(UIButton))]
public class UIButtonLongTapComponent : MonoBehaviour
{
[SerializeField]
float intervalAction = 1.0f;
float firstTapTime = 0f;
bool isHoldAction;
// 押しっぱなし時に呼び出すAction
public Action OnLongTap = null;
public Action OnShortTap = null;
bool isPressing = false;
void Update()
{
if (isPressing && isHoldAction && (firstTapTime + intervalAction < Time.realtimeSinceStartup)) {
isPressing = false;
LongTap ();
}
}
void OnPress (bool pressed)
{
isPressing = pressed;
if (pressed) {
firstTapTime = Time.realtimeSinceStartup;
}
else
{
bool isLongTap = firstTapTime + intervalAction < Time.realtimeSinceStartup;
if (!isHoldAction && isLongTap)
LongTap();
else if(!isLongTap)
ShortTap();
}
}
public void SetButtonAction(Action shortTap, Action longTap, bool isHoldLongTap) {
isHoldAction = isHoldLongTap;
OnLongTap = longTap;
OnShortTap = shortTap;
}
public void CleanAction()
{
OnLongTap = null;
}
public void LongTap()
{
if (OnLongTap != null)
OnLongTap ();
}
public void ShortTap()
{
if (OnShortTap != null)
OnShortTap ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment