-
-
Save NicolasChicunque/c2512380b1732d50e75fac4574a44b26 to your computer and use it in GitHub Desktop.
Unity 6.3 Custom Main Toolbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEditor; | |
| using UnityEngine; | |
| using UnityEngine.UIElements; | |
| public static class MainToolbarElementStyler | |
| { | |
| public static void StyleElement<T>(string elementName, System.Action<T> styleAction) where T : VisualElement | |
| { | |
| EditorApplication.delayCall += () => | |
| { | |
| ApplyStyle(elementName, (element) => | |
| { | |
| T targetElement = element is T typedElement ? typedElement : element.Query<T>().First(); | |
| if (targetElement == null) return; | |
| styleAction(targetElement); | |
| }); | |
| }; | |
| } | |
| static void ApplyStyle(string elementName, System.Action<VisualElement> styleCallback) | |
| { | |
| var element = FindElementByName(elementName); | |
| if (element == null) return; | |
| styleCallback(element); | |
| } | |
| static VisualElement FindElementByName(string name) | |
| { | |
| var windows = Resources.FindObjectsOfTypeAll<EditorWindow>(); | |
| foreach (var window in windows) | |
| { | |
| var root = window.rootVisualElement; | |
| if (root == null) continue; | |
| VisualElement element; | |
| if ((element = root.Q<VisualElement>(name)) != null) return element; | |
| if ((element = root.Query<VisualElement>().Where(e => e.tooltip == name).First()) != null) return element; | |
| } | |
| return null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEditor.Toolbars; | |
| using UnityEngine; | |
| using UnityEngine.UIElements; | |
| public class MainToolbarTimescaleSlider | |
| { | |
| const float k_minTimeScale = 0f, k_maxTimeScale = 5f; | |
| [MainToolbarElement("Timescale/Slider", defaultDockPosition = MainToolbarDockPosition.Middle)] | |
| public static MainToolbarElement TimeSlider() | |
| { | |
| MainToolbarElementStyler.StyleElement<VisualElement>("Timescale/Slider", (element) => | |
| { | |
| element.style.paddingLeft = 10f; | |
| }); | |
| return new MainToolbarSlider( | |
| new MainToolbarContent("Time Scale", "Time Scale"),//Content(Text, Tooltip) | |
| Time.timeScale,//Value | |
| k_minTimeScale,//Min Value | |
| k_maxTimeScale,//Max Value | |
| value => Time.timeScale = value//Action | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment