Photo by Joshua Newton on Unsplash

Spawn Doctor: Part Two

Pauline Pun
Mighty Bear Games
Published in
3 min readJul 6, 2021

--

Putting your ScriptableObject functions to work

In Part One of my Spawn Doctor series, I talked about the basic data that goes into 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. Now that everything’s in place, it’s about getting it all working together…

Combining Your Elements

First let’s add 2 visualiser classes, which will be used by EnemySpawnCollection.

Both EnemySpawnVisualiserRoot and EnemySpawnPointVisualiser 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 SerialiseData 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.

SerialiseData
You’ll recall that when using the NodeVisualiser, we call SerialiseData in the update function whenever the game object transform is 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.

OnValidate
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 object visualiser nodes in scene.

Here we’ve added a new variable, rootNode, to which all visualiser nodes will now be parented.

Create marker
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.

Clear marker
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 different spawn points in scene and want to be able to identify each at a glance.

Almost there…

Take another 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. (Head to https://docs.unity3d.com/Manual/SpecialFolders.html for more information on special folders!)

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.

Enemy_spawn_point_collection.asset

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 along the way.

Takeaway

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 inbuilt 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!

--

--