Hey everyone. Im working on a minecraft style engine for Unity. I already wrote one, but im rewriting it to be faster. I have a class called TerrainGenerator, which is obviously for generating terrain. But i want the user to be able to write their own TerrainGenerator. They can make a class that inherits from TerrainGenerator, and use that class for generating the terrain. I’m trying to make this all very simple for the user. So I want to be able to set this in the inspector of my World class.
Heres an example of my base class
public class TerrainGenerator {
public static TerrainGenerator instance;
public virtual void GenerateTerrain() {}
}
And a derived class made by the user
public class FlatTerrainGenerator : TerrainGenerator {
public override void GenerateTerrain () {
//Generates flat terrain
}
}
And here’s what I want to do
public class World {
//somehow set TerrainGenerator to FlatTerrainGenerator in inspector
void Awake () {
//if desired TerrainGenerator is set to FlatTerrainGenerator
TerrainGenerator.instance = new FlatTerrainGenerator ();
}
void Start () {
//generate terrain using desired terrain generator
TerrainGenerator.instance.GenerateTerrain ();
}
}
I can easily make TerrainGenerator inherit from Monobehaviour, then stick an instance of FlatTerrainGenerator on a GameObject. Then drag and drop that component into a field in the World inspector. But that’s an ugly solution. I’m looking for a clean and simple way for the user to change the instance of terrainGenerator. Its easy to do in code, but I would like it to be assigned in the editor because of simplicity and the fact that the TerrainGenerator should not be changed during runtime, it only needs to be serialized as a variable in the World component.