Dynamic game configuration

During an internship I was tasked with maintaining a Unity game that had the concept of Gamestyles. A Gamestyle is the configuration for the game, and the host can switch between gamestyles before starting the game.

Unfortunately, I can’t show you any of the code, or tell you the name of the game, or the company that this was done for.

When I picked up the work, the gamestyle editor window consisted of about a hundred hard-coded UI fields, spread over 8 tabs, each of which was hard-coded into multiple scripts and assigned to references in the Inspector, which ended up being 400 lines of code that looked pretty much identical in every line. Naturally, I took it upon myself to fix this madness.

After a few days of work I had gotten rid of all of this and replaced it with far-less-work-to-maintain code that inspects the Gamestyle class using reflection and creates tabs and input field objects as necessary. Absolutely no UI functionality was lost in this process, and adding new gamestyle fields is now as easy as adding the field to the Gamestyle class. Everything is done for you when you add new fields.

The gamestyle editor, featuring test fields.

A property in the Gamestyle class is considered eligible if it has a NameAttribute. It may additionally have a MinAttribute and/or MaxAttribute, which control the min and max values for numeric types. For gamestyle fields that have an explicit unit (such as the game time in minutes), a PostfixAttribute allows you to add text to the right of the field. Finally, a TooltipAttribute allows you to provide a little explanation for a gamestyle field.

A fun side-effect of the fact that it uses reflection, which I only realized after I built the whole thing, is that it is possible to create derived classes of Gamestyle, which allows you to create entirely unique gamestyle categories with settings specific to that category.