Unity Serializable Class Custom Inspector
Asked Answered
M

3

6

I have a very simple class in unity, UnitRange (which has a minimum and a maximum range).

[System.Serializable]
public class UnitRange {
    public int Minimum;
    public int Maximum;
}

And this shows up in the inspector (if I make a public variable of this type.) Though the default way it is shown isn't very good:

enter image description here

Now, I was wondering how I can change this? I found how to change the inspector of monobehaviours, though couldn't find how to change it of other classes. I would like it to just be two numbers next to each other, something like this:

enter image description here

It's just a small thing, and not that big a problem if it's not possible, though knowing how to could prove more useful later too.

Oh yes, as you might have noticed, I'm using c#, so it would be nice if any example code is in c#.

Thanks.

Meiny answered 13/11, 2012 at 14:18 Comment(4)
are there any other types that do this in the inspector? if so, maybe have a look at how they are implemented (in particular, do they have any interesting attributes, interfaces, or custom ToString etc)Borgerhout
Unity's Vector3 does it in the transform. Though maybe that's just the transform that has a custom inspector to make Vector3 look better. Though Unity isn't open source so you can't see the code of the transform :(Meiny
yes, but surely type metadata is available via reflection? so you should still be able to see attributes and interfaces... it should even be available just in the object browser.Borgerhout
is there a place where I can find the source stuff, of people who already did that before me, that you know of? Or do I have to do it myself?Meiny
A
1

From Unity4 you can make this with PropertyDrawer

Affix answered 3/2, 2015 at 14:16 Comment(0)
M
2

This is no longer true in later versions of Unity.

Just found out this is not possible.

The only way to do this is, whenever you use it in a monobehaviour, to give that monobehaviour a custom inspector and in there give the class your custom layout. To make this easier you can make a method which does the layouting and then use that in each monobehaviour.

Meiny answered 13/11, 2012 at 17:9 Comment(0)
A
1

From Unity4 you can make this with PropertyDrawer

Affix answered 3/2, 2015 at 14:16 Comment(0)
L
0

One solution that would require writing less custom inspectors would be to make UnitRange a component. Anything that needs a UnitRange you can annotate with [RequireComponent (typeof (UnitRange))] so you don't have to go through the hassle of adding it yourself. Make UnitRange check it's the only one attached (and error/remove itself etc if it's not).

Then make your various units cache the attached unit range component on Start() using GetComponent<UnitRange>(), ready for later use (as you currently do, if you just change the visibility to private and reuse).

Finally - write the inspector for UnitRange that looks nice.

Lovins answered 15/11, 2012 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.