Skip to content

Instantly share code, notes, and snippets.

@sugi-cho
Created April 4, 2015 10:35
Show Gist options
  • Save sugi-cho/3acacf4f9564a6126da1 to your computer and use it in GitHub Desktop.
Save sugi-cho/3acacf4f9564a6126da1 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using System.Collections;
public class BakeTransformToMesh
{
[MenuItem("sugi.cho/Mesh/Bake Transform")]
public static void BakeTransform ()
{
var targetGo = Selection.activeGameObject;
if (targetGo == null || targetGo.GetComponent<MeshFilter> () == null)
return;
var targetMesh = targetGo.GetComponent<MeshFilter> ().sharedMesh;
if (targetMesh == null)
return;
var mesh = new Mesh ();
var ci = new CombineInstance ();
ci.mesh = targetMesh;
ci.transform = targetGo.transform.localToWorldMatrix;
mesh.CombineMeshes (new CombineInstance[]{ci});
mesh.RecalculateNormals ();
mesh.RecalculateBounds ();
AssetDatabase.CreateAsset (mesh, "Assets/" + targetGo.name + "_BakeTransform" + ".asset");
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment