public Spawner CreateSpawner(bool autoAddResources = false, Transform targetTransform = null) { //Find or create gaia GameObject gaiaObj = GaiaUtils.GetGaiaGameObject(); GameObject spawnerObj = new GameObject(this.name); spawnerObj.AddComponent <Spawner>(); if (targetTransform != null) { spawnerObj.transform.parent = targetTransform; } else { spawnerObj.transform.parent = gaiaObj.transform; } Spawner spawner = spawnerObj.GetComponent <Spawner>(); spawner.LoadSettings(this); //spawner.m_settings.m_resources = (GaiaResource)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(this.m_resourcesGUID), typeof(GaiaResource)); if (autoAddResources) { TerrainLayer[] terrainLayers = new TerrainLayer[0]; DetailPrototype[] terrainDetails = new DetailPrototype[0]; TreePrototype[] terrainTrees = new TreePrototype[0]; GaiaDefaults.GetPrototypes(new List <BiomeSpawnerListEntry>() { new BiomeSpawnerListEntry() { m_spawnerSettings = this, m_autoAssignPrototypes = true } }, ref terrainLayers, ref terrainDetails, ref terrainTrees, Terrain.activeTerrain); foreach (Terrain t in Terrain.activeTerrains) { GaiaDefaults.ApplyPrototypesToTerrain(t, terrainLayers, terrainDetails, terrainTrees); } } //We need to check the texture prototypes in this spawner against the already created terrain layers for this session //- otherwise the spawner will not know about those in subsequent spawns and might create unneccessary additional layers //Get a list of all exisiting Terrain Layers for this session string path = GaiaDirectories.GetTerrainLayerPath(); #if UNITY_EDITOR AssetDatabase.ImportAsset(path); if (Directory.Exists(path)) { string[] allLayerGuids = AssetDatabase.FindAssets("t:TerrainLayer", new string[1] { path }); List <TerrainLayer> existingTerrainLayers = new List <TerrainLayer>(); foreach (string guid in allLayerGuids) { try { TerrainLayer layer = (TerrainLayer)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(TerrainLayer)); if (layer != null) { existingTerrainLayers.Add(layer); } } catch (Exception ex) { if (ex.Message == "") { } } } foreach (SpawnRule sr in spawner.m_settings.m_spawnerRules) { if (sr.m_resourceType == SpawnerResourceType.TerrainTexture) { ResourceProtoTexture protoTexture = spawner.m_settings.m_resources.m_texturePrototypes[sr.m_resourceIdx]; //if a terrainLayer with these properties exist we can assume it fits to the given spawn rule TerrainLayer terrainLayer = existingTerrainLayers.FirstOrDefault(x => x.diffuseTexture == protoTexture.m_texture && x.normalMapTexture == protoTexture.m_normal && x.tileOffset == new Vector2(protoTexture.m_offsetX, protoTexture.m_offsetY) && x.tileSize == new Vector2(protoTexture.m_sizeX, protoTexture.m_sizeY) ); if (terrainLayer != null) { protoTexture.m_LayerGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(terrainLayer)); } } } } #endif foreach (SpawnRule rule in spawner.m_settings.m_spawnerRules) { rule.m_spawnedInstances = 0; } if (Terrain.activeTerrains.Where(x => !TerrainHelper.IsWorldMapTerrain(x)).Count() > 0) { spawner.FitToAllTerrains(); } //else //{ // spawner.FitToTerrain(); //} return(spawner); }
public BiomeController CreateBiome(bool autoAssignPrototypes) { int totalSteps = m_spawnerPresetList.Count; int currentStep = 0; #if !UNITY_POST_PROCESSING_STACK_V2 //Workaround to disable warnings for "unused" field m_postProcessingProfileGUID //This is just a "harmless" piece of code to make the compiler think the field is in use if (m_postProcessingProfileGUID == "") { currentStep = 0; } #endif GaiaSessionManager sessionManager = GaiaSessionManager.GetSessionManager(); Transform gaiaTransform = GaiaUtils.GetGaiaGameObject().transform; GameObject newGO = new GameObject(); newGO.name = this.name + " Biome"; newGO.transform.parent = gaiaTransform; BiomeController biomeController = newGO.AddComponent <BiomeController>(); RefreshSpawnerListEntries(); //Track created spawners List <Spawner> createdSpawners = new List <Spawner>(); foreach (BiomeSpawnerListEntry spawnerListEntry in m_spawnerPresetList) { if (spawnerListEntry != null) { if (spawnerListEntry.m_spawnerSettings != null) { createdSpawners.Add(spawnerListEntry.m_spawnerSettings.CreateSpawner(false, biomeController.transform)); //GaiaUtils.DisplayProgressBarNoEditor("Creating Tools", "Creating Biome " + this.name, ++currentStep / totalSteps); if (ProgressBar.Show(ProgressBarPriority.CreateBiomeTools, "Creating Biome", "Creating Tools", ++currentStep, totalSteps, false, true)) { break; } } } } if (createdSpawners.Count > 0) { biomeController.m_settings.m_range = createdSpawners[0].m_settings.m_spawnRange; } for (int i = 0; i < createdSpawners.Count; i++) { Spawner spawner = createdSpawners[i]; biomeController.m_autoSpawners.Add(new AutoSpawner() { isActive = m_spawnerPresetList[i].m_isActiveInBiome, status = AutoSpawnerStatus.Initial, spawner = spawner }); } if (autoAssignPrototypes) { //prepare resource prototype arrays once, so the same prototypes can be added to all the tiles. TerrainLayer[] terrainLayers = new TerrainLayer[0]; DetailPrototype[] terrainDetails = new DetailPrototype[0]; TreePrototype[] terrainTrees = new TreePrototype[0]; GaiaDefaults.GetPrototypes(m_spawnerPresetList.Where(x => x.m_autoAssignPrototypes == true).ToList(), ref terrainLayers, ref terrainDetails, ref terrainTrees, Terrain.activeTerrain); foreach (Terrain t in Terrain.activeTerrains) { GaiaDefaults.ApplyPrototypesToTerrain(t, terrainLayers, terrainDetails, terrainTrees); } } if (GaiaUtils.CheckIfSceneProfileExists()) { GaiaGlobal.Instance.SceneProfile.CullingProfile = GaiaSceneCullingProfile; } #if UNITY_POST_PROCESSING_STACK_V2 biomeController.m_postProcessProfile = postProcessProfile; #endif ProgressBar.Clear(ProgressBarPriority.CreateBiomeTools); return(biomeController); }