Hi guys,
i’m fairly new to unity (but not gameprogramming in general). While implementing a procedural terrain system, I came across some problems. The system works as follows:
-generate a worldmap with some climatic simulation to create different biomes that are naturally placed over the map
-set some (more or less) random modifier-flags for every area
-with the worldmap set up you can calculate a heightfield for every area as needed
The rough structure of whole system looks like this:
-class worldgrid is responsible for generating the worldmap and defining the diefferent areas
-class terrainchunk creates a gameobject and adds all needed components to it (terrain-component for example)
-behaviour terrainsystem is responsible for calculating the areas, that are currently around the camera (and thus need to be drawn). It also has a queue in it holding all terrainchunks that need to get their heightfield calculated, so that only one of them is generated per frame. The last relevant part is an array of all terrainchunks. Those are created (but not initialized) in the OnEnable()-method.
Up to this point everything works as expected, i can walk around the map, heightfields are generated as needed (and also destroyed when out of sight/too far away).
What I want to achieve next is that I can also see the terrain in editormode, change some properties of the different areas and see those changes in realtime. When using the [ExecuteInEditMode]-attribute some problems arise. Sometimes unity just crashes without any hint why, i get a lot of NullReference-exceptions because the array of terrainchunks is null and generally it feels like there are some important points i’m missing. Another thing is that i don’t want to recalculate the worldmap on every playmode/editormode change and instead keep it in memory all the time. Of course i could serialize and deserialize it on every modechange but it would be a lot more comfortable to do it only when needed.
So my main questions are probably:
What exactly happens when leaving editormode and entering game mode and vice versa? Why are my objects like the terrainchunk-array released when switching modes? OnEnable seems to not be called when entering editormode, so what events are actually fired?
If you need some actual code just ask for it, I didn’t want to blow this up too much.
I would appreciate every help from you to make this whole process as smooth as possible. Thanks for reading!
Morgan