Skip to content

Instantly share code, notes, and snippets.

@becksebenius
Last active December 25, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save becksebenius/6958389 to your computer and use it in GitHub Desktop.
Save becksebenius/6958389 to your computer and use it in GitHub Desktop.
Exports a mesh instance from the currently selected Mesh Filter. Should go in the Editor folder. http://blog.becksebenius.com/ for more.
using UnityEngine;
using UnityEditor;
public class MeshExporter
{
const string menuItemPath = "Assets/Export Mesh Instance";
[MenuItem(menuItemPath)]
public static void SaveMeshAsAsset ()
{
var go = Selection.activeGameObject;
if(!go)
{
EditorUtility.DisplayDialog("Invalid Selection", "You must select a game object with a mesh filter.", "Confirm");
return;
}
var mf = go.GetComponent<MeshFilter>();
if(!mf)
{
EditorUtility.DisplayDialog("Invalid Selection", "You must select a game object with a mesh filter.", "Confirm");
return;
}
var mesh = mf.sharedMesh;
if(!mesh)
{
EditorUtility.DisplayDialog("Invalid Selection", "Selected mesh filter does not have a mesh applied.", "Confirm");
return;
}
string path = EditorUtility.SaveFilePanelInProject("Cache Mesh", "mesh", "asset", "Pick a location to cache your mesh.");
if(path == null) return;
if(path == "") return;
var obj = AssetDatabase.LoadAssetAtPath(path, typeof(UnityEngine.Object));
if(obj)
{
if(!EditorUtility.DisplayDialog("Overwrite asset?", "An asset already exists at this path. Overwrite?", "Confirm", "Cancel"))
{
return;
}
if(!(obj is Mesh))
{
if(!EditorUtility.DisplayDialog("Destroy asset?", "The path selected contains a non-mesh asset. Destroy the original asset file?", "Confirm", "Cancel"))
{
return;
}
}
}
if(EditorUtility.IsPersistent(mesh))
{
mesh = GameObject.Instantiate(mesh) as Mesh;
mf.sharedMesh = mesh;
}
AssetDatabase.CreateAsset(mesh, path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment