Our game is a infinite procedural world. On delete and regen of areas we often got a Pure Virtual Function crash.
It was caused by the Unity Tree system where we created a few zillion trees, then later deleted them all and created more trees on the same terrain instance, but after changing altitudes etc. to represent a new area of the game.
Our code was like:
...terrainData.treeInstances = new TreeInstance[0];
to clear the trees. Then lots of:
myTerrain.AddTreeInstance(ti);
The problem was that the time between the clearing of the terrain and the first tree add was several ticks.
When the garbage collector kicks in it calls any needed destructors, and somewhere in the Tree system a virtual function call is tried in a destructor. (that is bad to do in C#)
The solution is the following class...
public class TreeBugFix {
static bool isCleared = true;
public static void ClearTrees()
{
isCleared = true;
}
public static void AddTreeInstance(TreeInstance t)
{
if(isCleared)
{
isCleared = false;
TheDeadLinger.tdl.globalTerrain.terrainData.treeInstances = new TreeInstance[0];
}
TheDeadLinger.tdl.globalTerrain.AddTreeInstance(t);
}
}
This makes the first tree get created before the garbage collector can kick in avoiding the crash.
↧