//Explore an area public void ExploreArea(Vector3 position, float exploreSize) { Vector3 pos = ConvertPosition(position); WhitenTerrain(pos, ExplorationSize); try { List <GameObject> gos = new List <GameObject> (); if (!string.IsNullOrEmpty(ExploreTag1)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag1)); } if (!string.IsNullOrEmpty(ExploreTag2)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag2)); } if (!string.IsNullOrEmpty(ExploreTag3)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag3)); } if (!string.IsNullOrEmpty(ExploreTag4)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag4)); } foreach (GameObject item in gos) { float aux = Vector3.Distance(item.transform.position, pos); if (aux < (ExplorationSize / 2)) { item.GetComponent <Renderer>().enabled = true; } } } catch (Exception ex) { Debug.Log(ex.Message); } List <TreeInstance> treesAux = new List <TreeInstance> (); for (int i = 0; i < treesInstance.Count; i++) { TreeInstance ti = treesInstance [i]; TreeInstance currentTree = ti; Vector3 currentTreeWorldPosition = Vector3.Scale(currentTree.position, Terrain.activeTerrain.terrainData.size) + Terrain.activeTerrain.transform.position; float aux = Vector3.Distance(currentTreeWorldPosition, pos); if (aux < (ExplorationSize / 2)) { TreeAux ta = treeAux.FirstOrDefault(n => n.Index == i); if (ta != null) { ti.prototypeIndex = ta.Prototype; } } treesAux.Add(ti); } Terrain.activeTerrain.terrainData.treeInstances = treesAux.ToArray(); treesInstance = treesAux; }
//Hide the trees void Start() { Current = this; detailBkp = new List <int[, ]> (); detailAux = new List <int[, ]> (); //TerrainData------------------------------ terr = this.GetComponent <Terrain> (); if (terr != null && terr.terrainData != null) { hmWidth = terr.terrainData.heightmapWidth; hmHeight = terr.terrainData.heightmapHeight; alphaMapWidth = terr.terrainData.alphamapWidth; alphaMapHeight = terr.terrainData.alphamapHeight; numOfAlphaLayers = terr.terrainData.alphamapLayers; splats = terr.terrainData.splatPrototypes.Where(n => n != null && n.texture != null).ToList(); int splatLenth = splats.Count + 4; numOfAlphaLayers = splatLenth; SplatPrototype[] test = new SplatPrototype[splatLenth]; for (int i = 0; i < splatLenth; i++) { if (i < splats.Count) { test [i] = terr.terrainData.splatPrototypes [i]; } else { test [i] = new SplatPrototype(); if (i == splats.Count) { test [i].texture = (Texture2D)Resources.Load("tblue", typeof(Texture2D)); } else if (i == splats.Count + 1) { test [i].texture = (Texture2D)Resources.Load("tgreen", typeof(Texture2D)); } else if (i == splats.Count + 2) { test [i].texture = (Texture2D)Resources.Load("tred", typeof(Texture2D)); } else if (i == splats.Count + 3) { test [i].texture = (Texture2D)Resources.Load("twhite", typeof(Texture2D)); } test [i].tileOffset = new Vector2(0, 0); test [i].tileSize = new Vector2(15, 15); } } terr.terrainData.splatPrototypes = test; if (Debug.isDebugBuild) { heightMapBackup = terr.terrainData.GetHeights(0, 0, hmWidth, hmHeight); alphaMapBackup = terr.terrainData.GetAlphamaps(0, 0, alphaMapWidth, alphaMapHeight); } BlackTerrain(); } //-------------------------------- if (Debug.isDebugBuild) { if (Terrain.activeTerrain != null && Terrain.activeTerrain.terrainData != null) { treeDefault = Terrain.activeTerrain.terrainData.treeInstances.ToList(); treePrototypes = Terrain.activeTerrain.terrainData.treePrototypes.ToList(); int nLayers = Terrain.activeTerrain.terrainData.detailPrototypes.Length; for (int i = 0; i < nLayers; i++) { detailBkp.Add(Terrain.activeTerrain.terrainData.GetDetailLayer(0, 0, terr.terrainData.detailWidth, terr.terrainData.detailHeight, i)); } } } if (Application.isPlaying) { try { List <GameObject> gos = new List <GameObject> (); if (!string.IsNullOrEmpty(ExploreTag1)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag1)); } if (!string.IsNullOrEmpty(ExploreTag2)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag2)); } if (!string.IsNullOrEmpty(ExploreTag3)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag3)); } if (!string.IsNullOrEmpty(ExploreTag4)) { gos.AddRange(GameObject.FindGameObjectsWithTag(ExploreTag4)); } foreach (GameObject item in gos) { item.GetComponent <Renderer>().enabled = false; } } catch (Exception ex) { Debug.Log(ex.Message); } if (Terrain.activeTerrain != null && Terrain.activeTerrain.terrainData != null) { int nLayers = Terrain.activeTerrain.terrainData.detailPrototypes.Length; int[,] hds = new int[terr.terrainData.detailWidth, terr.terrainData.detailHeight]; for (int i = 0; i < nLayers; i++) { detailAux.Add(terr.terrainData.GetDetailLayer(0, 0, terr.terrainData.detailWidth, terr.terrainData.detailHeight, i)); } for (int y = 0; y < terr.terrainData.detailWidth; y++) { for (int x = 0; x < terr.terrainData.detailHeight; x++) { hds [x, y] = 0; } } for (int i = 0; i < nLayers; i++) { Terrain.activeTerrain.terrainData.SetDetailLayer(0, 0, i, hds); } List <TreePrototype> trees = new List <TreePrototype> (Terrain.activeTerrain.terrainData.treePrototypes); List <TreePrototype> prototypes = new List <TreePrototype> (); TreePrototype tp = new TreePrototype(); tp.prefab = (GameObject)Resources.Load("EmptyTree"); prototypes.Add(tp); foreach (TreePrototype item in trees) { prototypes.Add(item); } Terrain.activeTerrain.terrainData.treePrototypes = prototypes.Where(n => n != null).ToArray(); List <TreeInstance> treesIns = new List <TreeInstance> (Terrain.activeTerrain.terrainData.treeInstances); treesInstance = new List <TreeInstance> (treesIns); for (int i = 0; i < treesIns.Count; i++) { TreeInstance ti = treesIns [i]; TreeAux ta = new TreeAux(); ta.Index = i; ta.Prototype = ti.prototypeIndex + 1; treeAux.Add(ta); } try { List <TreeInstance> tis = new List <TreeInstance> (); for (int i = 0; i < Terrain.activeTerrain.terrainData.treeInstances.Length; i++) { TreeInstance tiii = Terrain.activeTerrain.terrainData.treeInstances [i]; tiii.prototypeIndex = 0; tis.Add(tiii); } Terrain.activeTerrain.terrainData.treeInstances = tis.ToArray(); } catch (Exception ex) { Debug.Log(ex.Message); } } } }