Spawn Doctor: Part Two

Photo by Joshua Newton on Unsplash

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.

https://medium.com/media/486be36433f09ef484a59ec0462c4c04/href

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.

https://medium.com/media/84bf368c2550bf6c830f4f88ef675f13/href

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

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

https://medium.com/media/e942c03a347a0ee165524712e028661e/href

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.

https://medium.com/media/deb28e5b177d13763c91ff577f8aca9a/href

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.

Almost there…

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)

https://medium.com/media/451e466bbfca83bb2cbe0f6c46956d99/href

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.

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


Spawn Doctor: Part Two was originally published in Mighty Bear Games on Medium, where people are continuing the conversation by highlighting and responding to this story.