/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using TMPro; using UnityEngine.UI; namespace OmiLAXR.Extensions { /// /// Extension methods specifically for Unity's Button components. /// Provides utility methods for button state management and text retrieval. /// public static class ButtonExt { /// /// Enables or disables a button with proper deselection. /// Ensures the button loses focus before changing its interactive state. /// /// The button to modify /// True to disable the button, false to enable it public static void SetDisabled(this Button button, bool flag) { button.OnDeselect(null); button.interactable = !flag; } /// /// Retrieves the text content from a button's TextMeshProUGUI component. /// Searches for a TextMeshProUGUI component in the button's children and returns its text. /// /// The button to get text from /// Text to return if no TextMeshProUGUI component is found /// The button's text content, or the default text if no text component exists public static string GetTextOrDefault(this Button button, string defaultText = "") { var textMesh = button.GetComponentInChildren(); return !textMesh ? defaultText : textMesh.text; } } }