Last active
December 25, 2015 09:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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