using System.Collections; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; using TMPro; namespace Michsky.UI.Heat { [ExecuteInEditMode] [DisallowMultipleComponent] public class PanelButton : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IDeselectHandler, ISubmitHandler { // Content public Sprite buttonIcon; public string buttonText = "Button"; // Resources [SerializeField] private CanvasGroup disabledCG; [SerializeField] private CanvasGroup normalCG; [SerializeField] private CanvasGroup highlightCG; [SerializeField] private CanvasGroup selectCG; [SerializeField] private TextMeshProUGUI disabledTextObj; [SerializeField] private TextMeshProUGUI normalTextObj; [SerializeField] private TextMeshProUGUI highlightTextObj; [SerializeField] private TextMeshProUGUI selectTextObj; [SerializeField] private Image disabledImageObj; [SerializeField] private Image normalImageObj; [SerializeField] private Image highlightImageObj; [SerializeField] private Image selectedImageObj; [SerializeField] private GameObject seperator; // Settings public bool isInteractable = true; public bool isSelected; public bool useLocalization = true; public bool useCustomText = false; public bool useSeperator = true; public bool useUINavigation = false; public Navigation.Mode navigationMode = Navigation.Mode.Automatic; public GameObject selectOnUp; public GameObject selectOnDown; public GameObject selectOnLeft; public GameObject selectOnRight; public bool wrapAround = false; public bool useSounds = true; [Range(1, 15)] public float fadingMultiplier = 8; // Events public UnityEvent onClick = new UnityEvent(); public UnityEvent onHover = new UnityEvent(); public UnityEvent onLeave = new UnityEvent(); public UnityEvent onSelect = new UnityEvent(); // Helpers bool isInitialized = false; Button targetButton; LocalizedObject localizedObject; [HideInInspector] public NavigationBar navbar; void OnEnable() { if (!isInitialized) { Initialize(); } UpdateUI(); } void Initialize() { if (!Application.isPlaying) { return; } if (UIManagerAudio.instance == null) { useSounds = false; } if (useUINavigation) { AddUINavigation(); } if (gameObject.GetComponent() == null) { Image raycastImg = gameObject.AddComponent(); raycastImg.color = new Color(0, 0, 0, 0); raycastImg.raycastTarget = true; } disabledCG.alpha = 0; normalCG.alpha = 1; highlightCG.alpha = 0; selectCG.alpha = 0; if (useLocalization) { localizedObject = gameObject.GetComponent(); if (localizedObject == null || !localizedObject.CheckLocalizationStatus()) { useLocalization = false; } else if (useLocalization && !string.IsNullOrEmpty(localizedObject.localizationKey)) { // Forcing button to take the localized output on awake buttonText = localizedObject.GetKeyOutput(localizedObject.localizationKey); // Change button text on language change localizedObject.onLanguageChanged.AddListener(delegate { buttonText = localizedObject.GetKeyOutput(localizedObject.localizationKey); UpdateUI(); }); } } isInitialized = true; } public void IsInteractable(bool value) { isInteractable = value; if (!isInteractable) { StartCoroutine("SetDisabled"); } else if (isInteractable && !isSelected) { StartCoroutine("SetNormal"); } } public void AddUINavigation() { if (targetButton == null) { targetButton = gameObject.AddComponent