- Home /
Question by
buestad · Oct 18, 2012 at 07:25 AM ·
exportmesh colliderconvex.obj
Export convex mesh collider data as .obj
Is there a way to export the mesh data of a mesh collider with the convex option set to it.
I have used the following C# code to export an .obj file containing the mesh, but setting the Convex option does not make any difference.
using UnityEngine;
using System.Collections;
using System.IO;
public class ReadCollider : MonoBehaviour
{
private Vector3[] vertices;
private int[] triangles;
// Use this for initialization
void Start ()
{
Collider c = collider;
if( c is MeshCollider)
{
MeshCollider mc = (MeshCollider)c;
mc.convex = true;
Mesh m = mc.sharedMesh;
vertices = m.vertices;
triangles = m.triangles;
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\collider.obj");
file.WriteLine("# vertices");
for(int i = 0; i< vertices.Length; i++)
file.WriteLine("v " + vertices[i].x + " " + vertices[i].y + " " +vertices[i].z);
file.WriteLine("# triangles");
for(int i = 0; i< triangles.Length;)
file.WriteLine("f " + (triangles[i++]+1) + " " + (triangles[i++]+1) + " " +(triangles[i++]+1) +"");
file.Close();
}
}
// Update is called once per frame
void Update ()
{
}
}
Comment