/// <summary> /// Run this safe method if you want to edit anything on the specific tree instance. /// This will cause the changes to be saved over the chunks and it will be "safe". /// </summary> /// <param name="treeInstance">the tree instance</param> /// <param name="treeInstanceID">The id of the tree instance</param> /// <param name="action">the action</param> public static void RunActionOnTreeInstance(this Terrain terrain, TreeInstance treeInstance, int treeInstanceID, System.Func <TreeInstance, TreeInstance> action, System.Action <Sectors.ChunkObject> runOnChunk) { UNTerrain baseTerrain = terrain.GetComponent <UNTerrain>(); if (baseTerrain != null) { TIChunk chunk = baseTerrain.sector.getChunk(treeInstance.position.LocalToWorld(terrain.terrainData.size, Vector3.zero), 0) as TIChunk; if (chunk != null) { if (action != null) { TreeInstance result = action(treeInstance); if (chunk != null) { for (int i = 0; i < chunk.objects.Count; i++) { if (chunk.objects[i].instanceID == treeInstanceID) { chunk.objects[i].treeInstance = result; // update tree instance if (runOnChunk != null) { runOnChunk(chunk.objects[i]); } break; } } } } } } }
/// <summary> /// Restore a certain tree instance from the terrain. /// </summary> /// <param name="treeInstanceID">the tree instance id of the tree you wish to restore.</param> /// <param name="originalHeight">the original height of the targeted tree.</param> public static void RestoreTreeInstance(this TerrainData terrainData, int treeInstanceID, UNTerrain UNTerrain) { TreeInstance treeInstance = terrainData.GetTreeInstance(treeInstanceID); Sectors.ChunkObject chunkObject; // Update the sector chunk... TIChunk chunk = UNTerrain.sector.getChunk(treeInstance.position.LocalToWorld(UNTerrain.terrainData.multiThreaded_terrainDataSize, Vector3.zero), 0) as TIChunk; if (chunk != null) { for (int i = 0; i < chunk.objects.Count; i++) { chunkObject = chunk.objects[i]; if (chunkObject.instanceID == treeInstanceID) { UNTerrain.Pool.TryResetOnUID(treeInstanceID, true); treeInstance.heightScale = chunkObject.originalHeight; terrainData.SetTreeInstance(treeInstanceID, treeInstance); chunkObject.treeInstance = treeInstance; UNSeeker seeker; for (int c = 0; c < UNSeeker.FReceivers.Count; c++) { seeker = UNSeeker.FReceivers[c] as UNSeeker; Targets.UNTarget.CheckTargets(seeker, seeker.seekingDistance); // check targets to verify colliders assigning } break; } } } }