Search Unity

Question New trees for terrain (Programmaticaly)

Discussion in 'World Building' started by fheronax, Mar 5, 2023.

  1. fheronax

    fheronax

    Joined:
    May 2, 2020
    Posts:
    13
    How it infuriates me that the documentation of the unit is very stingy and complicated.

    Hi all. I'm doing random terrain generation, I stumbled upon a problem (I don't understand how it works):
    There is a terrain, inside the terrain there is a Paint trees tab.
    upload_2023-3-5_20-20-39.png
    How to programmatically put tree prefabs here? I turned to ChatGpt, he only gave an example with TreeInstance, TreePrototype ... But nowhere is it indicated that the terrain will have the prefab of the tree that I assign to it. I also searched on google and didn't find anything useful.
     

    Attached Files:

  2. fheronax

    fheronax

    Joined:
    May 2, 2020
    Posts:
    13
    For those who face the same issue
    I solved it this way



    Code (CSharp):
    1.  
    2.     TerrainData terrainData = terrain.terrainData;
    3.     GameObject[] loadedAssets = Resources.LoadAll<GameObject>("Prefabs/Trees"); //getting tree prefabs from project // folder
    4.     List<TreePrototype> treePrototypes = new List<TreePrototype>();
    5.  
    6.     foreach (GameObject tree in loadedAssets)
    7.     {
    8.       TreePrototype treePrototype = new TreePrototype();
    9.       treePrototype.prefab = tree;
    10.       treePrototypes.Add(treePrototype);
    11.     }
    12.     terrainData.treePrototypes = treePrototypes.ToArray();
    13.