// ################################################################################################################### // ############### Load method ####################################################################################### // ################################################################################################################### public void Load(CTYPES.CHUNK_DATA chunkData) { Terrain[] childs = transform.parent.GetComponentsInChildren <Terrain>(); biome = chunkData.biome; GenerateBiome(); foreach (string neighbor in chunkData.neighBors) { if (neighbor == null) { NeighBors.Add(null); } else { bool found = false; int i = 0; while (!found) { if (neighbor == childs[i].name) { NeighBors.Add(childs[i]); found = true; } else { ++i; } } } } }
// ################################################################################################################## // ###################################### Save/Load Methods ######################################################### // ################################################################################################################## public override void Save(string _path) { List <CTYPES.CHUNK_DATA> arrChunks = new List <CTYPES.CHUNK_DATA>(); foreach (Chunk chunk in _chunks) { CTYPES.CHUNK_DATA chunkData = chunk.chunkTerrain.GetComponent <TerrainManager>().Serialize(); arrChunks.Add(chunkData); } CTYPES.TERRAIN_DATA terrainData = new CTYPES.TERRAIN_DATA(terrainResolution, chunksX, chunksY); CTYPES.WORLD_STATE state = new CTYPES.WORLD_STATE(builded, texturized, noised, stitched, transitioned); CTYPES.WORLD_DATA worldData = new CTYPES.WORLD_DATA(terrainData, arrChunks, state); SaveHandler.saveObject = worldData; SaveHandler.Save(_path, true); }
public override void Load(string _path) { DestroyChilds(); SaveHandler.Load(_path, true); CTYPES.WORLD_DATA worldData = (CTYPES.WORLD_DATA)SaveHandler.deserializedObject; CTYPES.WORLD_STATE state = (CTYPES.WORLD_STATE)worldData.state; CTYPES.TERRAIN_DATA terrainData = worldData.terrainData; _chunks = new Chunk[terrainData.nGridsX, terrainData.nGridsY]; builded = state.builded; texturized = state.texturized; noised = state.noised; stitched = state.stitched; transitioned = state.transitioned; Transitioner.Instance.firstTransition = false; int i = 0; chunksX = terrainData.nGridsX; chunksY = terrainData.nGridsY; // Load all terrain grid for (int x = 0; x < terrainData.nGridsX; ++x) { for (int y = 0; y < terrainData.nGridsY; ++y) { _chunks[x, y] = new Chunk(); CTYPES.CHUNK_DATA chunkData = worldData._chunks[i]; TerrainData newTerrainData = new TerrainData(); UnityEditor.AssetDatabase.CreateAsset(newTerrainData, Paths.SavedWorlds + "Terrains/" + chunkData.name); _chunks[x, y].chunkTerrain = Terrain.CreateTerrainGameObject(newTerrainData).GetComponent <Terrain>(); _chunks[x, y].chunkTerrain.name = chunkData.name; _chunks[x, y].chunkTerrain.transform.position = new Vector3(chunkData.pos.x, chunkData.pos.y, chunkData.pos.z); _chunks[x, y].chunkTerrain.transform.parent = gameObject.transform; _chunks[x, y].chunkTerrain.terrainData.heightmapResolution = terrainData.resolution.heightMapResolution; _chunks[x, y].chunkTerrain.terrainData.size = new Vector3(terrainData.resolution.terrainSize, terrainData.resolution.terrainHeight, terrainData.resolution.terrainSize); _chunks[x, y].chunkTerrain.terrainData.SetDetailResolution(terrainData.resolution.detailResolution, terrainData.resolution.resolutionPerPatch); _chunks[x, y].chunkTerrain.detailObjectDensity = terrainData.resolution.detailObjectDensity; _chunks[x, y].chunkTerrain.detailObjectDistance = terrainData.resolution.detailObjectDistance; _chunks[x, y].chunkTerrain.terrainData.SetHeights(0, 0, chunkData.heights); _chunks[x, y].chunkTerrain.gameObject.AddComponent <TerrainManager>().chunk = _chunks[x, y]; ++i; } } // Load data like biome, premade and neighbors; for each chunck foreach (Chunk chunk in _chunks) { CTYPES.CHUNK_DATA chunkData = worldData._chunks[0]; chunk.chunkTerrain.GetComponent <TerrainManager>().Load(chunkData); worldData._chunks.RemoveAt(0); } DestroyImmediate(GameObject.Find(TerrainName)); }