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
0
Question by Ben Blaut · Nov 02, 2012 at 07:35 PM · javascripteditorprefab

How can I mimic the "Apply" button in editor script?

I have the following code to mimic the "Apply" button in an editor script, and it works great to automatically force any changes I apply to my prefab in the Scene view to the prefab it draws from in the Project view. However, this ONLY works if I make changes to the parent object of the prefab. If I make changes to a child in the prefab, when it subsequently applies the changes, the parent object becomes the child object that I modified, which is not the functionality I want. Obviously, the `GetPrefabParent` is working as intended, but I'm looking for something different. Ideally, when I edit the child, it would apply the changes only to the same child in the Project view, not replace the parent. However, I don't see how to do this.

 if (GUI.changed) {
   EditorUtility.SetDirty(EI);
   if (PrefabUtility.GetPrefabType(Selection.objects[0]) == PrefabType.PrefabInstance) {
     PrefabUtility.ReplacePrefab(Selection.objects[0], PrefabUtility.GetPrefabParent(Selection.objects[0]), ReplacePrefabOptions.ConnectToPrefab);
   }
 }
Comment
Add comment · Show 4
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 DaveA · Nov 02, 2012 at 08:32 PM 0
Share

Are you trying to 'nest' prefabs?

avatar image Ben Blaut · Nov 05, 2012 at 09:38 PM 0
Share

I guess in a way, yeah. They're already nested, and I just want to apply the changes that are made to them in the scene view as they get changed.

avatar image ThomasKang · Aug 30, 2013 at 07:07 AM 0
Share

Can you show me how to do this in C#?

avatar image Ben Blaut · Sep 10, 2013 at 12:22 PM 0
Share

@Croirefly I think the only thing that changes is how you typecast the variables.

1 Reply

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

Answer by Ben Blaut · Feb 27, 2013 at 06:33 PM

To do this, I had to specify which part I wanted to update the prefab origin with from my selection. Before, I was simply replacing the prefab origin with my selection (which was the child). I had to change it so it replaced the prefab origin (which was a parent) with the parent of the child I had selected in the "Hierarchy" View.

 if (GUI.changed) {
   var selectedGameObject : GameObject;
   var selectedPrefabType : PrefabType;
   var parentGameObject : GameObject;
   var prefabParent : UnityEngine.Object;
   
   // Get currently selected object in "Hierarchy" view and store
   // its type, parent, and the parent's prefab origin
   selectedGameObject = Selection.gameObjects[0];
   selectedPrefabType = PrefabUtility.GetPrefabType(selectedGameObject);
   parentGameObject = selectedGameObject.transform.root.gameObject;
   prefabParent = PrefabUtility.GetPrefabParent(selectedGameObject);
   
   // Notify the script this is modifying that something changed
   EditorUtility.SetDirty(target);
   
   // If the selected object is an instance of a prefab
   if (selectedPrefabType == PrefabType.PrefabInstance) {
     // Replace parent's prefab origin with new parent as a prefab
     PrefabUtility.ReplacePrefab(parentGameObject, 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

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

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How to find a prefab in another scene? 1 Answer

editor script makes editor crash 1 Answer

Problems with creating custom EditorGUI for script 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