using System; using UnityEditor; using UnityEngine.UIElements; namespace Unity.Tutorials.Core.Editor { using UnityObject = UnityEngine.Object; static class UIElementsUtils { internal const string k_UIAssetPath = "Packages/com.unity.learn.iet-framework/Editor/UI"; /// /// Sets up a button. /// /// The method that will be called when the button is clicked. /// The text for the butto, if any. /// The tooltip for the button, if any. public static void SetupButton(Button button, Action onClick, string text = null, string tooltip = null) { button.clickable = new Clickable(() => onClick.Invoke()); if (text != null) button.text = text; if (tooltip != null) button.tooltip = tooltip; } /// /// Hides a visual element. /// /// The element to hide public static void Hide(VisualElement element) { SetVisible(element, false); } /// /// Shows a visual element. /// /// The element to show public static void Show(VisualElement element) { SetVisible(element, true); } /// /// Sets visibility of a visual element. /// /// The element to show /// the wanted visibility. public static void SetVisible(VisualElement element, bool visible) { if (element == null) return; // TODO hides programming errors silently, preferably remove element.style.display = visible ? DisplayStyle.Flex : DisplayStyle.None; } /// /// Loads an asset from the common UI resource folder. /// /// type fo the file to load /// name of the file /// A reference to the loaded file internal static T LoadUIAsset(string filename) where T : UnityObject => AssetDatabase.LoadAssetAtPath($"{k_UIAssetPath}/{filename}"); } }