Skip to content

Instantly share code, notes, and snippets.

@0V
Last active May 20, 2017 11:24
Show Gist options
  • Save 0V/d8aaf2bd9881af1e2bb48a9a1ed6bb74 to your computer and use it in GitHub Desktop.
Save 0V/d8aaf2bd9881af1e2bb48a9a1ed6bb74 to your computer and use it in GitHub Desktop.
Unity(uGUI)で N 回クリックするとオブジェクトをアクティブにするスクリプト
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class SecretClickHandler : MonoBehaviour, IPointerClickHandler
{
[SerializeField]
private ClickEvent clickHandler;
[SerializeField]
private GameObject secretObject;
[SerializeField]
private int activeCount = 10;
[SerializeField]
private float clickInterval = 0.75f;
private float lastTimeClick;
private int clickCount = 0;
public void OnPointerClick(PointerEventData eventData)
{
clickCount++;
float currentTimeClick = eventData.clickTime;
if (Mathf.Abs(currentTimeClick - lastTimeClick) >= clickInterval)
{
clickCount = 1;
}
lastTimeClick = currentTimeClick;
// 10回連続クリック
if (clickCount >= activeCount)
{
// ここにしたい処理を書く
secretObject.SetActive(true);
}
}
public void AddClickHandler(UnityAction<GameObject> handler)
{
this.clickHandler.AddListener(handler);
}
[Serializable]
public class ClickEvent : UnityEvent<GameObject> { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment