26
2013
Save Mesh Created by Script in Editor 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:
This file contains hidden or 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
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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? } } |
Related Posts
14 Comments + Add Comment
Leave a comment
Recent posts
- [LudumDare57] Theme: Depths
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
Recent Comments
- Using RenderDoc with Unity (graphics debugger) on
- UI Scroll View automatic Content height on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
oh nice one. also for those that want it, there is an export object script around somewhere.
ASCII FBX file exporter
https://gist.github.com/mstevenson/6159107
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?
I think then need to save as prefab (CreatePrefab() ?) , instead of just mesh asset, havent tested it but maybe this could work: http://wiki.unity3d.com/index.php?title=CreatePrefabFromSelected
Yeah. thx. I’ve already figured this out.
I had to save materials as well, cause unity creates instances of the materials.
export to .OBJ (free)
http://forum.unity3d.com/threads/free-scene-obj-exporter.270240/
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?
try mrz00m’s prefab saver, maybe it saves mats also..
or look for prefab savers:
https://docs.unity3d.com/ScriptReference/PrefabUtility.html
https://github.com/pharan/Unity-MeshSaver/blob/master/MeshSaver/Editor/MeshSaverEditor.cs
https://docs.unity3d.com/ScriptReference/AssetDatabase.CreateAsset.html