- Home /
How to undo AssetDatabase.CreateAsset?
In an editor script I am creating a hidden asset that is stored as part of another asset:
public static T AddAssetOfType<T>(string path, bool hide) where T:ScriptableObject {
T item = ScriptableObject.CreateInstance<T> ();
if (hide) {
item.hideFlags = HideFlags.HideInHierarchy;
}
AssetDatabase.AddObjectToAsset (item, path);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
return item;
}
At one point I'd like to undo this. So I did something like:
var obj = AddObjectToAsset(...);
Undo.RegisterCreatedObjectUndo(obj, "Create Entry" );
However I'm getting this error message:
Undo Created Object may not be performed on persistent or non-default hide flag objects
That's basically killer since my object is persisted AND hidden. Is there a way to undo this using the built in undo system? If not is there a way to put in some fake undo state and run some own code to undo the change?
Don't know about your issue specifically, although as it's three years old I don't htink it's relevant anymore. But for anyone else, your last sentence is basically what I did with UndoPro. UndoPro uses the command-pattern to execute custom actions when a dummy entry is undone/redone. You could take a look at that, maybe it will help you;) https://github.com/Seneral/UndoPro
Your answer
Follow this Question
Related Questions
How do I register undo for the moving/renaming of a folder along with the assets contained? 0 Answers
How to allow Undo on project assets created with an editor script? 0 Answers
How to support undo for objects in .asset files 1 Answer
Can I safely put a prefab asset into version control with Unity Free? 3 Answers