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?

1 Answer

1

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;

Glad you got it working, thanks for posting code!

–