| | 1 | | using TMPro; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Used for customize the color of the text and icon of a UI Component. |
| | 7 | | /// </summary> |
| | 8 | | [RequireComponent(typeof(BaseComponentView))] |
| | 9 | | public class UIHelper_ChangeTextAndIconColorOnFocus : MonoBehaviour |
| | 10 | | { |
| | 11 | | [SerializeField] internal TMP_Text textToChange; |
| | 12 | | [SerializeField] internal Image iconToChange; |
| | 13 | | [SerializeField] internal Color onFocusColor; |
| | 14 | | [SerializeField] internal Color onLoseFocusColor; |
| | 15 | |
|
| | 16 | | internal BaseComponentView componentView; |
| | 17 | |
|
| | 18 | | private void Awake() |
| | 19 | | { |
| 2665 | 20 | | componentView = GetComponent<BaseComponentView>(); |
| 2665 | 21 | | } |
| | 22 | |
|
| | 23 | | private void Start() |
| | 24 | | { |
| 1 | 25 | | componentView.onFocused += ComponentView_onFocused; |
| 1 | 26 | | ComponentView_onFocused(componentView.isFocused); |
| 1 | 27 | | } |
| | 28 | |
|
| | 29 | | private void OnDestroy() |
| | 30 | | { |
| 2664 | 31 | | componentView.onFocused -= ComponentView_onFocused; |
| 2664 | 32 | | } |
| | 33 | |
|
| | 34 | | internal void ComponentView_onFocused(bool isFocused) |
| | 35 | | { |
| 3 | 36 | | if (textToChange != null) |
| 3 | 37 | | textToChange.color = isFocused ? onFocusColor : onLoseFocusColor; |
| | 38 | |
|
| 3 | 39 | | if (iconToChange != null) |
| 3 | 40 | | iconToChange.color = isFocused ? onFocusColor : onLoseFocusColor; |
| 3 | 41 | | } |
| | 42 | | } |