Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Arkade · Nov 02, 2013 at 09:23 AM · prefabeditor-scriptingundo

How to provide undo for EditorScript changes to a prefab?

I'm writing an Editor Script (or ScriptableWizard) to modify a prefab -- specifically add child GameObjects to the prefab. I wish to make this operation undoable but cannot determine how.

I've tried Undo.RegisterUndo(), Undo.RegisterCreatedObjectUndo() and Undo.SetSnapshotTarget() + Undo.CreateSnapshot() + Undo.RegisterSnapshot().

Here's the code:

     [MenuItem("Tools/Add child to prefab", true)]
     static bool validateAddChildToPrefab() {
         GameObject go = Selection.activeObject as GameObject;
         if (null == go) return false;
         
         return PrefabUtility.GetPrefabType(go) == PrefabType.Prefab || PrefabUtility.GetPrefabType(go) == PrefabType.ModelPrefab;
     }
 
     [MenuItem("Tools/Add child to prefab")]
     static void addChildToPrefab() {
         GameObject go = (GameObject) Selection.activeObject;
         
         // register undo -- my question: how to get this to work?
 //        Undo.RegisterUndo(go, "Add child to prefab"); // does nothing
 //        makeUndoFor(go, "Add child to prefab"); // (see below) does nothing
 //        Undo.RegisterCreatedObjectUndo(go, "Add child to prefab"); // error: Undo Created Object may not be performed on persistent or non-default hide flag objects
 //        Undo.RegisterSceneUndo("Add child to prefab"); // does nothing (not in scene so wasn't hopeful)
 
         GameObject clone = (GameObject) PrefabUtility.InstantiatePrefab(go);
         GameObject child = new GameObject("My child");
         child.transform.parent = clone.transform;
         PrefabUtility.ReplacePrefab(clone, go, ReplacePrefabOptions.ReplaceNameBased);
         
         // Remove the scene instance of the prefab
         DestroyImmediate(clone);
     }
 
     private static void makeUndoFor(Object root, string actionName) {
         Object[] objectsToRecord = EditorUtility.CollectDeepHierarchy(new Object[]{ root });
         Debug.Log("Creating UNDO point for "+ objectsToRecord.Length +" objects");
         Undo.SetSnapshotTarget(objectsToRecord, actionName);
         Undo.CreateSnapshot();
         Undo.RegisterSnapshot();
     }

p.s. If there's a better way to add child GameObjects to prefabs (i.e. without instantiating), I'd really like to know! Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dizzy2003 · Apr 29, 2014 at 06:10 AM

you need to put Undo.RegisterCreateObjectUndo() after the InstantiatePrefab and pass it the new object

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modify prefabs in editor script 1 Answer

Is it possible to ensure that certain game objects don't get saved in the scene or otherwise hook into the default save scene to run custom editor code? 2 Answers

Add Asset Bundle Name to a prefab in "Editor Code" 1 Answer

How do I change the name of the prefab in the prefab folder with code? 0 Answers

Undo.RegisterFullObjectHierarchyUndo not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges