Best Way to Implement Command Pattern For Scriptable Object-like Objects

Hello!

I often find myself struggling to design code inside of Unity due to the nature of MonoBehaviors, ScriptableObjects, etc being a bit different from standard classes.

To give a specific example of something I am trying to map out right now:

Let’s say in normal C# terms, I have a class called Symbol. Every symbol has a name, a sprite, and an Execute command which differs between symbols. I want to be able to define things like the sprite and name through the editor. I’d love to use something like the command pattern to handle the functionality execution for every different symbol, but that’s where things become confusing for me.

I do not want these symbols to be on GameObjects, because they are moreso just data and instructions. GameObjects will use them, but there logic does not need to exist as its own GameObject in scene. In other words, MonoBehaviours are definitely not what I want. ScriptableObjects almost seem like what I want, they allow me to define values and whatnot through the editor, but if I were to create a symbols class and then derive variations of classes from that class, or just create separate classes and use an interface, it results in a setup that feels awkward to use. I would have to create each script, and then create a singular object for each script after that. I COULD do that, but it just seems kind of clunky and weird to create ScriptableObjects just to create a singular object from each of them so that I can define the stats.

I just wonder if there’s a better way and I just don’t know what it is. Hoping someone has a better idea. I could of course also just make a ScriptableObject and throw a switch statement in the execute function with all the logic, but that just feels like taking the easy way out haha. I am trying to learn to use more advanced and scalable patterns in Unity. Maybe I could create normal classes for each of the different symbols with a common interface, and then make a singular ScriptableObject with a switch statement that chooses a class to use based on some Enum designation on the object itself? Still not what I’d call elegant lol.

Thanks!

Classically, I have solved my own issue after posting. I imagine this will be the most elegant solution and I am quite happy with it.

I learned that Unity also has a [SerializeReference] tag. In researching that further, I found this github project: GitHub - mackysoft/Unity-SerializeReferenceExtensions: Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.

Basically, this will allow me to assign a reference to a specific subclass of my command Interface. So I can just create a bunch of command scripts using ICommand, create a single ScriptableObject class for Symbol, then create all my various symbols and assign the relevant ICommand to each of them. Love it!