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
1
Question by firasdeep · Oct 02, 2011 at 01:10 PM · prefabapply

How do I "Apply" a prefab if I have access to one of its instances via code?

If I modify something in an instance of my prefab, how can I "Apply" the changes to the prefab? (Just like clicking "Apply" from the editor)

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

3 Replies

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

Answer by mindlace · Mar 06, 2012 at 11:30 PM

If you want to recreate the behaviour of the Apply button in an editor script, this works (assuming instance is your disconnected scene instance)

PrefabUtility.ReplacePrefab(instance, PrefabUtility.GetPrefabParent(instance), ReplacePrefabOptions.ConnectToPrefab);

Comment
Add comment · Show 5 · 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 michael-bartnett · Jan 26, 2013 at 10:22 AM 0
Share

Thank you very much for providing the actual answer.

avatar image Turbine · Jun 03, 2013 at 04:32 PM 0
Share

Here is a script I made that does it to all children of a game object.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 public class UnitEditorBulkApply : $$anonymous$$onoBehaviour, ICheckForUnsafeChanges
 {
     public bool resetPosition = true;
     public bool resetScale = true;
     public bool resetRotation = true;
 
 
 #if UNITY_EDITOR
     [Context$$anonymous$$enu("Apply Bulk Prefab Apply")]
     public void CheckForUnsafeChanges()
     {
         for ( int i = 0; i < transform.childCount; i++)
         {
             GameObject go = transform.GetChild( i ).gameObject;
 
             if ( PrefabUtility.GetPrefabParent( go ) != null )
             {
                 Vector3 pos = go.transform.localPosition;
                 Vector3 scale = go.transform.localScale;
                 Quaternion rot = go.transform.localRotation;
 
                 if ( resetPosition ) go.transform.localPosition = Vector3.zero;
                 if ( resetScale ) go.transform.localScale = Vector3.one;
                 if ( resetRotation ) go.transform.localRotation = Quaternion.identity;
 
                 {
                     $$anonymous$$onoBehaviour [] behaviours = go.GetComponents<$$anonymous$$onoBehaviour>();
                     foreach ( $$anonymous$$onoBehaviour b in behaviours )
                         if ( b is ICheckForUnsafeChanges )
                             ( b as ICheckForUnsafeChanges ).CheckForUnsafeChanges();
                 }
 
                 Debug.Log( "Applying changes to " + go.name, go );
                 PrefabUtility.ReplacePrefab( go, PrefabUtility.GetPrefabParent( go ), ReplacePrefabOptions.ConnectToPrefab );
 
                 if ( resetPosition ) go.transform.localPosition = pos;
                 if ( resetScale ) go.transform.localScale = scale;
                 if ( resetRotation ) go.transform.localRotation = rot;
             }
         }
     }
 #endif
 }
 
 public interface ICheckForUnsafeChanges {
 #if UNITY_EDITOR
     void CheckForUnsafeChanges();
 #endif
 }
avatar image yoyo · Feb 28, 2014 at 08:16 PM 0
Share

Anyone else finding that PrefabUtility.ReplacePrefab breaks all connections to that prefab from other assets?

avatar image Turbine · Feb 28, 2014 at 10:45 PM 0
Share

It definitely breaks them. It started happening with the new Undo system. I've had to abandon the above script because I haven't had the sanity to find a work around or the proper way of doing things.

You have to be really careful with the new Undo system to log everything or else your prefabs will get mangled. You'll find objects floating in the middle of nowhere from time to time.

avatar image ThePilgrim · Feb 09, 2017 at 07:01 PM -1
Share

I also found that this solution broke all references that any other prefab had to the prefab that I was updating using this method. I was able to solve the problem by first instantiating the prefab, then using PrefabUtility.ConnectGameObjectToPrefab() to connect the instantiated game object to the original prefab. Then, I called PrefabUtility.ReplacePrefab() with ReplacePrefabOptionsDefault argument. See example code below.

 var go = Instantiate(prefab);
 go = PrefabUtility.ConnectGameObjectToPrefab(go, prefab);
 
 // Change the "go" game object as needed.
 
 var updatedPrefab = PrefabUtility.ReplacePrefab(
             go,
             prefab,
             ReplacePrefabOptions.Default
             );
 
 
avatar image
10

Answer by stevesan · May 21, 2014 at 07:04 PM

One caveat to the above answer: If 'instance' is actually a child object inside the prefab, that one line of code will do the wrong thing. You want this instead:

                     var instanceRoot = PrefabUtility.FindRootGameObjectWithSameParentPrefab(target.gameObject);
                     var targetPrefab = UnityEditor.PrefabUtility.GetPrefabParent(instanceRoot);
 
                     PrefabUtility.ReplacePrefab(
                             instanceRoot,
                             targetPrefab,
                             ReplacePrefabOptions.ConnectToPrefab
                             );
 
Comment
Add comment · Show 3 · 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 infinitypbr · Aug 28, 2015 at 10:19 PM 0
Share

This. This works.

avatar image Odyssee · Apr 28, 2016 at 10:46 AM 0
Share

On a prefab; I had values that was set in the inspector by script but disapeared at play. Your lines resolved my problem. Thanks.

avatar image a_p_u_r_o · Jul 28, 2016 at 02:33 PM 0
Share

I just happened to be passing by, but for somebody referencing this in the future I shall leave one note here. "to recreate the behaviour of the Apply button" you need ReplacePrefabOptions.ReplaceNameBased.

avatar image
2

Answer by Veehmot · Jan 31, 2018 at 08:01 AM

From Unity source:

 UnityEngine.Object prefabParent = PrefabUtility.GetPrefabParent(target);
 GameObject gameObject = PrefabUtility.FindValidUploadPrefabInstanceRoot(target);
 PrefabUtility.ReplacePrefab(gameObject, prefabParent, ReplacePrefabOptions.ConnectToPrefab);
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

15 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

Related Questions

before and after apply prefab changes callback 0 Answers

Shooting problem - bullet shoots diagonally when facing forward 2 Answers

Editor utility for handling nested prefabs.. 4 Answers

Help: script that worked don't work with Unity 3.5 0 Answers

Gun Script 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