Last active
May 20, 2017 11:33
-
-
Save 0V/b6629519bed7627f616228b1abbc4b85 to your computer and use it in GitHub Desktop.
uGUIで指定回数連続クリックされたら何かをするスクリプト
This file contains hidden or 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 System; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.EventSystems; | |
public class CounterClickHandler : MonoBehaviour, IPointerClickHandler | |
{ | |
[SerializeField] | |
private ClickEvent clickHandler; | |
[SerializeField] | |
private int activeCount = 10; | |
[SerializeField] | |
private float clickInterval = 0.75f; | |
[SerializeField] | |
private int clickCount = 0; | |
private float lastTimeClick; | |
public void OnPointerClick(PointerEventData eventData) | |
{ | |
clickCount++; | |
float currentTimeClick = eventData.clickTime; | |
if (Mathf.Abs(currentTimeClick - lastTimeClick) >= clickInterval) | |
{ | |
clickCount = 1; | |
} | |
lastTimeClick = currentTimeClick; | |
// activeCount回連続クリックした時 | |
if (clickCount >= activeCount) | |
{ | |
// ここにしたい処理を書く | |
} | |
} | |
public void AddClickHandler(UnityAction<GameObject> handler) | |
{ | |
this.clickHandler.AddListener(handler); | |
} | |
[Serializable] | |
public class ClickEvent : UnityEvent<GameObject> { } | |
} |
This file contains hidden or 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 System; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.EventSystems; | |
public class CounterClickHandler : MonoBehaviour, IPointerClickHandler | |
{ | |
[SerializeField] | |
private ClickEvent clickHandler; | |
[SerializeField] | |
private int activeCount = 10; | |
[SerializeField] | |
private float clickInterval = 0.75f; | |
[SerializeField] | |
private int clickCount = 0; | |
public bool IsDone { get; set; } | |
private float lastTimeClick; | |
private void Start(){ | |
IsDone = false; | |
} | |
public void OnPointerClick(PointerEventData eventData) | |
{ | |
clickCount++; | |
float currentTimeClick = eventData.clickTime; | |
if (Mathf.Abs(currentTimeClick - lastTimeClick) >= clickInterval) | |
{ | |
clickCount = 1; | |
} | |
lastTimeClick = currentTimeClick; | |
// activeCount回連続クリックした時 | |
if (!IsDone && clickCount >= activeCount) | |
{ | |
IsDone = 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