How can I extend the `SerializeField` attribute in Unity? Why is it sealed?

How can I extend the SerializeField attribute in Unity? Why is it sealed?

I have a custom attribute AutoAssign. When the attribute is applied to a field, I find object of the field type in scene and assign the found reference to the field automatically.

Now, I would also like to see the object in editor, but I do not want to write two attributes each time, i.e. SerializeField and AutoAssign.

Is there a way to extend SerializeField, ideally I would want my AutoAssign to extend SerializeField, but the SerializeField is sealed, so there seem to be no way to do that. :frowning:

You can write attributes like this if you weren’t aware:

[SerializeField, AutoAssign]
private GameObject myGo;

Which can make them a little cleaner.

[SerializeField] has probably been sealed to ensure stability. It’s a complex attribute that affects how the engine works, both on the C# and C++ side. They probably want to discourage it’s extension for these reasons. It’s also generally a bad idea to obfuscate attributes in this way.

4 Likes