- Home /
Accessing a submesh's components from script.
Hello. I imported an FBX into unity from 3DS Max. In the 3DS Max file I have created an array of game tiles for the board game I am working on. There are 108 total tiles, each with a unique appearance. The file is extracted in a single FBX which contains the submeshes of all 108 tiles.
When I am in Unity, if I extracted them all separately I can access the submesh components. I did it with a line such as this.
GameObject MeshObject = (GameObject) (Resources.Load(string.Format("Models/{0}", theTileValue.ToString())));
where the TileValue is an enum which corresponds to the tile that I wish to load, such as "A1". Problems arise now when I try to do it all from the one file. If I do Resources.Load("Models/ClassicTiles/A1") it does not work. It cannot find what it did before. ClassicTiles is the container for all the smaller tile pieces, and all the tile pieces exist as subpieces(A1, A2, A3, B1, B2, B3) etc. I am trying to pull out the MeshFilter and Mesh for these objects.
How can I do this?
I want to be able to export a single file from 3DS $$anonymous$$ax ins$$anonymous$$d of having to export 108 files... and algorithmically find the meshes I need.
Answer by DaveA · Mar 28, 2012 at 09:03 PM
Submeshes as Unity defines them? http://unity3d.com/support/documentation/ScriptReference/Mesh-subMeshCount.html and http://unity3d.com/support/documentation/ScriptReference/Mesh.GetTriangles.html with more explanation here: http://unity3d.com/support/documentation/ScriptReference/Mesh.SetTriangles.html
If you mean the gameobject hierarchy that the importer produces, you want to crawl the hiearchy. You can get objects by name, or use probably recursion with something like:
foreach (var child : Transform in transform)
var mf : MeshFilter = child.GetComponent(MeshFilter);
if (mf)
var mesh = mf.mesh;
Oh you're a bloody genius! I've been away from Unity so long I totally forgot about traversing children nodes... and forgot it was tucked away in transform. "Hierarchy" was the word I was looking for but failed to find.
Thank you so much Dave! This is the code that works for me now, by the way. No longer do I have to export all 108 tiles, I can ins$$anonymous$$d export the single file from 3DS $$anonymous$$ax.
public void Change$$anonymous$$esh(TileValue theTileValue)
{
GameObject TileSetObject = (GameObject) Resources.Load(TileSetPath);
GameObject Tile$$anonymous$$eshObject = TileSetObject.transform.FindChild(theTileValue.ToString()).gameObject;
$$anonymous$$eshFilter filter = this.GetComponentInChildren<$$anonymous$$eshFilter>();
filter.mesh = Tile$$anonymous$$eshObject.GetComponent<$$anonymous$$eshFilter>().mesh;
this.GetComponentInChildren<Renderer>().shared$$anonymous$$aterials = Tile$$anonymous$$eshObject.renderer.shared$$anonymous$$aterials;
}
Your answer
Follow this Question
Related Questions
Sketchup Importing 5 Answers
Cheetah 3d Animations not Working with Mesh Renderer! 0 Answers
Unity 'auto import' of 3DSMax file - can you 'tag' mesh to be unrecognized? 0 Answers
Can't Reduce Draw Calls / Combine Meshes 2 Answers
Vertex triangles 0 Answers