Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
OrganizationEventAdvent CalendarQiitadon (β)
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

NGUI UIButtonをロングタップ対応

using UnityEngine;
using System.Collections;
using System;

public class UIButtonLongTapComponent : MonoBehaviour

    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;
            bool isLongTap = firstTapTime + intervalAction < Time.realtimeSinceStartup;
            if (!isHoldAction && isLongTap)
            else if(!isLongTap)

    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 ();


Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Help us understand the problem. What are the problem?