Skip to content

Instantly share code, notes, and snippets.

@GuyGinat
Created November 18, 2024 18:01
Show Gist options
  • Save GuyGinat/267bc6d626c9dc4fb36ef146922f06c0 to your computer and use it in GitHub Desktop.
Save GuyGinat/267bc6d626c9dc4fb36ef146922f06c0 to your computer and use it in GitHub Desktop.
Saves a Unity runtime generated mesh as an asset
using UnityEditor;
using UnityEngine;
namespace Editor
{
public class MeshSaver : MonoBehaviour
{
[MenuItem("Tools/Mesh/Save Mesh")]
public static void SaveMesh()
{
GameObject go = Selection.activeObject as GameObject;
// Create the directory if it doesn't exist
if (!System.IO.Directory.Exists("Assets/Models"))
{
System.IO.Directory.CreateDirectory("Assets/Models");
}
MeshFilter mf = go.GetComponent<MeshFilter>();
Mesh meshToSave = mf.sharedMesh;
// if (optimizeMesh)
// {
// // This optimizes the mesh for GPU access
// MeshUtility.Optimize(meshToSave);
// }
AssetDatabase.CreateAsset(meshToSave, "Assets/Models/RingMeshes/" + go.name + ".asset");
AssetDatabase.SaveAssets();
}
[MenuItem("Tools/Mesh/Save Multiple Meshes")]
public static void SaveMeshes()
{
GameObject[] gos = Selection.gameObjects;
foreach (GameObject go in gos)
{
MeshFilter mf = go.GetComponent<MeshFilter>();
if (mf == null) continue;
Mesh meshToSave = mf.sharedMesh;
AssetDatabase.CreateAsset(meshToSave, "Assets/Models/RingMeshesWithUVs/" + go.name + ".asset");
}
AssetDatabase.SaveAssets();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment