- Home /
Question by
Marek_Bakalarczuk · Nov 20, 2014 at 07:15 AM ·
terraingrassterraindata
Save terrain grass data
Hi,
I have no idea how to save Unity terrain grass placement data. I can store every other data but not this. Here's my code
TerrainData tdata = new TerrainData ();
tdata.heightmapResolution = ter.terrainData.heightmapResolution;
tdata.size = ter.terrainData.size;
tdata.alphamapResolution = ter.terrainData.alphamapResolution;
tdata.baseMapResolution = ter.terrainData.baseMapResolution;
tdata.SetDetailResolution (ter.terrainData.detailResolution, 8);
// Generate the heightmap
tdata.SetHeights (0, 0, ter.terrainData.GetHeights (0, 0, ter.terrainData.heightmapWidth, ter.terrainData.heightmapHeight));
tdata.splatPrototypes = ter.terrainData.splatPrototypes;
tdata.treePrototypes = ter.terrainData.treePrototypes;
tdata.treeInstances = ter.terrainData.treeInstances;
tdata.wavingGrassAmount = ter.terrainData.wavingGrassAmount;
tdata.wavingGrassSpeed = ter.terrainData.wavingGrassSpeed;
tdata.wavingGrassStrength = ter.terrainData.wavingGrassStrength;
tdata.wavingGrassTint = ter.terrainData.wavingGrassTint;
tdata.GetDetailLayer(0, 0, ter.terrainData.detailWidth, ter.terrainData.detailHeight, 0);
AssetDatabase.CreateAsset(tdata, "Assets/Terrains/"+ter.name+".asset");
AssetDatabase.SaveAssets();
// Now if we have splatmaps for this terrain, time to load them
float[,,] splatarray = ter.terrainData.GetAlphamaps(0,0,ter.terrainData.alphamapWidth-1, ter.terrainData.alphamapHeight-1);
SplatPrototype[] prototypes = ter.terrainData.splatPrototypes;
Debug.Log (ter.terrainData.detailPrototypes.Length);
details = new List<DetailPrototype>();
for(int i = 0; i < ter.terrainData.detailPrototypes.Length; i++) {
DetailPrototype prototype = new DetailPrototype();
prototype.prototype = ter.terrainData.detailPrototypes[i].prototype;
prototype.prototypeTexture = ter.terrainData.detailPrototypes[i].prototypeTexture;
prototype.dryColor = ter.terrainData.detailPrototypes[i].dryColor;
prototype.healthyColor = ter.terrainData.detailPrototypes[i].healthyColor;
prototype.noiseSpread = ter.terrainData.detailPrototypes[i].noiseSpread;
prototype.renderMode = ter.terrainData.detailPrototypes[i].renderMode;
prototype.usePrototypeMesh = ter.terrainData.detailPrototypes[i].usePrototypeMesh;
prototype.minWidth = ter.terrainData.detailPrototypes[i].minWidth;
prototype.maxWidth = ter.terrainData.detailPrototypes[i].maxWidth;
prototype.minHeight = ter.terrainData.detailPrototypes[i].minHeight;
prototype.maxHeight = ter.terrainData.detailPrototypes[i].maxHeight;
details.Add(prototype);
}
TerrainData td = (TerrainData)AssetDatabase.LoadAssetAtPath("Assets/Terrains/"+ter.name+".asset", typeof(TerrainData));
td.splatPrototypes = prototypes;
td.detailPrototypes = details.ToArray();
td.RefreshPrototypes ();
td.SetAlphamaps (0, 0, splatarray);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log (td.detailPrototypes.Length);
ter.GetComponent<Terrain> ().terrainData = td;
ter.GetComponent<TerrainCollider> ().terrainData = td;
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613172004im_/https://answers.unity.com/themes/thub/images/avi.jpg)