- Home /
Assigning imported mesh from script
I have imported a blender file, and placed the Import (along with its child mesh, of course) in "Assets/Resources/Models".
Is it possible to assign the mesh directly from script, without first making a prefab containing the mesh?
Answer by fafase · Jul 14, 2013 at 12:53 AM
I guess this should do it.
Mesh mesh = (Mesh)Resources.Load("path",typeof(Mesh));
GetComponent<MeshFilter>().mesh = mesh;
this is not always true. imported mesh assets are .FBX files. the actual mesh is contained within that file and have a separate name. so to load a mesh named mesh0 contained in mesh1.FBX you need to make sure to use $$anonymous$$esh mesh = ($$anonymous$$esh)Resources.Load("Assets/Resources/$$anonymous$$odels/mesh1",typeof($$anonymous$$esh));
This is not always true, what if the model is in a sub folder...
"path" was just an example since it would be a little long to provide all the possible paths the model could be stored in.
How about:
string path = "Assets/Resources/$$anonymous$$odels/mesh1";
$$anonymous$$esh mesh = ($$anonymous$$esh)Resources.Load(path,typeof($$anonymous$$esh));
GetComponent<$$anonymous$$eshFilter>().mesh = mesh;