• 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.

avatar image AMAT_Sooraj misher · Oct 15, 2019 at 01:30 PM 0
Share

The problem with that is I may have materials with the same name under different folders in my assets. Searching and assigning them through brute force search may not yield desired results. Do you happen to know how to revert individual materials in array on prefabs through script? Doing it through the editor works, but its a lengthy process.

avatar image misher AMAT_Sooraj · Oct 15, 2019 at 01:46 PM 0
Share

did you try this API: https://docs.unity3d.com/ScriptReference/PrefabUtility.RevertPropertyOverride.html

avatar image AMAT_Sooraj misher · Oct 15, 2019 at 01:53 PM 0
Share

Yes. Please go through my original question. I'm having a bit of problem using that coz it takes a SerializedProperty as a parameter. And somehow I get a null reference when I try to find the shared$$anonymous$$aterials inside $$anonymous$$eshRenderer. Because shared$$anonymous$$aterials is just a property which returns an array of materials that are marked as internal. And properties are not serializable.

avatar image misher AMAT_Sooraj · Oct 15, 2019 at 02:27 PM 0
Share

serializedProperty is available when you are doing a custom inspector for a component by extending the Editor class and with attribute (CustomEditor(typeof...). I'm not sure where else you can get serialied propertiers, you could try to create a custom editor for mesh renderer with a button to activate a custom method in custom editor script, then select all in the scene and press that button, remember to add attribute that allows you to select multiple components at once... don't remember how it is called.

avatar image AMAT_Sooraj misher · Oct 16, 2019 at 12:49 AM 0
Share

I already tried to do this by creating a new SerializedObject from the $$anonymous$$eshRenderer component and used FindProperty to get the shared$$anonymous$$aterials array. The SerializedObject is fine and its returning all values properly except shared$$anonymous$$aterials, materials, shared$$anonymous$$aterial and material. Because these items are C# properties and not C# variables in the Renderer class. I'm not sure if your method will be any different. But I'll try it and see.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

PrefabUtility.CreatePrefab fails if prefab already exists 1 Answer

Should I be able to cast the result of PrefabUtility.GetPrefabObject() to a GameObject? 0 Answers

How can I destroy a gameobject that is part of a prefab ? 0 Answers

Prefab Interference? 0 Answers

use PrefabUtility after build,PrefabUtility use after bulid 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