Skip to content

Latest commit

 

History

History
32 lines (17 loc) · 2.46 KB

README.md

File metadata and controls

32 lines (17 loc) · 2.46 KB

StructableObject

StructableObject is a wrapper for Unity's ScriptableObject system. It basically just lets you manipulate a data struct as if it were a ScriptableObject. This has been useful in my own development as it allows you to create systems which work well with both procedural data and designed data.

Why

Let's say you have a simple struct which represents some data package. This could be damage information (how much HP, whether it's poison, the damage source...), an enemy AI profile, any small bit of data.

You want to make it so your designers can easily modify this information, so you put it in a ScriptableObject. However, your programmers also want to be able to easily create instances of these data objects at runtime with any kind of modification. Maybe your player gets a damage buff so all your projectiles now take 2x HP. There are many solutions for this. You can create ScriptableObjects without saving them as Unity assets, but that has a lot of overhead. You can make interfaces, but that could require some refactoring and it's a pain when you want to add or remove variables to your data.

I often found myself just putting this information in a struct and serializing that struct in a ScriptableObject. Over time, I began writing custom editors for these ScriptableObjects to make it appear that the struct's contents were the ScriptableObject, to make the experience seamless for designers. Finally, I wrote this generic solution.

How

All you have to do is write a serializable struct:

image

Inherit from StructableObject, giving this struct as a type parameter:

image

And likewise, create a Custom Editor class:

image

The final ScriptableObject appears and works exactly as though you had put the fields right in a ScriptableObject, including supporting undo.

image

The struct can be accessed in code via the .Data property on the StructableObject, but sometimes I add passthrough properties to make it even more seamless.

image