Tanoda
UIHighlightable.cs
Go to the documentation of this file.
1
3
5
7{
8 [AddComponentMenu("UI/Extensions/UI Highlightable Extension")]
9 [RequireComponent(typeof(RectTransform), typeof(Graphic))]
10 public class UIHighlightable : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
11 {
12 private Graphic m_Graphic;
13 private bool m_Highlighted;
14 private bool m_Pressed;
15
16 [System.Serializable]
17 public class InteractableChangedEvent : Events.UnityEvent<bool> { }
18
19 [SerializeField][Tooltip("Can this panel be interacted with or is it disabled? (does not affect child components)")]
20 private bool m_Interactable = true;
21 [SerializeField][Tooltip("Does the panel remain in the pressed state when clicked? (default false)")]
22 private bool m_ClickToHold;
23
24 public bool Interactable
25 {
26 get { return m_Interactable; }
27 set
28 {
29 m_Interactable = value;
30 HighlightInteractable(m_Graphic);
31 OnInteractableChanged.Invoke(m_Interactable);
32 }
33 }
34
35 public bool ClickToHold
36 {
37 get { return m_ClickToHold; }
38 set { m_ClickToHold = value; }
39 }
40
41 [Tooltip("The default color for the panel")]
42 public Color NormalColor = Color.grey;
43 [Tooltip("The color for the panel when a mouse is over it or it is in focus")]
44 public Color HighlightedColor = Color.yellow;
45 [Tooltip("The color for the panel when it is clicked/held")]
46 public Color PressedColor = Color.green;
47 [Tooltip("The color for the panel when it is not interactable (see Interactable)")]
48 public Color DisabledColor = Color.gray;
49
50 [Tooltip("Event for when the panel is enabled / disabled, to enable disabling / enabling of child or other gameobjects")]
52
53 void Awake()
54 {
55 m_Graphic = GetComponent<Graphic>();
56 }
57
58 public void OnPointerEnter(PointerEventData eventData)
59 {
60 if (Interactable && !m_Pressed)
61 {
62 m_Highlighted = true;
63 m_Graphic.color = HighlightedColor;
64 }
65 }
66
67 public void OnPointerExit(PointerEventData eventData)
68 {
69 if (Interactable && !m_Pressed)
70 {
71 m_Highlighted = false;
72 m_Graphic.color = NormalColor;
73 }
74 }
75
76 public void OnPointerDown(PointerEventData eventData)
77 {
78 if (Interactable)
79 {
80 m_Graphic.color = PressedColor;
81 if (ClickToHold)
82 {
83 m_Pressed = !m_Pressed;
84 }
85 }
86 }
87
88 public void OnPointerUp(PointerEventData eventData)
89 {
90 if(!m_Pressed) HighlightInteractable(m_Graphic);
91 }
92
93 private void HighlightInteractable(Graphic graphic)
94 {
95 if (m_Interactable)
96 {
97 if (m_Highlighted)
98 {
99 graphic.color = HighlightedColor;
100 }
101 else
102 {
103 graphic.color = NormalColor;
104 }
105 }
106 else
107 {
108 graphic.color = DisabledColor;
109 }
110 }
111
112#if UNITY_EDITOR
113 private void OnValidate()
114 {
115 HighlightInteractable(GetComponent<Graphic>());
116 }
117#endif
118 }
119}
UnityEngine.Color Color
Definition: TestScript.cs:32
void OnPointerExit(PointerEventData eventData)
void OnPointerDown(PointerEventData eventData)
void OnPointerEnter(PointerEventData eventData)
InteractableChangedEvent OnInteractableChanged
void OnPointerUp(PointerEventData eventData)
Credit Erdener Gonenc - @PixelEnvision.