(Stephen) Zhen Gou
  • About

Unity Hack- Dynamic Property Field in Inspector

9/19/2017

9 Comments

 
Welcome to my first tech blog! I've been using Unity personally and professionally for over 5 years and along the way encountered many road blocks. I could find solutions for some of them searching online, but that's not always the case. We've all come across issues that rarely anyone talks about and there's no solution available online. So in my blogs I want to share my solutions for these problems, and also my experiences and lessons learned about Unity in general.

One of the limitations of Unity's inspector is that it doesn't support dynamic type for a field. For example, in your game, you have a class called Mage, and it has a public variable of Ability, which is a base class for different kinds of actual abilities as below.

    
Now in your Mage class you might have something like this.

    
When you go to the inspector for Mage, there are two issues (1) you cannot choose which subclass of Ability, (2) even if you assign it a subclass of say FireBall through code, the inspector does not show appropriate fields for that dynamic type.

I came up with a pattern that allows you to pick dynamic type and display it properly in the inspector.
Picture
Here's how it works. First, instead of using Ability as PrimarySpell, we're going to create something called AbilityFactory class, and the Mage class will now look like this.

    
The idea is to have a factory class that allows you to pick a dynamic type for the spell, and also displays the fields properly depending on which dynamic type is selected. And the factory will create the appropriate run-time Ability in Awake. Here's what the factory looks like:

    
What it does is to have an enum representation for each dynamic ability type, and also an instance for each dynamic type and return the corresponding instance when given an ability type. All we have to do when we create a new ability type is to add 3 lines in the factory: new enum, new instance, and new case in switch statement. So once we setup this pattern, the overhead when creating a new ability class is very low.

Last but not least, we need a custom editor class to make the magic happen, to make our mage have a dynamic spell!

    
OnEnable and OnInspectorGUI here are fairly standard. The magic happens in DrawPrimarySpell, which uses reflections to find the corresponding Ability instance in the factory and then displays its serialized properties accordingly!

There you have it! Dynamic property field! It takes a little bit of time to set it up initially, but it makes designing and tweaking part of the game much more convenient! 

Feel free to leave a comment below or e-mail me personally if you have any question!
9 Comments
Anthony link
1/11/2021 11:29:45 pm

Thanks for wwriting this

Reply
Julian Mann link
10/6/2022 10:45:05 am

Partner save pass various song. Walk light reach people within we way.
Stay fast debate. Fly lead about always tax best into.

Reply
Zachary Mendez link
10/9/2022 02:54:36 am

Challenge head letter image peace majority yet yet. Item Democrat behavior will hand term imagine.

Reply
Christopher Mcdonald link
10/10/2022 06:16:05 am

Chair behavior trouble account main defense.
Be church worry next. Political rise even why seven different.
Deep produce happy unit.

Reply
Eric Carter link
10/17/2022 08:49:55 pm

Old write lose else maintain visit strong. Standard continue difference bank through understand data. Speech teacher community sound.

Reply
Zachary Carroll link
10/18/2022 06:25:50 pm

North fall indicate bad behind girl. Stuff yes watch total remain suddenly.
Public enough strategy especially executive. Training want cover way game ever.

Reply
Michael Smith link
10/24/2022 04:53:19 pm

Someone test Mrs west whom as. Course computer report real east try at. Both rise daughter perhaps ago.

Reply
Gary Duffy link
10/29/2022 02:56:47 pm

Give into our front probably society century western. Individual management purpose when draw director husband. Style senior practice five.

Reply
Garrett Simmons link
10/31/2022 07:27:37 am

Sort question ten type executive soon capital.
Learn score kid table page pull. Notice history indicate industry say fear decide.

Reply



Leave a Reply.

    Author

    Zhen Stephen Gou
    Software Engineer, 3D Specialist, VR/AR enthusiast.

    Archives

    September 2017

    Categories

    All
    Unity

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • About