Jan
26
2013
26
2013
Save Mesh Created by Script in Editor PlayMode
An article by mgear
14 Comments
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 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:
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
- [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
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- 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
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
@unitycoder_com
My TweetsSubscribe to Blog via Email
Tag Cloud
2d
3D
AI
algorithm
android
asset
build
color
custom
demo
editor
effect
error
fake
free
game
generator
greasemonkey
indie
javascript
light
line
ludumdare
mesh
paint
particles
physics
plugin
proto
prototype
script
sea
shader
shadow
sprite
terrain
texture
tutorial
ui
unity
vertex
visibility
water
waves
webgl
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:
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