Jan
26
2013

Save Mesh Created by Script in Editor PlayMode

save_mesh_created_in_playmode

Saving meshes (which were created by script)  in editor playmode was surprisingly simple,
http://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.CreateAsset.html

 

Updated version:


#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
// Usage: Attach to gameobject, assign target gameobject (from where the mesh is taken), Run, Press savekey
public class SaveMeshInEditor : MonoBehaviour
{
public KeyCode saveKey = KeyCode.F12;
public string saveName = "SavedMesh";
public Transform selectedGameObject;
void Update()
{
if (Input.GetKeyDown(saveKey))
{
SaveAsset();
}
}
void SaveAsset()
{
var mf = selectedGameObject.GetComponent<MeshFilter>();
if (mf)
{
var savePath = "Assets/" + saveName + ".asset";
Debug.Log("Saved Mesh to:" + savePath);
AssetDatabase.CreateAsset(mf.mesh, savePath);
}
}
}
#endif

 

 

old version:


using UnityEngine;
using UnityEditor;
using System.Collections;

// assetSaver v1.0 - attach this script to any object, assign the target transfrom (from where the mesh is saved), give some filename. Play. Then press F to save.

public class saveMesh : MonoBehaviour
{

public string name1;
public Transform obj1;

void Update ()
{
if (Input.GetKeyDown("f"))
{
SaveAsset();
}
}

void SaveAsset()
{
Mesh m1 = obj1.GetComponent<MeshFilter>().mesh;
AssetDatabase.CreateAsset(m1, "Assets/" + name1 + ".asset"); // saves to "assets/"
//AssetDatabase.SaveAssets(); // not needed?
}

}


14 Comments + Add Comment

  • oh nice one. also for those that want it, there is an export object script around somewhere.

  • Do you know if it’s possible to save the entire GameObject inclduing the MeshCollider? how would i go from the above script to saving MeshCollider so that it can reload fast?

    • Is that basically saving the whole object as a newly created prefab?

      • Yes. i figured it out. here is a version inspired by unify wiki creatprefeabfromselected.cs it saves the entire prefab including a new copy of the mesh, by using your above example to save the mesh to disk prior to saving the prefab:

        using UnityEditor;
        using UnityEngine;
         
        /// <summary>
        /// Creates a prefab from a selected game object.
        /// </summary>
        class CreatePrefabFromSelected
        {
        	const string menuName = "GameObject/Create Prefab From Selected";
         
        	/// <summary>
        	/// Adds a menu named "Create Prefab From Selected" to the GameObject menu.
        	/// </summary>
        	[MenuItem(menuName)]
        	static void CreatePrefabMenu ()
        	{
        		var go = Selection.activeGameObject;
         
        		Mesh m1 = go.GetComponent<MeshFilter>().mesh;//update line1
        		AssetDatabase.CreateAsset(m1, "Assets/savedMesh/" + go.name +"_M" + ".asset"); // update line2
         
         
        		var prefab = EditorUtility.CreateEmptyPrefab("Assets/savedMesh/" + go.name + ".prefab");
        		EditorUtility.ReplacePrefab(go, prefab);
        		AssetDatabase.Refresh();
        	}
         
        	/// <summary>
        	/// Validates the menu.
        	/// The item will be disabled if no game object is selected.
        	/// </summary>
        	/// <returns>True if the menu item is valid.</returns>
        	[MenuItem(menuName, true)]
        	static bool ValidateCreatePrefabMenu ()
        	{
        		return Selection.activeGameObject != null;
        	}
        }
        
        • It’s handy because it also saves the meshcollider to disk, dont have to generate meshcollider in game, in the prefab, and the shader settings. load time was 1.2 secs for importing 65k mesh prefab vs 4 seconds for generating. only thing is to make camera see the 65k meshes at level load time so they are in graphics card, and hey presto, 100ds thousands vertices available in game at 200fps during play.

  • Awesome, just what I needed, thanks!

  • Awesome!
    Btw, I noticed one problem. If I changed any shader vars in runtime, this script couldn’t save material.
    Any ideas how to solve it?

  • Pretty cool. How could I load it back in (set another object’s sharedMesh equal to the saved mesh)?

  • Would it be possible to save the mesh material too so it comes with it texture?

Leave a comment to Keenan

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.