Spawn Doctor: Part Two
Putting your ScriptableObject functions to work
Previously in Part One of our spawn doctor series, we talked about the basic data for setting up an enemy spawn using ScriptableObject as well as some additional visualiser components. However, from this point on in the process, there’s not as much happening on the editor side of things…
Tying it Together
First let’s add 2 visualiser classes, which will be used by EnemySpawnCollection.
Both EnemySpawnVisualizerRoot and EnemySpawnPointVisualizer classes extend from our node visualisers. It allows the use of set specific data for our nodes, which might include colour, enemy spawn data, etc.
Now, let’s go back to EnemySpawnCollection.cs and expand on the functions SerializeData and OnValidate.
We’ve added a new dictionary called lookupTable, which will be used to store the map between scene game object visualiser nodes and the corresponding serialised data.
You’ll recall that back when using the NodeVisualiser, we would use SerialiseData in the update function whenever the game object transform was changed. With it, we can update our stored data with the new position value. This means that when you move a node in scene, the data will be reflected in the inspector.
Where SerialiseData allows us to transfer data from scene to serialised data, OnValidate does the opposite. Whenever there’s a change in the inspector value, OnValidate updates the scene object accordingly.
Some Less Fancy Functions
Now that we have set up all the required data, we need to be able to create the game objects visualiser nodes in scene.
Here we’ve added a new variable, rootNode, to which all visualiser nodes will now be parented.
This function allows us to create the visualiser nodes in scene based on our serialised data. It uses the helper functions in NodeVisualizerHelper.cs to create the required game objects. At this point we’ll also be updating the lookupTable.
A handy function to clear all nodes belonging to a ScriptableObject.
I’ve also added a new variable, DisplayName, to EnemySpawnData. This is especially handy when you have many spawn points in scene and want to be able to identify each at a glance.
Take a look at the ScriptableObject asset you have created so far. Still not seeing any thing new? There’s still one crucial step to go: drawing the buttons.
Create EnemySpawnDataEditor.cs in the Editor folder. (For more information regarding special folders, head to https://docs.unity3d.com/Manual/SpecialFolders.html)
We have now created 2 buttons, Create Marker and Clear Marker, which will be used to create new visualiser nodes or clear existing nodes for our asset.
You should now see something similar to the following when you inspect the ScriptableObject asset again.
This is your cue to create some new spawn points and play around with the Create Marker and Clear Marker buttons!
Try moving the nodes in scene or changing the inspector values and see what you can make happen.
Of course, what we have here isn’t a cure-all for spawn point editing. But, if you’re looking for a way to streamline the editing process for your spawn system using ScriptableObject while also leveraging Unity’s own features, this is certainly a good starting point.
Did you find these steps useful? Got any other tips or tricks you’d like to add? Let me know in the comments!