- Home /
Save procedural mesh in the editor as asset.
Hi!
I'm curious, how to save a procedural (script generated) mesh as an asset. I was trying to do it, I failed.
What I tried is: get the Mesh Filter component's mesh, store it in a variable and use AssetDatabase.CreateAsset.
I wanted to do it with a custom GUI. There is a button and a textfield. I can name the asset with it. It puts the mesh to Assets/CustomModels/. It makes the asset based on the first selection (Selection.transforms[0]).
When I press the Save Mesh button (I called as that), it makes the asset first time, but after that, it doesn't work.
I guess it can't overwrite the asset, but it even don't work if I give it a new name.
Here is the error I get always:
Couldn't create asset file because the Mesh 'NewPlain' is already an asset at 'Assets/CustomModels/NewPlain.asset'! UnityEditor.AssetDatabase:CreateAsset(Object, String) SaveMeshAsset:OnGUI() (at Assets/Editor/SaveMeshAsset.js:28) UnityEditor.DockArea:OnGUI()
UnityException: Creating asset at path Assets/CustomModels/NewPlain2.asset failed. SaveMeshAsset.OnGUI () (at Assets/Editor/SaveMeshAsset.js:28) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture)
I'm a very beginner script writer, and I don't get the reason of the error. The script first changes the nameOfMesh variable that is modifiable from the GUI.TextField, and after it had to save.
There are several places you could get things wrong. $$anonymous$$aybe this video will help: Unity Tips and Tricks
Answer by MediaGiant · Jul 23, 2014 at 06:15 AM
The docs say "If an asset already exists at path it will be deleted prior to creating a new asset." which seems to be true if the asset isn't currently selected, otherwise you get the error above. I also tried using the AssetDatabase.DeleteAsset before saving but this broke the reference to the mesh in the original object.
What appears to work is to create an new instance of the mesh you want to save:
Mesh tempMesh = (Mesh)UnityEngine.Object.Instantiate(originalMesh);
That's a genius answer and is indeed a solution to the issue... in js
var copy : $$anonymous$$esh = Instantiate(original);
didnt even know i could instantiate without position and rotation variables, very convenient.
in memory critical programs i think the copies would have to be cleared afterwards.
$$anonymous$$y goodness Thanks man i hope you get a kiss from Amber heard. Thanks for the great answer.
Answer by DaveA · Mar 19, 2012 at 09:09 PM
Try using AssetDatabase.RenameAsset or DeleteAsset before (re)saving, to get rid of any original and references to it.
"Try using AssetDatabase.RenameAsset or DeleteAsset before (re)saving, to get rid of any original and references to it."
Well, I tried to do that. Even I'm not sure how it works. One thing confuses me.
What I did is: Put the DeleteAsset before the SaveAsset. It was logical for me too. But I got the error even if I did delete and save with an other name. Basicly in the error there was always the first successfully saved asset's name.
It could save only once per game object.