#Update System.Serializable object's name on field update?

1 messages · Page 1 of 1 (latest)

craggy relic
#

I have a very simple plain C# class that uses [System.Serializable] to be editable in the Unity Editor. It has a field that takes an enum, and I would like the class to update its name field to the enum whenever an enum is selected. As it doesn't inherit from MonoBehaviour or anything, I can't use the normal Editor extensions.

Is there a way to achieve this?

Online I read about C# 7.3 possibly achieving this with autoproperties, but my C# version is lower, so if possible I'd prefer not to update as Unity can be unstable with stuff like that. If the conclusion is "use autoproperties", then that's fair

unborn mesa
#

my C# version
"your" c# doesn't particularly matter? unity has its own specified runtime

#

i don't see how autoproperties specifically helps, just properties themselves might

#

what's the point of having the name field if it's just going to be the enum value?

craggy relic
#

So it just makes it easier for the designer to see what's going on exactly

unborn mesa
#

ah, i see.

#

same usecase as you're trying to get, actually

#

aside from that, yeah properties would probably work

craggy relic
#

Ooh, that's exactly what I'm looking for! Thanks

unborn mesa
#

i don't think autoproperties would work though

craggy relic
#

I'll try it out in a bit

craggy relic
unborn mesa
#

added in C# 13, unity uses C# 9 i think. might be 8.

#

it'd look something like this

public string name;
[field: SerializeField] ClipTransition animation { get; set { field = value; name = Enum.GetName(typeof(ClipTransition), value); } }
#

the field = value part is the newer, unavailable feature

#

though sidenote - if you want a single instance of AnimationLink with each ClipTransition, perhaps consider a dictionary keyed by ClipTransition

#

(also AnimationLink could probably be a struct)

craggy relic
#

Thanks a lot for the in-depth reply, it's very much appreciated!

craggy relic