-
Notifications
You must be signed in to change notification settings - Fork 1
/
Btn.cs
69 lines (58 loc) · 1.74 KB
/
Btn.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Btn : MonoBehaviour ,IPointerDownHandler , IPointerUpHandler, IPointerClickHandler
{
[HideInInspector]
public bool Held = false ,Clicked = false , doubleClicked = false;
[SerializeField]
private float delay = 0.7f;
[SerializeField]
Color color , pressedColor = new Color(200,200,200,255);
private Image image;
private Text btnText;
private void Start()
{
image = GetComponent<Image>();
btnText = transform.Find("Text").GetComponent<Text>();
color = image.color;
pressedColor = color * pressedColor;
}
private void Update ()
{
delay -= (Time.deltaTime);
if (Held)
image.color = pressedColor;
else image.color = color;
}
/// <summary>
/// use this to change the button's text with no need to refrencing the text gameobject
/// </summary>
public void ChangeText(string newText) { btnText.text = newText; }
public void OnPointerDown(PointerEventData eventData)
{
Held = true;
if (delay > 0){
doubleClicked = true;
StartCoroutine(reset());
}
else
delay = 0.7f;
}
public void OnPointerUp(PointerEventData eventData) { Held = false; }
public void OnPointerClick(PointerEventData eventData) { Clicked = true; StartCoroutine(reset());}
private IEnumerator reset()
{
if (Clicked)
{
yield return new WaitForEndOfFrame();
Clicked = false;
}
if (doubleClicked)
{
yield return new WaitForEndOfFrame();
doubleClicked = false;
}
}
}