Odin Inspector can now render Visual Elements (UI Toolkit) in ImGUI! We're inviting you to test this early Alpha Preview version and share your feedback.
Keep in mind that the current minimum Unity version for this preview build to run at all is Unity 2020.3, though there is a small chance it may work in 2020.2 (we have not tested this). It should also support the latest 2023.x Unity beta and alpha versions just fine. Later preview versions should support down to the current base requirement of Unity 2019.4, though the UI Toolkit integration will not function on any version lower than 2020.2 (estimated).
This new feature is made possible by our OdinImGuiElement integration, allowing you to draw visual elements inside ImGUI from anywhere, even in non-Odin editors. You can also utilize this API manually in your own ImGUI code to embed arbitrary visual elements anywhere:
using Sirenix.OdinInspector.Editor.Internal.UIToolkitIntegration;
// Your element goes in the constructor.
// You can also give it a serializedProperty.
private OdinImGuiElement SomeElement = new OdinImGuiElement(new ListView());
private void OnGUI()
{
GUILayout.BeginVertical();
var rect = ImguiElementUtils.EmbedVisualElementAndDrawItHere(this.SomeElement);
// Or optionally with a label:
//var rect = ImguiElementUtils.EmbedVisualElementAndDrawItHere(this.SomeElement, new GUIContent("Custom label"));
GUILayout.EndVertical();
}
In addition to the manual API above, Odin will now draw any Unity PropertyDrawer, DecoratorDrawer and inlined Editor as an embedded VisualElement if it provides one, with a fallback to normal ImGUI. You can also force a property to be drawn with visual elements by applying the new DrawWithVisualElements attribute to it.
Keep in mind that it's an alpha preview; there will probably be some edge case stability issues, and we'll probably change the API and namespaces around a bit before any official release.
Please report issues or give any other feedback in the Discussion/Feedback thread linked below. These are the currently known issues:
Thank you for your support and input as we work on this feature!
Download | Discussion & FeedbackIntroducing enhanced number interaction with Unit and smart fields in Odin Inspector.
The new Unit attribute allows you to define a field's value, such as meters, and easily switch units without changing the underlying code. You can even input values in other units, and the inspector will automatically convert them. With over 200 built-in units, Unit and Smart Fields offer a more flexible way of handling numerical values.
The Unit system also comes with a new Unit Overview window, allowing overview of all units in your system, including custom units you've added. It's accessible from the Odin menu, or by right clicking a unit field in the inspector.
Smart fields enable scripting directly into a field's text box using Odin expressions by starting with an @ character. They also maintain a history of your entries for quick navigation.
// A meters field. The user can enter other values and convert them to meters, or change the display on the fly.
[Unit(Units.Meter)]
public float Meters;
// Radians field shown in degrees. The user can still enter other values and change the display as they want.
[Unit(Units.Radian, Units.Degree)]
public float RadiansDisplayedAsDegrees;
// You can reference units by name. Useful for using custom units.
[Unit("Meters per Second")]
public float MetersPerSecond;
// Combine with ShowInInspector and properties can be used as a kind of debug field by displaying a value in multiple units at once.
// ForceDisplayUnit prevents users from change the unit in the inspector, and DisplayAsString, well, displays as string.
[ShowInInspector, Unit(Units.MetersPerSecond, Units.KilometersPerHour, ForceDisplayUnit = true, DisplayAsString = true)]
public float KilometersPerHour => MetersPerSecond;
Fairly simple, but it can be surprisingly hard to wrap your head around. Just remember: The actual unit never changes. A meters field will always be a meters field, regards of whether it's displayed as nanometers or light-years in the inspector.
All the new unit and smart field behaviors can be found in the SirenixEditorFields (Sirenix.Utilities.Editor) class, alongside all other custom Odin fields. The new methods are UnitField, SmartUnitField, and SmartField, with variations for different primitive data types.
In UnitNumberUtility (Sirenix.Utilities.Editor), is the core of the new unit system. The class contains definitions for all implemented units and conversion methods. You can also use the AddCustomUnit methods to add custom units, and they will work with the unit fields.
The UnitInfo (Sirenix.Utilities.Editor) class defines a unit in the Unit system. It contains a unit's name, symbols, category, and conversion instructions.
We welcome feedback on the Unit system, built-in units, and any issues related to interacting with Unit and smart fields, as well as suggestions for improvements.
Download | Discussion & Feedback