- Home /
Creating TerrainData.Asset programatically
How do I create Terrain from script, I know about Terrain.CreateTerrainGameObject()
, but this function accept TerrainData
as an argument, and then by this forum topic the TerrainData
instance must already be in Project view.
My question is how do I create that TerrainData
instance from my script (without using Unity default Terrain tools). I'am planning to create custom heightmap importer for my terrain.
I'm not sure about this one (at all), but is there anything stopping you from calling 'new TerrainData()' and then assigning all the values to it at runtime? Or even, making a 'blank' default-settings TerrainData in your assets, instantiating that, and then doing your work using SetHeights() etc.?
Just read that forum topic- I can't really think of any reason why you can't just use that solution!
I can't use that solution because it need to create terrain the normal way, which involved user clicking Terrain menu then create terrain, then delete the terrain and re-use its generated TerrainData.asset in Asset Folder by script, what I need to do is to create TerrainData.asset file from script, I need to create that file from script because I need to create multiple TerrainData for multiple terrain.
Yes, but once you've created one TerrainData, you can duplicate it, and then modify the duplicate, can't you?
Answer by jonas-echterhoff · Oct 25, 2011 at 02:35 PM
Try this:
TerrainData terrainData = new TerrainData ();
const int size = 513;
terrainData.heightmapResolution = size;
terrainData.size = new Vector3 (2000, 600, 2000);
terrainData.heightmapResolution = 512;
terrainData.baseMapResolution = 1024;
terrainData.SetDetailResolution(1024, terrainData.detailResolutionPerPatch);
AssetDatabase.CreateAsset(terrainData, "Assets/New Terrain.asset");
I am following your example, but it generates a whole slew of CS1519 Unexpected Symbol errors. What's going wrong here?
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour {
ArrayList< GameObject > gameObjects = new ArrayList<GameObject>();
TerrainData _terrainData = new TerrainData ();
const int size = 513;
_terrainData.heightmapResolution = size;
_terrainData.size = new Vector3 ( 2000, 600, 2000 );
_terrainData.base$$anonymous$$apResolution = 512;
_terrainData.SetDetailResolution ( 1024, _terrainData.detailResolutionPerPatch );
AssetDatabase.CreateAsset ( _terrainData, "Assets/New Terrain.asset" );
}
I suppose I should add that the problem starts after the const declaration line. _terrainData is not recognized by auto complete. I've also pasted your code in place of $$anonymous$$e and get the same result. Each of the lines with _terrainData is highlighted red.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
copy smaller size terrain's data, and seet the heights on bigger Terrain at click position 0 Answers
How to generate multiple Terrain objects with different TerrainData and Splat Texture via C# script? 1 Answer
Problem with blending two terrains together on an edge. 1 Answer