Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Cleanup TreePrototypes list of terrain with small script

Discussion in 'Scripting' started by hww, Jun 12, 2013.

  1. hww

    hww

    Joined:
    Nov 23, 2007
    Posts:
    58
    Once, I need to remove all TreePrototypes which was not used. I did it with this script:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. // By ValeryAlex (hww)
    8.  
    9. public class CleanupTreePrototypes : ScriptableObject
    10. {
    11.  
    12.     [MenuItem ("Tools/Terrain/Cleanup Unused Trees")]
    13.     static void MenuAddChild()
    14.     {
    15.        
    16.         Terrain terrain = GameObject.FindObjectOfType(typeof(Terrain)) as Terrain;
    17.         Debug.Log("Found terrain " + terrain.name);
    18.        
    19.         TerrainData terrainData = terrain.terrainData;
    20.        
    21.         Dictionary<int,int> list = new Dictionary<int,int>() ;
    22.         for (int i = 0; i<terrainData.treePrototypes.Length; i++) {
    23.             list[i] = 0;
    24.         }
    25.         foreach (TreeInstance treeInstance in terrainData.treeInstances) {
    26.                 list[treeInstance.prototypeIndex] += 1;
    27.         }
    28.        
    29.         int cnt = 0;
    30.         List<TreePrototype> protos = new List<TreePrototype>();
    31.         string str = "";
    32.         for (int i = 0; i<list.Count; i++) {
    33.            
    34.             str += i.ToString() + " " + list[i].ToString() + "\n";
    35.  
    36.  
    37.             if (list[i]==0)
    38.                 list[i]=-1;
    39.             else {
    40.                 protos.Add(terrainData.treePrototypes[i]);
    41.                 list[i] = cnt++;
    42.             }
    43.         }
    44.        
    45.         Debug.Log(str);
    46.        
    47.         List<TreeInstance> trees = new List<TreeInstance>();
    48.        
    49.         foreach (TreeInstance treeInstance in terrainData.treeInstances) {
    50.             TreeInstance tree = treeInstance;
    51.             tree.prototypeIndex = list[tree.prototypeIndex];
    52.             trees.Add(tree);
    53.         }
    54.        
    55.        
    56.         terrainData.treeInstances = trees.ToArray();
    57.         terrainData.treePrototypes = protos.ToArray();
    58.  
    59.  
    60.         terrainData.RefreshPrototypes();
    61.         terrain.Flush();
    62.     }
    63. }
    64.  
    Maybe there is better solution... I do not know. Anyway maybe this piece of code will be helpful to anybody.

    P.S. Backup your level before try this script :!:
     
    Last edited: Jun 12, 2013
  2. closingiris

    closingiris

    Joined:
    May 18, 2019
    Posts:
    7
    Thanks for this!