| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Creates a fullscreen blocker above all UIs. |
| | 8 | | /// It will ignore the specific UIs set in the 'nonBlockingCanvas' list. |
| | 9 | | /// </summary> |
| | 10 | | public class UIHelper_ClickBlocker : MonoBehaviour |
| | 11 | | { |
| | 12 | | [Header("Prefab References")] |
| | 13 | | [SerializeField] internal Canvas blockerCanvas; |
| | 14 | | [SerializeField] internal RectTransform blockerTransform; |
| | 15 | | [SerializeField] internal Button blockerButton; |
| | 16 | |
|
| | 17 | | [Header("Optional Settings")] |
| | 18 | | [SerializeField] internal List<Canvas> nonBlockingCanvas; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Event that will be triggered when the blocker is clicked. |
| | 22 | | /// </summary> |
| | 23 | | public event Action OnClicked; |
| | 24 | |
|
| | 25 | | internal Canvas parentcanvas = null; |
| | 26 | |
|
| | 27 | | private void Awake() |
| | 28 | | { |
| 4 | 29 | | blockerButton.onClick.AddListener(() => OnClicked?.Invoke()); |
| 4 | 30 | | } |
| | 31 | |
|
| | 32 | | private void OnDestroy() |
| | 33 | | { |
| 4 | 34 | | blockerButton.onClick.RemoveAllListeners(); |
| 4 | 35 | | } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Activates the blocker. |
| | 39 | | /// </summary> |
| | 40 | | public void Activate() |
| | 41 | | { |
| 4 | 42 | | if (parentcanvas == null) |
| 4 | 43 | | parentcanvas = GetComponentInParent<Canvas>(); |
| | 44 | |
|
| 4 | 45 | | blockerTransform.SetParent(parentcanvas.rootCanvas.transform); |
| | 46 | |
|
| 4 | 47 | | blockerTransform.offsetMax = Vector2.zero; |
| 4 | 48 | | blockerTransform.offsetMin = Vector2.zero; |
| 4 | 49 | | gameObject.SetActive(true); |
| 24 | 50 | | foreach (Canvas canvas in nonBlockingCanvas) |
| | 51 | | { |
| 8 | 52 | | canvas.overrideSorting = true; |
| 8 | 53 | | canvas.sortingOrder = blockerCanvas.sortingOrder + 1; |
| | 54 | | } |
| 4 | 55 | | } |
| | 56 | |
|
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Deactivates the blocker. |
| | 60 | | /// </summary> |
| | 61 | | public void Deactivate() |
| | 62 | | { |
| 403 | 63 | | gameObject.SetActive(false); |
| 2418 | 64 | | foreach (Canvas canvas in nonBlockingCanvas) |
| | 65 | | { |
| 806 | 66 | | canvas.overrideSorting = false; |
| | 67 | | } |
| 403 | 68 | | } |
| | 69 | | } |