NaughtyAttributes icon indicating copy to clipboard operation
NaughtyAttributes copied to clipboard

[Issue] NaughtyAttributes breaks all custom UIElements-based PropertyDrawers

Open KesKim opened this issue 5 years ago • 7 comments

Problem: NaughtyInspector has the catch-all [CustomPropertyDrawer(typeof(UnityEngine.Object))], so everything without a custom Inspector of its own is displayed via NaughtyAttributes via IMGUI.

Background: IMGUI cannot render UIElements within itself.

My use-case: I just noticed that making UIElements PropertyDrawers is pretty simple and would speed up making custom PropertyDrawers for complex classes that need it much faster. Making per-System.Serializeable PropertyDrawers is also much better than making per-Component Inspectors, since I could have the same class drawn in multiple Components via PropertyDrawers and would only have to make one UI extension implementation as a PropertyDrawer. Only way to use my UIElements PropertyDrawers then becomes implementing custom Inspectors to everything that has fields that need my custom PropertyDrawers.

I also widely use NaughtyAttributes for a ton of simple Components that benefit excessively from NaughtyAttributes simplifying and contextualizing the available options dynamically, so there's no way I can ditch NaughtyAttributes either - your framework is essentially fantastic by its basic premise.

Suggested solution: Could you consider adding UIElements support to NaughtyInspector? If PropertyFields in NaughtyAttributes were rendered as UIElements.PropertyFields, then all custom UIElements PropertyDrawers would also function.

Some of the work could possibly be offloaded with IMGUIContainers and still using IMGUI for most of the code that doesn't directly concern PropertyFields; UIElements can create a simple VisualElement type of IMGUIContainer, which takes a method that renders the IMGUI as constructor parameter.

KesKim avatar Mar 19 '20 10:03 KesKim

Even though the NaughtyInspector targets all UnityEngine.Objects it still draws the default inspector if there are no naughty attributes at all.

bool anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute<INaughtyAttribute>(p) != null);
if (!anyNaughtyAttribute)
{
	DrawDefaultInspector();
}

I will check the UIElements. It's a fairly new feature and I haven't looked at it yet.

dbrizov avatar Mar 20 '20 07:03 dbrizov

Yeah, UIElements (soon renamed UI Toolkit) is relatively new, and as I said I just started getting to grips with it myself so don't substitute looking into it yourself when you have the time by just taking everything I say as gospel truth.

I think DrawDefaultInspector() is an IMGUI method and as such does not help for this issue: Inspectors have two paths, first via override VisualElement CreateInspectorGUI() and if there's no override for that, only as a fallback override void OnInspectorGUI(). As DrawDefaultInspector() doesn't return a VisualElement and is already called in OnInspectorGUI() we've already branched into the IMGUI-only path.

KesKim avatar Mar 20 '20 08:03 KesKim

Best case estimate is that to actually solve this you could implement CreateInspectorGUI() and figure out a solution using IMGUIContainers() reusing some of the stuff you have.

Worst case is that for NaughtyAttributes to support the changing UI handling of Unity you'd have to duplicate all GUI drawing and have it implemented in VisualElements and IMGUI.

Some out-of-the-box solution might exist, if there's a way to not have a catch all [CustomEditor(typeof(UnityEngine.Object))], but you're the one here who's published a whole neato GUI-centered toolkit and probably know this stuff and no doubt have excellent reasons to have picked the current approach.

Anyway, Unity is pushing UIElements, eventually more people are gonna pick it up, and I gotta say after a bit of looking into it's quite nice to build stuff in, so far.

KesKim avatar Mar 20 '20 08:03 KesKim

Ok mate, thanks. I will look into it. There has to be a smart way to support both.

dbrizov avatar Mar 20 '20 08:03 dbrizov

Any news on supporting UIToolkit for naughty attributes? Starting with unity 2022.2 all default inspectors are drawn with ui toolkit and NaughtyAttributes makes it so, that they are still drawn by the old IMGUI. So any custom property drawers based on uitoolkit won't work. Please fix it, so we still can use NaughtyAttributes :)

YanickFrehner avatar Dec 01 '22 14:12 YanickFrehner

(Year 2024) Inspector enhancement like NaughtyAttributes but supports UI Toolkit, besides OdinInspector (paid), I have a project called SaintsField that:

  1. Supports UI Toolkit.
  2. Has almost all functions that NaughtyAttributes have.

It's relatively new so might have some bugs.

BTW I don't see any smart way to support both IMGUI & UI Toolkit without maintaining two different code implements. (There is a IMGUIContainer in UI Toolkit but it's too weak to tweak)

TylerTemp avatar Feb 29 '24 08:02 TylerTemp