Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Updating Trees via Editor Scripting

Discussion in 'World Building' started by D5R, Mar 21, 2019.

  1. D5R

    D5R

    Joined:
    Aug 15, 2015
    Posts:
    2
    So I've been working on a custom Editor script which edits a tree's values in edit mode but I'm having trouble refreshing the tree to show changes.

    I know that Trees created with the Unity Tree Editor are stored as ScriptableObject assets and I've been able to manipulate the values using a custom Editor script, but when I edit the values on the asset (via scripting or changing the asset on the inspector), the tree doesn't show the changes until I move the sliders on the Tree component itself. Anyone has any ideas on how to automatically update or "refresh" the look of a tree via scripting?
     
  2. D5R

    D5R

    Joined:
    Aug 15, 2015
    Posts:
    2
    Alright so I've been messing with the Unity TreeData and discovered that there are a bunch of (undocumented?) methods available to be called. Calling TreeEditor.TreeData.UpdateMesh() seems to update the tree mesh via scripting but I haven't fully tested it out yet. Right now, I'm simply editing the root tree seed via an editor script and using the method above to update the tree's mesh in edit mode. If anyone cares to try it out, I'm posting the scripts which currently update the tree. My plan is to make a tool which generates trees semi-randomly and this seems to be a first step towards it.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. //Pressing the RANDOMIZE button randomizes the tree's Seed value.
    4. public class TreeMake1 : MonoBehaviour
    5. {
    6.     public GameObject tree;
    7.     private Tree t;
    8.     public Material[] m;
    9.  
    10.     public void UpdateTree() {
    11.         t = tree.GetComponent<Tree>();
    12.         var tData = t.data as TreeEditor.TreeData;
    13.         var root = tData.root;
    14.         root.seed = Random.Range(0, 999999);
    15.         //I honestly don't know what the parameters are for but it works.
    16.         tData.UpdateMesh(tree.transform.worldToLocalMatrix, out m);
    17.         Debug.Log("Current Seed: " + root.seed);
    18.     }
    19. }

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. //Adds a button to the TreeMake1 component.
    5. [CustomEditor(typeof(TreeMake1))]
    6. public class TreeGenEditor : Editor
    7. {
    8.  
    9.     override public void OnInspectorGUI() {
    10.         DrawDefaultInspector();
    11.         if (GUILayout.Button("RANDOMIZE", GUILayout.Height(30)))
    12.         {
    13.             TreeMake1 tm1 = (TreeMake1)target;
    14.             tm1.UpdateTree();
    15.         }
    16.     }
    17. }
     
    Last edited: Mar 22, 2019
  3. kwandrus

    kwandrus

    Joined:
    Mar 18, 2020
    Posts:
    1
    How were you able to find these "undocumented" methods? I'm looking to find a way to access the end points of each branch of my tree, but I can't find any documentation on how to access this information within a script. Any help would be very much appreciated!
     
    Last edited: Mar 19, 2020
  4. fnhalluk

    fnhalluk

    Joined:
    Feb 27, 2016
    Posts:
    3
    Well... I think I've hit the jackpot!

    Start by adding "Using TreeEditor" as a declaration at the top. Then go to declare any variable starting with "Tree..." and you should find a whole host of useful variables to use. Note, I'm completely new to scripting trees so just putting this out there to get you guys on the same page.
     

    Attached Files:

    YanMusatov likes this.
  5. fnhalluk

    fnhalluk

    Joined:
    Feb 27, 2016
    Posts:
    3
    Small progress report from this morning's efforts...

    There seems to be some disconnect between Tree.data and TreeData. The first seems to do very little and the second has everything that we'd ever want to edit but how they're linked I don't know. So for completeness, I'm referencing both while I try to figure this stuff out.

    Code (CSharp):
    1. using UnityEngine;
    2. using TreeEditor;
    3.  
    4. public class scriptTreeBase : MonoBehaviour
    5. {
    6.     public Tree tree;
    7.     public TreeData treeData;
    8.  
    9. void Update()
    10.     {
    11.         treeData.branchGroups[0].radius += 0.1f * Time.deltaTime;
    12.        
    13.     }
    14. }
    See attached file for linking up the references (drag the Tree asset into the public tree variable and drag the "Tree Data" asset into the treeData variable).
    The code correctly increases the radius of the truck of the tree but this doesn't seem to display. If you click the instance of the tree in the scene and then click "Recompute Tree", the new size then displays but it also comes with a host of errors saying this shouldn't be executed in play mode. It feels like there should simply be a call to a mesh update or something.

    I really hope that someone can take this starting point and push forward with some developments. Typing "treeData." will bring up a huge list of functions and should give us all the controls we need.
     

    Attached Files:

    UnityLighting and YanMusatov like this.
  6. YanMusatov

    YanMusatov

    Joined:
    Mar 9, 2020
    Posts:
    1
    Oh my God. Yesterday, I started looking for a way to "grow" the tree using scripts, but there is nowhere documentation for it and I just stumbled upon update methods typing TreeData just like you.
    I have the same problem with playMode
    sorry for my English
     
  7. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    Hello, do you have some news or advance on this?
    I'm getting into it, I have some script for "growing" trees and accessing the tree editor via script will be a huge leap for my project...
     
  8. iep78esy

    iep78esy

    Joined:
    Feb 28, 2020
    Posts:
    3
    Can you please share your code here? I'm also trying to grow my trees via code and this is the only place that this topic is explained in.
     
  9. capocchione

    capocchione

    Joined:
    Dec 9, 2019
    Posts:
    47
    It's just code to solve ODEs because the growth is modeled by biological mathematical equations (in this case, Ordinary Differential Equations)
     
  10. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,913
    I want to create high resolution texture atlas for tree creator
    Anyone now how can i do it?
    Default is 1024x1024 and the leaves are blurry