dbrizov / NaughtyAttributes

Attribute Extensions for Unity
MIT License
4.53k stars 464 forks source link

How to figure out which package is stopping NaughtyAttributes from working? #389

Closed KamilTheDev closed 3 months ago

KamilTheDev commented 3 months ago

I'm working on a large project in Unity 2022.3.27f1 that contains a lot of different assets/packages imported. I added NaughtyAttributes today from asset store, but most of the custom attributes have no effect on the inspector view. I don't have anything added for the purpose of custom editor features.

Specifically I want to use ReadOnly and BoxGroup right now, which have no effect on the inspector.

I created a brand new project and added NaughtyAttributes the same way, all the features are working.

I tried slowly adding back all the imported assets + Editor folder to the new project, but NaughtyAttributes still works, maybe I missed something. Is there any other way to try figure it out?

TylerTemp commented 3 months ago

Firstly, NaughtyAttributes uses the old IMGUI only, ensure you have: Player Settings - Editor - Use IMGUI Default Inspector checked. And restart your unity (IMPORTANT!).

Now, check if things work correctly. If things are broken, check that thing, it's been drawn by another drawer.

If meta attributes still broken, create these:

attribute decorator:

using UnityEngine;

public class MyAttribute : PropertyAttribute
{
}

MonoBehaviour, and attach to some gameObject

using UnityEngine;

public class MyMono : MonoBehaviour
{
    [MyAttribute] public string myField;
}

Now, create the drawer, under an Editor folder. (create an asmdef if you use asmdef to manage your project)::

using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[CustomPropertyDrawer(typeof(MyAttribute))]
public class MyAttributeDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        throw new Exception("IMGUI");
    }

    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return 20;
    }

    public override VisualElement CreatePropertyGUI(SerializedProperty property)
    {
        return new TextElement
        {
            text = "I'm drew by something else!",
        };
    }
}

If you see "I'm drew by something else!", then UI Toolkit is still the default drawer or NaughtyAttributes is not enabled at all. You need to dig more. You can comment out CreatePropertyGUI to see if there are more useful info.

If you see the error like this:

Exception: IMGUI
...
SomeThing.OnInspectorGUI () (at Assets/Editor/SomeThingcs:196)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass71_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <a62328349c154758b537cb55981f61df>:0)

This SomeThing is what blocks your NaughtyAttributes.


BTW, NaughtyAttributes have not been maintained for years and never support UI Toolkit. I'd suggest some other alternatives:

KamilTheDev commented 3 months ago

For anyone in the future, the issue was from Quibli, a toon shader that also contains custom editor features.

Here's a useful script to find out which asset may be causing issues with editor: https://github.com/TylerTemp/SaintsField/issues/51#issuecomment-2227303081

I've switched to using SaintsField since there's an easy fix without needing to modify the Quibli asset.