Tanoda
UISelectableExtension.cs
Go to the documentation of this file.
1
6
9
11{
15 [AddComponentMenu("UI/Extensions/UI Selectable Extension")]
16 [RequireComponent(typeof(Selectable))]
17 public class UISelectableExtension : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
18 {
19 #region Sub-Classes
20 [System.Serializable]
21 public class UIButtonEvent : UnityEvent<PointerEventData.InputButton> { }
22 #endregion
23
24 #region Events
25 [Tooltip("Event that fires when a button is initially pressed down")]
27 [Tooltip("Event that fires when a button is released")]
29 [Tooltip("Event that continually fires while a button is held down")]
31 #endregion
32
33 private bool _pressed;
34 private PointerEventData _heldEventData;
35
36 void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
37 {
38 //Can't set the state as it's too locked down.
39 //DoStateTransition(SelectionState.Pressed, false);
40
41 if (OnButtonPress != null)
42 {
43 OnButtonPress.Invoke(eventData.button);
44 }
45 _pressed = true;
46 _heldEventData = eventData;
47 }
48
49
50 void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
51 {
52 //DoStateTransition(SelectionState.Normal, false);
53
54 if (OnButtonRelease != null)
55 {
56 OnButtonRelease.Invoke(eventData.button);
57 }
58 _pressed = false;
59 _heldEventData = null;
60 }
61
62 void Update()
63 {
64 if (!_pressed)
65 return;
66
67 if (OnButtonHeld != null)
68 {
69 OnButtonHeld.Invoke(_heldEventData.button);
70 }
71 }
72
76 public void TestClicked()
77 {
78 #if DEBUG || UNITY_EDITOR
79 Debug.Log("Control Clicked");
80 #endif
81 }
82
86 public void TestPressed()
87 {
88 #if DEBUG || UNITY_EDITOR
89 Debug.Log("Control Pressed");
90 #endif
91 }
92
96 public void TestReleased()
97 {
98 #if DEBUG || UNITY_EDITOR
99 Debug.Log("Control Released");
100 #endif
101 }
102
106 public void TestHold()
107 {
108 #if DEBUG || UNITY_EDITOR
109 Debug.Log("Control Held");
110 #endif
111 }
112
113 //Fixed UISelectableExtension inactive bug (if gameObject becomes inactive while button is held down it never goes back to _pressed = false)
114 void OnDisable()
115 {
116 _pressed = false;
117 }
118 }
119}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void TestHold()
est method to verify if a control is being held
void TestReleased()
est method to verify if a control is released
void TestClicked()
Test method to verify a control has been clicked
void TestPressed()
Test method to verify a control is pressed
Credit Erdener Gonenc - @PixelEnvision.