In our previous tutorial about Tilemaps we are using interfaces to pass along to the Generate method to create our tilemap data.
In this topic I’d like to expand on this and use ScriptableObjects for future algorithms that can be applied on top of eachother.
I will create an abstract base class for each algorithm we will make in the future.
That will look something like this:
public abstract class AlgorithmBase : ScriptableObject
{
public abstract void Apply(TilemapStructure tilemap);
}
In this topic I don’t wanna expand on any new algorithms, so I’m just gonna use the same RandomGeneration we did in the Tilemaps tutorial, but change it so it works as a ScriptableObject.
I will rename RandomDataGenerator class to RandomGenerationAlgorithm and
let it be derived from AlgorithmBase.
Also we add an attribute ‘CreateAssetMenu’ this will allow us to be able to create a scriptableobject of this type from the menu in the editor.
[CreateAssetMenu(fileName = "RandomGeneration", menuName = "Algorithms/RandomGeneration")]
public class RandomGenerationAlgorithm : AlgorithmBase
{
public override void Apply(TilemapStructure tilemap)
{
var validEnumValues = (GroundTileType[])Enum.GetValues(typeof(GroundTileType));
for (int x = 0; x < tilemap.Width; x++)
{
for (int y = 0; y < tilemap.Height; y++)
{
// We use Unity's Random class to retrieve a random value
// between 0 and the length of the enums available.
// Cast enum random value to the underlying int,
// we defined in the enum type.
var randomValue = (int)validEnumValues[UnityEngine.Random.Range(0, validEnumValues.Length)];
// We set the tile type into our tilemap data structure
tilemap.SetTile(x, y, randomValue);
}
}
}
}
We can remove the interface IWorldGen and inside TilemapStructure.cs in our Generate(IWorldGen generator) method, replace IWorldGen generator parameter with
AlgorithmBase algorithm parameter, like so:
public void Generate(AlgorithmBase algorithm)
{
algorithm.Apply(this);
}
Back in our TilemapStructure.cs class we still have a problem, we call Generate method but we need to still pass our ScriptableObject, to do this we need to make one.
Let’s make an array of algorithms, that we want to apply one after the other.
So they can work onto each other, (etc spawn trees algorithm, grouping algorithm to group trees, spawn rivers algorithm etc)
[SerializeField]
private AlgorithmBase[] _algorithms;
In our awake we then loop over these algorithms and pass them to our Generate method in the same order.
// Apply all the algorithms to the tilemap
foreach (var algorithm in _algorithms)
{
Generate(algorithm);
}
Then back in unity, let’s make a scriptableobject through the menu.
(menu: Assets -> Create -> Algorithms -> RandomGeneration)
Since this one in particular doesn’t have any configuration, we don’t need to
do anything with this scriptable object, except assign it to our tilemap’s Algorithms array.


Now you can easily create new algorithms and assign them in the inspector to your tilemaps, without having to change the Generate method in the TilemapStructure everytime + a more convenient way to configure your algorithms.
Starting up the game, should give you the exact same result from the previous Tilemaps tutorial. We will be using this format to expand our tilemap in future tutorials such as Perlin Noise algorithm etc.
Github repository commit of this tutorial can be found here:
https://github.com/Venom0us/Code2DTutorials/commit/e42920ff9f292b207b043e9e3425d455cc86448a
Source files:
https://github.com/Venom0us/Code2DTutorials/releases/tag/v2
