dbrizov / NaughtyAttributes

Attribute Extensions for Unity
MIT License
4.57k stars 465 forks source link

Many attributes do not work in Unity 2022.2+ #369

Open emberTrev opened 1 year ago

emberTrev commented 1 year ago

Here is an excerpt from Unity's docs:

Note: You cannot have UI Toolkit running inside IMGUI, which means that if your custom PropertyDrawer only has a UI Toolkit implementation, it will not work inside an IMGUI custom Inspector or a parent IMGUI custom PropertyDrawer. In Unity 2022.2 and above, the default Inspector uses UI Toolkit exclusively in custom PropertyDrawers. Prior to 2022.2, it is recommended that you either implement both IMGUI and UI Toolkit versions of each PropertyDrawer, or make sure they are exclusively used inside custom UI Toolkit inspectors.

https://docs.unity3d.com/ScriptReference/PropertyDrawer.html

I added NaughtyAttributes to my project (running 2022.3) and these are the attributes that I noticed did nothing. There are probably more.

  1. ShowNativeProperty
  2. ShowNonSerializedField
  3. Button
emberTrev commented 1 year ago

image Screenshot of ShowNativePropertyTest component in action in the Test Scene.

emberTrev commented 1 year ago

I have become aware of this checkbox in the project settings. Not a fan of it, but I guess it could be a workaround. Screenshot_2023-08-22-10-45-23_Unity

crawfis commented 1 year ago

ShowIf/HideIf also do not work with UI Toolkit and Unity 2022.3

GameDevElliot commented 1 year ago

So I guess that even if it were possible for me to use the Odin inspector to customize the look and feel of my tools for people that download it (who may or may not have Odin inspector themselves), it would be problematic because it would be version dependent?

On Thu, 16 Nov 2023, 14:26 Roger Crawfis, @.***> wrote:

ShowIf/HideIf also do not work with UI Toolkit and Unity 2022.3

— Reply to this email directly, view it on GitHub https://github.com/dbrizov/NaughtyAttributes/issues/369#issuecomment-1814536388, or unsubscribe https://github.com/notifications/unsubscribe-auth/BCXLXGITXTR453UIGBUXDOLYEYPBVAVCNFSM6AAAAAA3Y3INVOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJUGUZTMMZYHA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

emberTrev commented 1 year ago

If you write custom editor UI that uses either IMGUI or UI Toolkit (not both), then yes your custom inspector won't appear for everyone. You can:

  1. Write both if you want to support everyone.
  2. Write only the UI Toolkit version if you are ok ignoring old versions of Unity Editor. I'm not sure how far back that will work. It's not like UI Toolkit was added with 2022.2, that is just when IMGUI was turned off.
  3. Writing only an IMGUI implementation seems like the worst choice since IMGUI is totally turned off starting with Unity Editor 2022.2. (You can re-enable it manually, see my previous comments in this thread.)

I don't know anything about Odin inspector.

Follow-up: NaughtyAttributes already has a full IMGUI implementation, so it needs to add to or replace the implementation with UI Toolkit to support newer Unity versions.

ADH-LukeBollam commented 11 months ago

@emberTrev did enabling that checkbox return your buttons? It doesn't do anything for me on 2022.3.15f

TylerTemp commented 10 months ago

Can you give a reproducable example? My test works:

  1. Unity: 2022.3.16f1
  2. NA: 2.1.4
  3. Project Settings - Editor -> Uncheck Inspector: Use IMGUI Default Inspector

屏幕截图 2024-01-01 185202

public class Cap : MonoBehaviour
{
    [ShowNonSerializedField] public static readonly Color red = Color.red;
    [ShowNativeProperty] public Color blue => Color.blue;

    [Button]
    private void EditorButton()
    {
        Debug.Log("EditorButon");
    }
}

屏幕截图 2024-01-01 185240


Update: Got it, but my case is using it with UIKit drawers, the UIKit drawers can not be rendered correctly

emberTrev commented 10 months ago

Yes, the issue I am reporting is that the UIKit drawers do not work. Since that is the default setup for newer versions of Unity, people who start new projects will install this extension and see that it doesn't work unless they know or find out about the Inspector: Use IMGUI Default Inspector. I do not have enough control over my team's project to change the Inspector: Use IMGUI Default Inspector checkbox for everyone.

I realize that to make UIKit work, the author would probably have to re-implement all the features of the extension in UIKit, which would be a lot of work. I guess that is what I am requesting since Unity is missing these features and they are very useful, but I can't use them on my team's project currently.

Are you saying it isn't possible to implement the same feature set in UIKit?

TylerTemp commented 10 months ago

OK after some digging here is the thing

  1. It's not possible to render ToolKit inside IMGUI

  2. It's possible to render IMGUI to ToolKit in public override VisualElement CreatePropertyGUI(SerializedProperty property)

    VisualElement container = new VisualElement();
    IMGUIContainer imGuiContainer = new IMGUIContainer(() =>
    {
        myDrawerTool.MyImGuiDraw(myRect, property, label);
    });
    imGuiContainer.style.height = myRect.height;
    container.Add(imGuiContainer);
    return container;
  3. As this makes the whole thing a callback, many things that are easy in IMGUI becomes tricky. For example, you need to call property.serializedObject.ApplyModifiedProperties(); which is unnecessary for IMGUI PropertyDrawer

  4. And yes, a hell lots of adaption for this change... (Odin actually supports UIToolKit+IMGUI by this IMGUIContainer , but in my test case, oddly, IMGUI sometimes does not work very well in Odin)

(I'm trying to implement it in my util project and, yes, it's hell lot of pain...)

emberTrev commented 10 months ago

I am going to post this again from my original issue.

From here: https://docs.unity3d.com/ScriptReference/PropertyDrawer.html

Note: You cannot have UI Toolkit running inside IMGUI, which means that if your custom PropertyDrawer only has a UI Toolkit implementation, it will not work inside an IMGUI custom Inspector or a parent IMGUI custom PropertyDrawer. In Unity 2022.2 and above, the default Inspector uses UI Toolkit exclusively in custom PropertyDrawers. Prior to 2022.2, it is recommended that you either implement both IMGUI and UI Toolkit versions of each PropertyDrawer, or make sure they are exclusively used inside custom UI Toolkit inspectors.

So that's why my expectation would be that you would have to re-implement everything with a UI Toolkit version that has the same behavior as the IMGUI version.

Sorry about this work that the Unity upgrade has brought to the surface. I feel like the bearer of bad news.

TylerTemp commented 10 months ago

Sorry about this work that the Unity upgrade has brought to the surface. I feel like the bearer of bad news.

Well, agreed...

I just adapted my project SaintsField to support UI Toolkit. And I completely rewrite every components. I don't think there is a shortcut.

(If you like it please give a star, thanks!)

And my experience about UI Toolkit as an editor extension, is painful. Yes it's delightful how easy to handle elements and style, but most things are missing for editor. I quote from my project's README:

  1. Label width. UI Toolkit uses a fixed label width 120px. However, this value is different when the field is nested and indented.

    In IMGUI, we have EditorGUIUtility.labelWidth, EditorGUI.indentLevel, which is absolutly not avaliable in UI Toolkit, and there is no way to get the label width. SaintsField just use the fixed label width.

  2. Even most UI Toolkit fields are fixed label width, there is one that the label width behavior exactly like IMGUI: PropertyField. This bug has been reported to Unity (Sorry I can't find the link now), but is never fixed.

    SaintsField heavily relies on PropertyField to fallback the appearence. This is not even fixable at this point. If you try to obtain the info by query out the PropertyField element, you'll notice that Unity is internally using a script (rather than a uss style) to update it's label width.

    Even without using any custom inspector, if you use your UI Toolkit PropertyDrawer with default fields, your inspector label will not aligned, and makes it look really rediculous.

    This is not fixable.

  3. PropertyField of an Object will give an error when click: NullReferenceException: ... UnityEditor.ProjectBrower.FrameObject.... Clicking will still lead you to active the target object, but I have no idea where this came from. Even official's example will have this error if you just add a PropertyField to it. Clicking on the error message will lead to the Console tab's layout got completely messed up.

    This is not fixable.

  4. DropdownField will tread a label's change as a value change... I have no idea why this happens and why only DropdownField. Luckily this change event will give a newValue=null so I can work around with it.

My project is very new and not fully tested. For people who want a stable editor plus UI Toolkit? Yeah, just pay for OdinInspector...


Edit:

Oh, and also, when leaving an PropertyDrawer when active a new target, the old one's CreatePropertyGUI will also get called once. It's not a big deal in most case, but... I just don't know why, and how, and what?

JellySnek commented 6 months ago

I have also tried this and I can say that Foldout doesn't work for me. I' on Unity 2022.3.20f1. Sucks, because this really looked like a powerful tool that I could use. Even Using IMGUI tickbox to either true or false, nothing appears. Foldouts don't work at all.

TylerTemp commented 6 months ago

I have also tried this and I can say that Foldout doesn't work for me. I' on Unity 2022.3.20f1. Sucks, because this really looked like a powerful tool that I could use. Even Using IMGUI tickbox to either true or false, nothing appears. Foldouts don't work at all.

Well, after half years, 50+ literation, I have fixed many issues in SaintsField and I personally feel it's stable, well, at least in my personal projects and company projects.

It's a hell lot of pain to adapted to UI Toolkit.

You may have a try, at this point it already has almost all features of NaughtyAttributs plus more