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.
I was thinking maybe I could have the Inspector variable a Type. Then the user could set the type to FlatTerrainGenerator. But I don't know if this would work. I can't test that out until tonight because I'm at work
– janzdottYou didn't ask a question in your question, but I suspect you want to know how to create a valid plug-in architecture. If that's the case, here's a great place to start - it's c# in general, not Unity in specific, but it still applies. http://www.codeproject.com/Articles/6334/Plug-ins-in-C
– DracoratHaha sorry I thought my question was implied when I explained what I'm trying to do. I'll read up on that plugin tutorial when I get a chance and see if I learn something useful!
– janzdottI just realized I had linked a VB .Net article - sorry about that - I just changed the link to a C# article.
– DracoratWell there's some useful information there, but it doesn't help me solve my problem. I've edited my question to better explain what I'm trying to do
– janzdott