How to dynamically instanciante SpeedTree assets from script?
Hello,
I am trying to figure out how I can create and put in my scene SpeedTree assets using a C# script. So I have downloaded the Free_SpeedTrees package and drag drop one of them in my scene to see what results in the Hierarchy panel. It appears that a SpeedTree asset is based on : -object with LOD component |_billboard |_LOD n |_LOD n+1 |_Collision object n |_Collison object n+1
Each LOD has a MeshFilter that loads the .spm file it needs.
I looked into the documentation (both Manual and Scripting API). I found no class for SpeedTree, except the SpeedTreeImporter but it seems that this class only customize import settings.
Then I looked into LODComponent and LOD, but the only example uses Primitives.
I also went through the MeshFilter class and its Mesh attribute, which appears in the Editor as a filepath but is instead a Mesh object in the API. Unfortunatly, the Mesh object has no method for loading a 3D file and building itself from the file.
I really have no clue on where to start, which class is needed. Don't even know if it is possible to dynamically create SpeedTree from script, as both the Forums and the Answers area are weak on this subject.
So any help would be greately appreciated! :)
Answer by Ali-hatem · Mar 23, 2016 at 12:20 PM
you can instantiate any thing from script just make the object a Prefabs by drag & drop from hierarchy to Asset folder & delete it from hierarchy.
then create an empty game object in hierarchy & attach a script to it .
open the script :
public class Example : MonoBehaviour{ public GameObject obj; void Start(){ Instantiate (obj,transform.position,transform.rotation); } }
now back to unity click the object this script attached to in the script component you will see an empte field called obj . drag the Prefab from Asset folder to this field & hit play you will see a game object created at the same position of the game object position you putted in the scene for more info see the doc unity doc
Answer by Darholm · Mar 31, 2016 at 08:18 AM
Hello,
Thanks for your answer but it doesn't help. I know about the solution you gave as I am already doing this kind of stuff in others scripts. But here I have to process differently : the idea is to dynamically create speedtrees in my scene without asking the user to do all these actions. Trees will be listed in an XML file with their transform datas and asset file. My problem is to create the speedtrees, XML handling is trivial stuff.
Your answer
Follow this Question
Related Questions
Tree chopping 0 Answers
Problem with Score/Highscore script 0 Answers
button.onClick.AddListener in for loop 2 Answers
How to rotate RigidBodyFPSController (C#) 1 Answer
.rotation doesn't seem to work, can't shoot projectile at mouse coordinates 0 Answers