| | 1 | | using UnityEngine; |
| | 2 | |
|
| | 3 | | /// <summary> |
| | 4 | | /// Attaching this component to a canvas, will hide/show it after triggering the ToggleUIVisibility input action. |
| | 5 | | /// </summary> |
| | 6 | | [RequireComponent(typeof(CanvasGroup))] |
| | 7 | | public class ShowHideUIByTrigger : MonoBehaviour |
| | 8 | | { |
| | 9 | | private CanvasGroup canvasGroup; |
| | 10 | | private ShowHideAnimator showHideAnimator; |
| | 11 | | private Animator animator; |
| | 12 | |
|
| | 13 | | private void Awake() |
| | 14 | | { |
| 868 | 15 | | canvasGroup = GetComponent<CanvasGroup>(); |
| 868 | 16 | | showHideAnimator = GetComponent<ShowHideAnimator>(); |
| 868 | 17 | | animator = GetComponent<Animator>(); |
| 868 | 18 | | CommonScriptableObjects.allUIHidden.OnChange += AllUIVisible_OnChange; |
| 868 | 19 | | SetUIVisibility(!CommonScriptableObjects.allUIHidden.Get()); |
| 868 | 20 | | } |
| | 21 | |
|
| 1736 | 22 | | private void OnDestroy() { CommonScriptableObjects.allUIHidden.OnChange -= AllUIVisible_OnChange; } |
| | 23 | |
|
| 48 | 24 | | private void AllUIVisible_OnChange(bool current, bool previous) { SetUIVisibility(!current); } |
| | 25 | |
|
| | 26 | | private void SetUIVisibility(bool isVisible) |
| | 27 | | { |
| 892 | 28 | | if (showHideAnimator != null) |
| 214 | 29 | | showHideAnimator.enabled = isVisible; |
| | 30 | |
|
| 892 | 31 | | if (animator != null) |
| 214 | 32 | | animator.enabled = isVisible; |
| | 33 | |
|
| 892 | 34 | | canvasGroup.alpha = isVisible ? 1f : 0f; |
| 892 | 35 | | canvasGroup.interactable = isVisible; |
| 892 | 36 | | canvasGroup.blocksRaycasts = isVisible; |
| 892 | 37 | | } |
| | 38 | | } |