Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Fix All Broken Materials

Discussion in 'Scripting' started by lmaoroot2, Oct 15, 2021.

  1. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Hey everyone, this is my first time posting here so I may get some of the traditional elements of a proper post incorrect.

    I was wondering if someone can point me in a direction, or just give me a script that can find all of the "Hidden/InternalShaderError" having Materials and set the shader to something LIKE the standard shader. Ofcourse, I will be changing the end result of whether its a standard shader or something else, but for now I'm searching for a solution thats faster than just doing it all manually. This is being done in the UnityEditor scripts obviously.

    Thanks.
     
  2. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Sorry, I meant to say "Hidden/InternalErrorShader"
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Have you switched rendering pipelines? There's a wizard for converting all your materials after switching.
     
  4. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    The thing is that i don't want to switch the rendering pipeline as it wont fix the issue im having. I just need a script that loops through every material that is in the assets folder, stores the materials that are missing shaders or are "broken" in a Materials array, and then applies the Standard shader to it or some other shader by name. Im really new to unity scripting so the api is a little confusing for me still.
     
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I just meant there's an automated way to do it already if they broke because you switched rendering pipelines.

    Anyways, you're going to have to get your hands dirty with some editor scripting for this. You'll want to use the AssetDatabase to find all of the materials. Loop through and load them all, then evaluate the shader they're using. Assign whichever shader you want back and then save your changes.
     
  6. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Figured it out. I created this method with the help of a friend:

    private static void fixMaterials()
    {
    string[] guids = AssetDatabase.FindAssets("t: material");
    foreach (string guid in guids)
    {
    string path = AssetDatabase.GUIDToAssetPath(guid);
    Material temp = (Material)AssetDatabase.LoadAssetAtPath(path, typeof(Material));
    if (temp != null && (temp.shader.name == "" || temp.shader.name == "Hidden/InternalErrorShader" || temp.shader.name == null))
    temp.shader = Shader.Find("Standard");
    }
    AssetDatabase.SaveAssets();
    }
     
    pjbaron, P3ndragonLLC and GroZZleR like this.
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,876
    Appreciate your sharing your solution!