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 ronronmx · Mar 17, 2012 at 10:59 PM · materialinstancesharedmaterial

Finding and replacing all "instanced" materials

In my efforts to write an editor script which replaces shaders on all scene objects, I made the mistake of using:

 transform.renderer.material.shader = newShader;

instead of:

 transform.renderer.sharedMaterial.shader = newShader;

Since the editor script is set to [ExecuteInEditMode], when ran, it created "instances" off all materials. Now I have to go through all my objects and reset their material to the default one, since they all have an "instance" material now.

Is there a way to find and loop through all material "instances" and reset them to the default, non-instanced material?

Stephane

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by sumittyagi · Mar 10, 2018 at 11:29 AM

Hi, May be its too late , but i would like to share it for others who have done such a simple mistake like me,

Using this you can revert back to your original materials, without doing it manually and later remove this script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [ExecuteInEditMode]
 public class RevertOriginalMaterials : MonoBehaviour {
 
     Renderer[] All_Objects;
     void Awake()
     {
         All_Objects = GetComponentsInChildren<Renderer> ();
     }
     void Start(){
 
         for (int i = 0; i < All_Objects.Length; i++)
         {            
             Material[] originalMats = Resources.FindObjectsOfTypeAll<Material>();
             Material[] mats = All_Objects [i].sharedMaterials;
             Material[] tempMats = new Material[mats.Length];
             for (int j = 0; j < originalMats.Length; j++) {
 
                 for (int k = 0; k < mats.Length; k++) {
                     if (originalMats [j].name + " (Instance) (Instance)" == mats [k].name) {
 
                         tempMats [k] = originalMats [j];
 
 
                     }
 
                     else if(originalMats [j].name + " (Instance)" == mats[k].name) 
                     {
                         tempMats [k] = originalMats [j];
 
                     }
 
 
                     else if (originalMats [j].name == mats [k].name) {
 
                         tempMats [k] = originalMats [j];
 
 
                     }
                 }
 
             }
 
             All_Objects [i].sharedMaterials = tempMats ;
 
 
         }
     }
 }
 

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 TooManySugar · Aug 13, 2018 at 03:08 PM 0
Share

Life saver! I screwed as the OP XD. I had even multiple materialname(instance) (instance) it took a while for the loops to finish in a 5$$anonymous$$+renderers scene . TX!!

avatar image
1

Answer by fherbst · Mar 17, 2012 at 11:15 PM

With another editor script? As far as I can remember, when Unity instantiates something, it appends "(Clone)" to the name... maybe you could go through all scene materials, check the name, and set the original one. You can probably get the material you need via AssetDatabase.LoadAllAssetsAtPath (searching for the right material asset recursively). Lots of work for a simple mistake :-)

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 ronronmx · Mar 18, 2012 at 12:28 AM 1
Share

It's different for materials when you use "renderer.material" ins$$anonymous$$d of "renderer.shared$$anonymous$$aterial", as using the first example creates an "instance" of the material at runtime, so it doesn't actually exist in the project folder, and it shows up as "material name (instance)" in the editor window.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

how do i create material instances without errors 4 Answers

SetMaterialBlock - anyone use this or know where documentation is? 2 Answers

Why does reading MeshRenderer.material change MeshRenderer.sharedMaterial? 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