- Home /
Assigning meshes & materials from Assets, how?
I know this must be plainly simple, but still it eludes me:
I want to create a mesh and/or material (in an Editor script), and I want to assign a mesh from my assets. Not necessarily in Resources folder mind you, just some random asset.
something like:
gameObject.GetComponent(MeshFilter).sharedMesh = ?LoadAsset? ("big_gun_mesh");
Is this possible?
Answer by Mike 3 · Jul 28, 2010 at 08:38 PM
You'll want to use AssetDatabase.LoadAssetAtPath:
http://unity3d.com/support/documentation/ScriptReference/AssetDatabase.LoadAssetAtPath.html
It's similar to Resource.Load, but takes paths relative to the project folder, not assets as you may first guess
Sounds promising, but is there any way to not use a path? Eg. when you go to the $$anonymous$$eshRenderer, you can drop-down a (non-pathed) list of meshes to choose. That would be nice. Otherwise, I have to crawl my project folder looking for the mesh, which in fact may be a sub-mesh.
In that case, I'd just make a wizard with a public mesh property, drag the right mesh in, and do the processing after create
I'm really trying to avoid that 'drag' step all together. I'm making a script to repair 'missing mesh' and 'missing material' problems. I have a ton of them, too much dragging each time.
In that case, the only thing I can think of is recurse through the assets using functions from System.IO.Directory, then render them in a selectable list
This can't be really interactive. Actually, I have made some headway. I'm assu$$anonymous$$g a particular path and all files .dae. So when I run into a game object with a $$anonymous$$eshRenderer but no mesh, I load the dae with the same name as the parent of this object, then look in the loaded objects for one that matches the current gameobject's name, with type of $$anonymous$$esh. I then assign this to the shared$$anonymous$$esh property on the mesh filter. This actually seems to work but I'm not thrilled, because the results seem to have some serious clipping plane problems (original, undamaged file had no problems). Also (see next..
Your answer
Follow this Question
Related Questions
Assign material and guiskin to variables 1 Answer
Beginner: assigning texture to part of GameObject 1 Answer
Is it possible to create a Material _asset_ from a script? 3 Answers
Save procedural mesh in the editor as asset. 2 Answers
Combining SkinnedMesh's with the same material and animations 0 Answers