private static void ClearSpawnExtensionsOnSingleTerrain(Terrain terrain, Spawner spawner, ClearSpawnFrom clearSpawnFrom) { Spawner[] allAffectedSpawners; if (clearSpawnFrom == ClearSpawnFrom.OnlyThisSpawner) { allAffectedSpawners = new Spawner[1] { spawner }; } else { allAffectedSpawners = Resources.FindObjectsOfTypeAll <Spawner>(); } foreach (Spawner sp in allAffectedSpawners) { foreach (SpawnRule sr in sp.m_settings.m_spawnerRules) { sp.ClearSpawnExtensionsForRule(sr); Spawner.ClearGameObjectsForRule(sp, sr, false, terrain); } } }
private static void ClearTreesOnSingleTerrain(Terrain terrain, Spawner spawner, ClearSpawnFrom clearSpawnFrom) { if (spawner == null || clearSpawnFrom == ClearSpawnFrom.AnySource) { //No tree prototypes passed in => we delete any tree from any source terrain.terrainData.treeInstances = new TreeInstance[0]; } else { //We need to get the correct prototype Ids for this terrain only //Prototype Ids might be different from terrain to terrain, depending on when / how the prototype was added List <int> treePrototypeIds = new List <int>(); foreach (SpawnRule sr in spawner.m_settings.m_spawnerRules) { if (sr.m_resourceType == GaiaConstants.SpawnerResourceType.TerrainTree) { int treePrototypeIndex = spawner.m_settings.m_resources.PrototypeIdxInTerrain(sr.m_resourceType, sr.m_resourceIdx, terrain); if (treePrototypeIndex != -1) { treePrototypeIds.Add(treePrototypeIndex); } } } //Reapply the tree instances on this terrain, but leave all "to be deleted" ids out via the where clause terrain.terrainData.SetTreeInstances(terrain.terrainData.treeInstances.Where(x => !treePrototypeIds.Contains(x.prototypeIndex)).ToArray(), true); } terrain.Flush(); }
/// <summary> /// Clear all the details (grass) on all the terrains /// </summary> private static void ClearDetailsOnSingleTerrain(Terrain terrain, Spawner spawner, ClearSpawnFrom clearSpawnFrom) { int[,] details = new int[terrain.terrainData.detailWidth, terrain.terrainData.detailHeight]; if (spawner == null || clearSpawnFrom == ClearSpawnFrom.AnySource) { for (int dtlIdx = 0; dtlIdx < terrain.terrainData.detailPrototypes.Length; dtlIdx++) { terrain.terrainData.SetDetailLayer(0, 0, dtlIdx, details); } } else { //We need to get the correct prototype Ids for this terrain only //Prototype Ids might be different from terrain to terrain, depending on when / how the prototype was added List <int> detailPrototypeIds = new List <int>(); foreach (SpawnRule sr in spawner.m_settings.m_spawnerRules) { if (sr.m_resourceType == GaiaConstants.SpawnerResourceType.TerrainDetail) { int detailPrototypeIndex = spawner.m_settings.m_resources.PrototypeIdxInTerrain(sr.m_resourceType, sr.m_resourceIdx, terrain); if (detailPrototypeIndex != -1) { detailPrototypeIds.Add(detailPrototypeIndex); } } } for (int dtlIdx = 0; dtlIdx < detailPrototypeIds.Count; dtlIdx++) { terrain.terrainData.SetDetailLayer(0, 0, detailPrototypeIds[dtlIdx], details); } } terrain.Flush(); }
/// <summary> /// Clear all the trees on all the terrains /// </summary> public static void ClearSpawns(SpawnerResourceType resourceType, ClearSpawnFor clearSpawnFor, ClearSpawnFrom clearSpawnFrom, List <string> terrainNames = null, Spawner spawner = null) { if (terrainNames == null) { if (clearSpawnFor == ClearSpawnFor.AllTerrains) { if (GaiaUtils.HasDynamicLoadedTerrains()) { GaiaSessionManager sessionManager = GaiaSessionManager.GetSessionManager(); terrainNames = TerrainLoaderManager.TerrainScenes.Select(x => x.GetTerrainName()).ToList(); } else { terrainNames = Terrain.activeTerrains.Select(x => x.name).ToList(); } } else { terrainNames = new List <string> { spawner.GetCurrentTerrain().name }; } } string progressBarTitle = "Clearing..."; Action <Terrain> act = null; switch (resourceType) { case SpawnerResourceType.TerrainTexture: progressBarTitle = "Clearing Textures"; //Not supported, should not be required throw new NotSupportedException("Clearing of Textures is currently not supported via the terrain helper"); case SpawnerResourceType.TerrainDetail: progressBarTitle = "Clearing Terrain Details"; act = (t) => ClearDetailsOnSingleTerrain(t, spawner, clearSpawnFrom); break; case SpawnerResourceType.TerrainTree: progressBarTitle = "Clearing Trees"; act = (t) => ClearTreesOnSingleTerrain(t, spawner, clearSpawnFrom); break; case SpawnerResourceType.GameObject: progressBarTitle = "Clearing Game Objects"; act = (t) => ClearGameObjectsOnSingleTerrain(t, spawner, clearSpawnFrom); break; case SpawnerResourceType.Probe: progressBarTitle = "Clearing Probes"; act = (t) => ClearGameObjectsOnSingleTerrain(t, spawner, clearSpawnFrom); break; case SpawnerResourceType.SpawnExtension: progressBarTitle = "Clearing Spawn Extensions"; act = (t) => ClearSpawnExtensionsOnSingleTerrain(t, spawner, clearSpawnFrom); break; case SpawnerResourceType.StampDistribution: //Not supported, should not be required throw new NotSupportedException("Clearing of Stamps is currently not supported via the terrain helper"); } if (GaiaUtils.HasDynamicLoadedTerrains()) { GaiaUtils.CallFunctionOnDynamicLoadedTerrains(act, true, terrainNames); } else { for (int idx = 0; idx < terrainNames.Count; idx++) { ProgressBar.Show(ProgressBarPriority.Spawning, progressBarTitle, progressBarTitle, idx + 1, terrainNames.Count(), true); GameObject go = GameObject.Find(terrainNames[idx]); if (go != null) { Terrain terrain = go.GetComponent <Terrain>(); act(terrain); } ProgressBar.Clear(ProgressBarPriority.Spawning); } } }