Procedural terrain and the editormode

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

Hi Morgan,

I’m sorry I cannot provide more detail right now as I’m about to leave, however I will leave you with a link which will shed a lot of light on exactly what you’re experiencing when switching between play and edit mode, what happens why.

This is a good thread to keep in mind in general and certainly your “go to” for understanding what’s happening:

http://forum.unity3d.com/threads/155352-Serialization-Best-Practices-Megapost

I hope this at least provides some insight and sets you off on the right course.

Generally, when you go from edit mode to playmode, it serializes and saves everything, then you play, then when you exit play mode, it does a sort of global “undo” and reverts everything to the saved serialize version. However, terrain is bugged and doesn’t work like that. Terrain doesn’t get its own separate serialized version in play mode. Any changes you make to terrain while in play mode are made directly to the terrain file and remain when you exit play mode. This happens regardless of any [ExecuteInEditMode] attributes or anything else on the terrain.

The functions like OnEnable that you mention are called differently when ExecuteInEditMode is on and it’s in editor mode; some are mentioned here:

“Awake” is called in edit mode when first opening the scene in Unity but not afterwards. I believe OnEnable is only called in edit mode if you disable and then re-enable a component by clicking the checkbox. Update’s not called every frame, only when you modify a noticable component.

Thanks for your help, especially the link darkarts provided is really awesome. It’s always best to understand what’s going on under the hood, even if asking for implementation-specifics could lead to faster results. I couldn’t test it out yet but i think i have all information i need to turn it into a solid system. Unfortunately the documention is a bit sparse as it doesn’t give a lot of insight into the inner workings of unity. I had already read the link you provided, makeshiftwings, but it doesn’t go into detail which events are fired and when exactly this happens (OnEnable and Start are not even mentioned so it seems like they are called the same way as without [ExecuteInEditmode]). But, yeah, i’m pretty new to unity and have to learn a lot of stuff. I’ll report back later after testing it out.