- Home /
Don't save gameobjects to scene file (No hide flags)
I am coding a terrain system that doesn't use the unity terrain engine. It works well but currently I am having problems with the terrain geometry being saved to the scene file The terrain geometry currently is over 200mb (!) when saved to the scene including all the associated splat maps and materials that go with it.
So I am looking for a way to not save the terrain tile game objects to the scene and instead load them every time unity opens by generating it from the heightmap rather than the raw meshes which uses much less data. The reason the scene cannot be such a large file is github has a 100mb limit of files and this means I cannot commit the scene to my repo, also saving the scene with the geometry in it takes longer than choosing not to save it.
Previously I was using HideFlags.DontSave however this caused issues with serialisation between the play mode and edit mode which caused the terrain geometry references to go null whenever I transitioned between these states.
Currently I have this component to place tiles in the game:
[ExecuteInEditMode]
[AddComponentMenu("Terrain/Tile Placer")]
public class TilePlacer : MonoBehaviour {
public int terrainSize = 8;
public GameObject terrainFab;
public List<GameObject> tiles;
public HeightMap heightMap;
public SplatMap splatMap;
public bool dirty;
public bool needsUpdate = false;
void Awake()
{
dirty = false;
DontDestroyOnLoad (gameObject);
if (tiles == null)
{
Debug.Log ("RECREATE LIST");
tiles = new List<GameObject>();
}
heightMap = GetComponent<HeightMap>();
}
void OnEnable()
{
bool missingTerrain = false;
for (int i = 0; i < tiles.Count; i++)
{
if (tiles[i] == null)
{
missingTerrain = true;
}
}
Debug.Log ("MISSING TERRAIN " + missingTerrain);
Debug.Log ("TILE COUNT " + tiles.Count + " SHOULD BE: " + terrainSize * terrainSize);
if (tiles.Count != terrainSize * terrainSize || missingTerrain)
{
//StartCoroutine( PlaceTerrain() );
MarkDirty ();
}
}
public void MarkDirty()
{
dirty = true;
if (Application.isEditor)
{
Debug.Log ("call dirty");
splatMap.CreateMap(heightMap.heightGrad);
PlaceTerrain ();
dirty = false;
}
}
public void ClearTerrain()
{
Debug.Log ("CLEAR TERRAIN");
foreach(GameObject tile in tiles)
{
DestroyImmediate(tile);
}
tiles.Clear ();
}
public void PlaceTerrain()
{
ClearTerrain ();
Debug.Log ("PLACE TERRAIN");
for (int x=0; x < terrainSize; x++)
{
for (int z=0; z < terrainSize; z++)
{
//Place tile
}
}
}
}
When using HideFlags.DontSave the List tiles would reference to null objects when serializing which meant MarkDirty() would get called straight away and the whole terrain mesh would have to reload itself which again would take a lot of time to load the scene.
So is there a way to have a similar behaviour to DontSave but the gameobject still serializes but never saves to the .scene file?
Or should hideflags not mess with the serialisation and I am just using them incorrectly.
I don't suppose it would be as simple as marking your tiles List as [NonSerialized]?