Custom editors for component/class attributes

Hello :slight_smile:

Is it possible to write custom editors for class-attributes? Like:

[ComponentPosition(1)]
public class Test : MonoBehaviour
{
    public int test;
}

What I want is, that Components with the “ComponentPosition” attribute are trying to place themselves to the given position on the gameobject, so that the component is always on top for example.

Does Unity support this feature?

Thanks!

Unity now explicitly allows to re-order components and some things depend on that particular order (like audio filters for example). So it’s no recommended to implement any automatic reordering of components.

Also keep in mind that you can’t change the position of the Transform component as it’s always the first component.

You should be able to write some code that change the ordering based on an attribute either is inside a custom editor for the component, or by writing a general editor script that subscribes itself to the Selection.selectionChanged event and perform the reordering. To change the order you can use the undocumented methods “MoveComponentUp” and “MoveComponentDown” inside the class “UnityEditorInternal.ComponentUtility”.

Besides the fact that it’s not recommended to do something like that at all and the ComponentUtility is undocumented (so it might become unavailable or might be renamed / moved in the future), keep in mind that the user can specify contradicing attributes. If two components want to be at position “2” your code should at least yield a stable result.