Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many attributes do not work in Unity 2022.2+ #369

Open
emberTrev opened this issue Aug 21, 2023 · 13 comments
Open

Many attributes do not work in Unity 2022.2+ #369

emberTrev opened this issue Aug 21, 2023 · 13 comments

Comments

@emberTrev
Copy link

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
Copy link
Author

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

@emberTrev
Copy link
Author

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
Copy link

crawfis commented Nov 16, 2023

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

@GameDevElliot
Copy link

GameDevElliot commented Nov 16, 2023 via email

@emberTrev
Copy link
Author

emberTrev commented Nov 16, 2023

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
Copy link

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

@TylerTemp
Copy link

TylerTemp commented Jan 1, 2024

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
Copy link
Author

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
Copy link

TylerTemp commented Jan 3, 2024

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
Copy link
Author

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
Copy link

TylerTemp commented Jan 15, 2024

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants