Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
5
Question by AdamOwen · Jul 16, 2011 at 04:40 AM · editorprefabs

Reverting several GameObjects to Prefab settings at once

Hey all,

I've ran into an issue this evening. I have a Prefab that contains a simple cube mesh and a material, and I've placed hundreds of instances of this through several different scenes.

I was attempting to change the prefab material when I realized that all the instances have overridden the material property due to something I must have changed on the first instance I ever created, thus not allowing me to reflect the changes to the Prefab across all of its instances.

I know that you can revert an instance to its Prefab but even when selecting all the instances (each scene they are all contained inside an empty GameObject) the revert only affects the last one I selected.

Is there no way to execute variable changes across multiple selected GameObjects? If not I assume I need to write an Editor script, something I have not delved into yet but if it comes to that I'm sure the experience will be worth something.

Hope I'm not overlooking something ridiculous here, it's 5:30am so I'm prepared for it. I've searched thoroughly and the only thing I found to go on is this thread on the main forums. It's dated 2009 though so I was hoping things have changed since then.

Thanks in advance!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by AdamOwen · Jul 16, 2011 at 05:27 PM

Well after more unsuccessful searching I read up on the Editor scripting reference and managed to write a script that resets all selected GameObjects to their prefab settings.

 @MenuItem ("Tools/Revert to Prefab %r")
 static function Revert() {
     var selection = Selection.gameObjects;
 
     if (selection.length > 0) {
         for (var i : int = 0; i < selection.length; i++) {
             EditorUtility.ResetGameObjectToPrefabState(selection[i]);
         }
     } else {
         Debug.Log("Cannot revert to prefab - nothing selected");
     }
 }

As I said I'm new to scripting the editor and it may not be the best way to do this but it works for me and that's all I need right now.

Comment
Add comment · Show 4 · 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
avatar image UdevMike · Aug 24, 2011 at 01:26 PM 0
Share

Hate to bump up an old question but is there really no way to do this within stock Unity?

Thanks!

avatar image Hugo-Bille · Oct 18, 2012 at 08:28 AM 1
Share

Slight update: nowadays you'd have to call PrefabUtility.RevertPrefabInstance(GameObject), as EditorUtility.ResetGameObjectToPrefabState(GameObject) doesn't exist anymore.

avatar image v01pe_ · Dec 03, 2016 at 10:13 PM 0
Share

Thanks a ton!

avatar image WBagley · Oct 11, 2017 at 03:09 AM 0
Share

@Udev$$anonymous$$ike Unfortunately, Unity doesn't support it by default. They expose the PrefabUtility class so that you can spin up your own scripts but its a bit fiddly. The function you want can be found in the Unity Docs.

Alternatively, you can use a premade asset such as $$anonymous$$ultifab that does all the hard bits for you.

avatar image
6

Answer by jim_altvr · Feb 23, 2016 at 01:12 PM

Non-programmer here... Had a coworker update your script to work in Unity 5(.2.3)

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class RevertPrefabInstance : MonoBehaviour {
 
     [MenuItem ("Tools/Revert to Prefab %r")]
     static void Revert() {
         var selection = Selection.gameObjects;
         
         if (selection.Length > 0) {
             for (var i = 0; i < selection.Length; i++) {
                 PrefabUtility.RevertPrefabInstance(selection[i]);
             }
         } else {
             Debug.Log("Cannot revert to prefab - nothing selected");
         }
     }
 }
Comment
Add comment · Show 1 · 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
avatar image v01pe_ · Dec 03, 2016 at 10:13 PM 0
Share

Thanks you too for posting an updated version! Saved me a lot of time!

avatar image
2

Answer by x4000 · Apr 28, 2016 at 01:54 PM

Here's a much more robust version:

 using UnityEngine;
 using UnityEditor;
 using System.Collections.Generic;
 
 /// <summary>
 /// http://answers.unity3d.com/questions/144453/reverting-several-gameobjects-to-prefab-settings-a.html
 /// </summary>
 public class RevertPrefabInstance : UnityEditor.Editor
 {
     [MenuItem( "Tools/Revert Selected Prefabs %&#r" )]
     static void Revert()
     {
         GameObject[] selection = Selection.gameObjects;
 
         if ( selection.Length > 0 )
         {
             int revertedCount = 0;
             Dictionary<UnityEngine.Object,bool> prefabsAlreadyReverted = new Dictionary<UnityEngine.Object, bool>();
 
             //we tell the user that we're about to do an expensive operation, because otherwise they may think the editor is hanging
             //additionally if the hotkey doesn't work (aka clash with another hotkey) they won't even know it failed to run!
             EditorUtility.DisplayDialog( "Please wait", "Checking " + selection.Length + " objects and their children to revert prefab status -- this may take a while!", "OK" );
             for ( int i = 0; i < selection.Length; i++ )
                 RecursiveRevertPrefabInstances( selection[i], ref revertedCount, ref prefabsAlreadyReverted );
 
             //tell them we finished, because otherwise they have no easy way to notice if it takes a bit!
             EditorUtility.DisplayDialog( "All done!", "Performed " + revertedCount + " reversions.", "OK" );
         }
         else {
             EditorUtility.DisplayDialog( "Error", "Cannot revert to prefab - nothing selected.", "OK" );
         }
     }
 
     /// <summary>
     /// This allows for both nested prefabs as well as simply going into object trees without having to expand the whole tree first.
     /// </summary>
     static void RecursiveRevertPrefabInstances( GameObject obj, ref int revertedCount, ref Dictionary<UnityEngine.Object, bool> prefabsAlreadyReverted )
     {
         if ( obj == null )
             return;
         if ( IsAPrefabNotYetReverted( obj, ref prefabsAlreadyReverted ) )
         {
             revertedCount++;
             PrefabUtility.RevertPrefabInstance( obj );
         }
         Transform trans = obj.transform;
         for ( int i = 0; i < trans.childCount; i++ )
             RecursiveRevertPrefabInstances( trans.GetChild( i ).gameObject, ref revertedCount, ref prefabsAlreadyReverted );
     }
 
     /// <summary>
     /// This keeps us from reverting the same prefab over and over, which otherwise happens when we're doing checks for nested prefabs.
     /// </summary>
     static bool IsAPrefabNotYetReverted( GameObject obj, ref Dictionary<UnityEngine.Object, bool> prefabsAlreadyReverted )
     {
         bool wasValidAtEitherLevel = false;
         UnityEngine.Object prefab = PrefabUtility.GetPrefabParent( obj );
         if ( prefab != null && !prefabsAlreadyReverted.ContainsKey( prefab ) )
         {
             wasValidAtEitherLevel = true;
             prefabsAlreadyReverted[prefab] = true;
         }
         prefab = PrefabUtility.GetPrefabObject( obj );
         if ( prefab != null && !prefabsAlreadyReverted.ContainsKey( prefab ) )
         {
             wasValidAtEitherLevel = true;
             prefabsAlreadyReverted[prefab] = true;
         }
         return wasValidAtEitherLevel;
     }
 }
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

11 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

Related Questions

Anyone know of a tutorial for making an editor that would easily piece prefab models together? 1 Answer

How to find component (for example: script) references/usages in prefabs? 3 Answers

DestroyImmediate Prefabs 3 Answers

Prefab becoming distorted 0 Answers

Problem with prefabs, help? 0 Answers


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