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

Convert Legacy Shaders to Standard in Batch

Discussion in 'Shaders' started by AirRic, Apr 10, 2015.

  1. AirRic

    AirRic

    Joined:
    Apr 10, 2015
    Posts:
    4
    Hey there,

    is there a way to set all shaders in my scene from legacy to the new standard one? I've got around 1000 different materials in this scene so of course I dont want to change them one by one.


    Thanks in advance
    AirRic
     
  2. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    Hmm, should be possible. Rough outline:
    1. Load all Material in the Resources folder into an array using Resoures.LoadAll()
    2. Iterate through the array: Create a new temporary material from script using Material newMat = new Material(Shader.Find("Standard"));
    3. Copy all shader properties from the Legacy material to the appropriate properties of the temp mat (things like mainTexture, color, normal map etc... This needs to be done by hand since not all properties overlap and you might have to do different dub-functions for different legacy shaders)
    4. Change the legacy material's shader to Standard
    5. Copy the properties from the temp material to the old/new material using tempMat.CopyPropertiesFromMaterial(oldMat);
     
  3. AirRic

    AirRic

    Joined:
    Apr 10, 2015
    Posts:
    4
    So there is really no build-in function? I can't imagine that I am the only one with this problem ... and the Unity devs didnt think about this properly ...
     
  4. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    It's impossible to make a converter that can convert every kind of shader, custom or legacy, to the new Standard shader. Writing a function like the one outlined above is trivial, however.