| | 1 | | using System.Collections.Generic; |
| | 2 | | using System; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine.EventSystems; |
| | 7 | |
|
| | 8 | | [RequireComponent(typeof(UIElementMouseCapturer))] |
| | 9 | | [RequireComponent(typeof(UIElementDragger))] |
| | 10 | | [RequireComponent(typeof(Image))] |
| | 11 | | public class UIWindow : MonoBehaviour { |
| | 12 | | // Window title |
| | 13 | | [SerializeField] |
| 0 | 14 | | private string windowTitle = "Window"; |
| | 15 | |
|
| | 16 | | // Close button |
| | 17 | | private GameObject closeButton; |
| | 18 | | [SerializeField] |
| | 19 | | private CloseButtonCallback closeButtonCallback; |
| | 20 | | [Serializable] |
| | 21 | | private enum CloseButtonCallback { CLOSE_WINDOW, TOGGLE_WINDOW } |
| | 22 | |
|
| | 23 | | // IsOpen property |
| | 24 | | private bool isOpen; |
| 0 | 25 | | private void OnEnable() { |
| 0 | 26 | | isOpen = true; |
| 0 | 27 | | } |
| 0 | 28 | | private void OnDisable() { |
| 0 | 29 | | isOpen = false; |
| 0 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | public void ToggleWindow() { |
| 0 | 33 | | gameObject.SetActive(!gameObject.activeSelf); |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | public void CloseWindow() { |
| 0 | 37 | | Destroy(gameObject); |
| 0 | 38 | | isOpen = false; |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Called when the UIWindow component is created in the editor |
| | 43 | | /// We will use it to configure the image component |
| | 44 | | /// </summary> |
| 0 | 45 | | private void Reset() { |
| | 46 | | // 18 16 28 125 |
| 0 | 47 | | GetComponent<Image>().color = new Color32(18, 16, 28, 125); |
| 0 | 48 | | } |
| | 49 | |
|
| 0 | 50 | | public virtual void Start() { |
| 0 | 51 | | isOpen = gameObject.activeSelf; |
| 0 | 52 | | CreateCloseButton(); |
| 0 | 53 | | CreateWindowTitle(); |
| 0 | 54 | | if (closeButtonCallback == CloseButtonCallback.CLOSE_WINDOW) |
| 0 | 55 | | closeButton.AddComponent<Button>().onClick.AddListener(CloseWindow); |
| 0 | 56 | | else if (closeButtonCallback == CloseButtonCallback.TOGGLE_WINDOW) |
| 0 | 57 | | closeButton.AddComponent<Button>().onClick.AddListener(ToggleWindow); |
| 0 | 58 | | } |
| | 59 | |
|
| 0 | 60 | | private void CreateWindowTitle() { |
| 0 | 61 | | GameObject windowTitleObject = new GameObject("WindowTitle", typeof(RectTransform)); |
| 0 | 62 | | windowTitleObject.transform.SetParent(transform); |
| 0 | 63 | | TextMeshProUGUI windowTitleHandle = windowTitleObject.AddComponent<TextMeshProUGUI>(); |
| 0 | 64 | | windowTitleHandle.text = windowTitle; |
| 0 | 65 | | windowTitleHandle.font = UIManager.Instance.Font; |
| 0 | 66 | | windowTitleHandle.fontSize = 14; |
| 0 | 67 | | windowTitleHandle.color = Color.white; |
| 0 | 68 | | windowTitleHandle.alignment = TextAlignmentOptions.Left; |
| 0 | 69 | | windowTitleHandle.rectTransform.anchorMin = new Vector2(0, 1); |
| 0 | 70 | | windowTitleHandle.rectTransform.anchorMax = new Vector2(1, 1); |
| 0 | 71 | | windowTitleHandle.rectTransform.pivot = new Vector2(0, 1); |
| 0 | 72 | | windowTitleHandle.rectTransform.sizeDelta = new Vector2(0, 30); |
| 0 | 73 | | windowTitleHandle.rectTransform.anchoredPosition = new Vector2(5, 0); |
| 0 | 74 | | windowTitleHandle.rectTransform.SetRight(30); // Give spacing to the close button |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Create the close [x] button in the top right corner of the window |
| | 79 | | /// </summary> |
| 0 | 80 | | private void CreateCloseButton() { |
| 0 | 81 | | closeButton = new GameObject("CloseButton", typeof(RectTransform)); |
| 0 | 82 | | RectTransform buttonTransform = closeButton.GetComponent<RectTransform>(); |
| 0 | 83 | | buttonTransform.SetParent(transform); |
| | 84 | | // anchor top right |
| 0 | 85 | | buttonTransform.anchorMin = new Vector2(1, 1); |
| 0 | 86 | | buttonTransform.anchorMax = new Vector2(1, 1); |
| 0 | 87 | | buttonTransform.pivot = new Vector2(1, 1); |
| | 88 | | // position top right |
| 0 | 89 | | buttonTransform.anchoredPosition = new Vector2(0, 0); |
| | 90 | | // size |
| 0 | 91 | | buttonTransform.sizeDelta = new Vector2(30, 30); |
| | 92 | | // add button component |
| 0 | 93 | | TextMeshProUGUI textbox = closeButton.AddComponent<TextMeshProUGUI>(); |
| 0 | 94 | | textbox.text = "X"; |
| 0 | 95 | | textbox.font = UIManager.Instance.Font; |
| 0 | 96 | | textbox.fontSize = 12; |
| 0 | 97 | | textbox.alignment = TextAlignmentOptions.Center; |
| 0 | 98 | | textbox.verticalAlignment = VerticalAlignmentOptions.Middle; |
| 0 | 99 | | } |
| | 100 | | } |