using System.Collections.Generic; using System; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.EventSystems; public class UISelectableEntry : EventTrigger { private List children; private List textContent; private UIDialog parentDialog; private RectTransform rectTransform; private CanvasRenderer canvasRenderer; private Image image; private TextMeshProUGUI textHandle; private bool isSelectable = true; private static Color baseColor = new Color32(31, 31, 45, 140); private Action OnClickCallback; private object callbackArgument; public void Awake() { rectTransform = gameObject.AddComponent(); textHandle = Instantiate(Resources.Load("Prefabs/EmptyObject"), rectTransform).AddComponent(); textHandle.gameObject.name = "UISelectableEntry::Text"; textHandle.fontSize = 12; textHandle.font = UIManager.Instance.Font; textHandle.alignment = TextAlignmentOptions.MidlineLeft; textHandle.GetComponent().anchorMin = new Vector2(0, 0.5f); textHandle.GetComponent().anchorMax = new Vector2(1, 0.5f); textHandle.GetComponent().pivot = new Vector2(0.5f, 0.5f); textHandle.GetComponent().anchoredPosition = new Vector2(0, 0); textHandle.GetComponent().sizeDelta = new Vector2(0, 20); image = gameObject.AddComponent(); image.type = Image.Type.Sliced; image.color = baseColor; } public void SetClickCallback(Action callback, object argument) { OnClickCallback = callback; callbackArgument = argument; } public override void OnPointerEnter(PointerEventData eventData) { if(isSelectable) image.color = baseColor + new Color32(20, 20, 20, 40); base.OnPointerEnter(eventData); } public override void OnPointerDown(PointerEventData eventData) { if(isSelectable && OnClickCallback != null) { OnClickCallback(callbackArgument); image.color = baseColor + new Color32(40, 40, 40, 40); } base.OnPointerClick(eventData); } public override void OnPointerExit(PointerEventData eventData) { if(isSelectable) image.color = baseColor; base.OnPointerExit(eventData); } public void SetIsSelectable(bool isSelectable) { if(isSelectable) image.enabled = true; else image.enabled = false; this.isSelectable = isSelectable; } public bool GetIsSelectable() { return isSelectable; } public void AddChildEntry(UISelectableEntry child) { if (children == null) { children = new List(); } children.Add(child); } public void SetParent(UIDialog parentDialog) { this.parentDialog = parentDialog; } public void SetChildEntries(List children) { this.children = children; } // Get the children of this entry public List GetChildEntries() { return children; } public void SetTextContent(List textContent) { this.textContent = textContent; textHandle.text = string.Join("\t", textContent); } public RectTransform GetTextTransform() { return textHandle.GetComponent(); } }