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
2
Question by AMAT_Sooraj · Oct 15, 2019 at 10:12 AM · prefabutility

Revert Prefab property override through script

Hi Guys,

I have a situation where materials leaked into the scene due to something I did. But all those materials are in a prefab. Now I want to write an editor script that will find all the materials instances in MeshRenderer.sharedMaterials array in each mesh in the prefab and revert only that particular array element. I tried PrefabUtility.RevertPropertyOverride since I cannot revert the whole object or component or even the entire sharedMaterials array. Only the elements with material instances in it. The problem with PrefabUtility.RevertPropertyOverride is that it takes a SerializedProperty and I'm unable to get the SerializedProperty of the sharedMaterials array or individual elements in it because its a property and not a variable.

Any help would be really appreciated!

Thanks

Comment
Add comment · Show 8
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 misher · Oct 15, 2019 at 10:30 AM 0
Share

If you have changed the material asset there is nothing you could do. It is saved in its asset and it is not a part of some prefab's property. As a prefab property, you have only a reference to the material asset, thus you can revert to point again to some asset or to be null for example, but not to change something that is a property of material itself. In other words, material own properties are serialized in its own asset and not into prefab asset.

avatar image AMAT_Sooraj misher · Oct 15, 2019 at 11:14 AM 0
Share

Yes I know. But my issue is different. $$anonymous$$y materials got leaked into the scene since I used $$anonymous$$eshRenderer.materials ins$$anonymous$$d of $$anonymous$$eshRenderer.shared$$anonymous$$aterials in Editor mode. Its only meant to be used in run time. Now I can sit and revert individual array elements by right-clicking any revert property, but I have over 10k meshes in a scene and 9 scenes. So I just want to write an editor script to do the same.

avatar image misher AMAT_Sooraj · Oct 15, 2019 at 11:54 AM 0
Share

Hmm, you could search for each mesh if there is an original material asset and set sharedmat = ref asset ins$$anonymous$$d of (instance). Although I can not imagine what criteria you can use in search.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Marc-Saubion · Feb 13, 2021 at 03:13 PM

Similar issue.

I'd like some renderers to blink red to add some visual cues but don't want to override the prefab material with the original material once it done.

Did you find anything?

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
avatar image
0

Answer by Meceka · Mar 25 at 04:34 PM

I had a similar case and I found a solution myself but it could have some unnecessary lines in the code for your use.

In a foreach loop of all instances of my type called WpNode, I want to revert the property "radius" if its value is 10.

 foreach (WpNode node in FindObjectsOfType<WpNode>()) {
     GameObject go = node.gameObject;
     if (PrefabUtility.GetPrefabInstanceStatus(go)!=PrefabInstanceStatus.Connected) continue;
     if (!PrefabUtility.IsOutermostPrefabInstanceRoot(go)) continue;
     if (!PrefabUtility.HasPrefabInstanceAnyOverrides(go, false)) continue;
     foreach (ObjectOverride objectOverride in PrefabUtility.GetObjectOverrides(go)) {
         Object obj = objectOverride.instanceObject;
         if (obj.GetType() == typeof(WpNode)) {
             WpNode wpNode = (WpNode)obj;
             if (wpNode.radius == 10) {
                 RevertPrefabPropertyOverrideWithMatchingName(obj,"radius");
             }
         }
     }
 }

And the function to revert a particular property;

 public static void RevertPrefabPropertyOverrideWithMatchingName(Object obj, string name) {
     SerializedObject serializedObject = new SerializedObject(obj);
     SerializedProperty serializedProperty = serializedObject.FindProperty(name);
     PrefabUtility.RevertPropertyOverride(serializedProperty,InteractionMode.AutomatedAction);
     Debug.Log("Reverted Object: "+obj.name,obj);
 }
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

113 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 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 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 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 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 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 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 avatar image

Related Questions

Is there a way to prevent non-prefab objects from being linked in the inspector? 0 Answers

How exactly are prefabs tagged with PrefabAssetType enums? 0 Answers

Can anyone help with creating Prefabs for procedural meshes 1 Answer

Cloning a GameObject with Linked Prefab Children 1 Answer

cannot load GameObject Prefabs using Resources.Load in Android 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