| | 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 | | private bool isOpen; |
| 0 | 24 | | private void OnEnable() { |
| 0 | 25 | | isOpen = true; |
| 0 | 26 | | } |
| 0 | 27 | | private void OnDisable() { |
| 0 | 28 | | isOpen = false; |
| 0 | 29 | | } |
| | 30 | |
|
| 0 | 31 | | public void ToggleWindow() { |
| 0 | 32 | | gameObject.SetActive(!gameObject.activeSelf); |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public void CloseWindow() { |
| 0 | 36 | | Destroy(gameObject); |
| 0 | 37 | | isOpen = false; |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | /// Called when the UIWindow component is created in the editor. |
| | 41 | | /// This is used to configure the image component. |
| 0 | 42 | | private void Reset() { |
| 0 | 43 | | GetComponent<Image>().color = new Color32(18, 16, 28, 125); |
| 0 | 44 | | } |
| | 45 | |
|
| 0 | 46 | | public virtual void Start() { |
| 0 | 47 | | isOpen = gameObject.activeSelf; |
| 0 | 48 | | CreateCloseButton(); |
| 0 | 49 | | CreateWindowTitle(); |
| 0 | 50 | | if (closeButtonCallback == CloseButtonCallback.CLOSE_WINDOW) |
| 0 | 51 | | closeButton.AddComponent<Button>().onClick.AddListener(CloseWindow); |
| 0 | 52 | | else if (closeButtonCallback == CloseButtonCallback.TOGGLE_WINDOW) |
| 0 | 53 | | closeButton.AddComponent<Button>().onClick.AddListener(ToggleWindow); |
| 0 | 54 | | } |
| | 55 | |
|
| 0 | 56 | | private void CreateWindowTitle() { |
| 0 | 57 | | GameObject windowTitleObject = new GameObject("WindowTitle", typeof(RectTransform)); |
| 0 | 58 | | windowTitleObject.transform.SetParent(transform); |
| 0 | 59 | | TextMeshProUGUI windowTitleHandle = windowTitleObject.AddComponent<TextMeshProUGUI>(); |
| 0 | 60 | | windowTitleHandle.text = windowTitle; |
| 0 | 61 | | windowTitleHandle.font = UIManager.Instance.GlobalFont; |
| 0 | 62 | | windowTitleHandle.fontSize = 14; |
| 0 | 63 | | windowTitleHandle.color = Color.white; |
| 0 | 64 | | windowTitleHandle.alignment = TextAlignmentOptions.Left; |
| 0 | 65 | | windowTitleHandle.rectTransform.anchorMin = new Vector2(0, 1); |
| 0 | 66 | | windowTitleHandle.rectTransform.anchorMax = new Vector2(1, 1); |
| 0 | 67 | | windowTitleHandle.rectTransform.pivot = new Vector2(0, 1); |
| 0 | 68 | | windowTitleHandle.rectTransform.sizeDelta = new Vector2(0, 30); |
| 0 | 69 | | windowTitleHandle.rectTransform.anchoredPosition = new Vector2(5, 0); |
| 0 | 70 | | windowTitleHandle.rectTransform.SetRight(30); // Give spacing to the close button |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | /// Create the close [x] button in the top right corner of the window. |
| 0 | 74 | | private void CreateCloseButton() { |
| 0 | 75 | | closeButton = new GameObject("CloseButton", typeof(RectTransform)); |
| 0 | 76 | | RectTransform buttonTransform = closeButton.GetComponent<RectTransform>(); |
| 0 | 77 | | buttonTransform.SetParent(transform); |
| | 78 | | // Anchor top right. |
| 0 | 79 | | buttonTransform.anchorMin = new Vector2(1, 1); |
| 0 | 80 | | buttonTransform.anchorMax = new Vector2(1, 1); |
| 0 | 81 | | buttonTransform.pivot = new Vector2(1, 1); |
| | 82 | | // Position top right. |
| 0 | 83 | | buttonTransform.anchoredPosition = new Vector2(0, 0); |
| | 84 | | // Size. |
| 0 | 85 | | buttonTransform.sizeDelta = new Vector2(30, 30); |
| | 86 | | // Add button component. |
| 0 | 87 | | TextMeshProUGUI textbox = closeButton.AddComponent<TextMeshProUGUI>(); |
| 0 | 88 | | textbox.text = "X"; |
| 0 | 89 | | textbox.font = UIManager.Instance.GlobalFont; |
| 0 | 90 | | textbox.fontSize = 12; |
| 0 | 91 | | textbox.alignment = TextAlignmentOptions.Center; |
| 0 | 92 | | textbox.verticalAlignment = VerticalAlignmentOptions.Middle; |
| 0 | 93 | | } |
| | 94 | | } |