dbrizov / NaughtyAttributes

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

[Question] How to change how ShowNonSerializedFields are rendered #378

Open AndrzejKebab opened 10 months ago

AndrzejKebab commented 10 months ago

So I'm doing a RPG game and i have bunch of stats and i would like to have the non serialized values to show at their places in inspector

image

TylerTemp commented 10 months ago

This at this point is not possible, because NA will always render NonSerializedFields and buttons at last.

A workaround is to write a extended script. This is what i use in my project:

gist link

Delete all non-functional (because I have many other drawer extension as well).

This works on my project but because the extensions not included here, and many custom functions I used for myself, so not guaranteed it works in yours. But... you get the idea.

And add a [Ordered] to field if the order is incorrect, then the inspector will know which comes first.


Update: and this is how it looks like in my project. Note the button is in the middle of where it should be.

[SerializeField, Ordered] private int _pIntPrivateOrdered;

[Ordered]
public int pIntOrdered;

[Button("Button Text"), Ordered]
private void MethodTwo() { }

[ShowNativeProperty, Ordered] private string NativeString => "NativeString";
[ShowNonSerializedField, Ordered] private const int NonSerInt = 9;

[Ordered]
public Animator someAnimator;

[AnimatorParam(nameof(someAnimator)), Ordered]
public int paramHash;

FW`}KJKN4N`I(GU~HZAK`J6


Update: @AndrzejKebab I just notice that the sort function is incorrect...

Please change

_fieldWithInfos.Sort((a, b) =>
{
    // Debug.Assert(a.inherentDepth != 0);
    // Debug.Assert(b.inherentDepth != 0);
    int firstResult = a.inherentDepth.CompareTo(b.inherentDepth);
    return firstResult != 0
        ? firstResult
        : a.order.CompareTo(b.order);
});

Into:

_fieldWithInfos = _fieldWithInfos
    .WithIndex()
    .OrderBy(each => each.value.inherentDepth)
    .ThenBy(each => each.value.order)
    .ThenBy(each => each.index)
    .Select(each => each.value)
    .ToArray();

I have also updated the gist script.

(P.S. I've open-sourced my utils from my old project and published here, if you're interested please consider to have a look. The gist is basiclly this script)