Reputation: 1229
I have a Unity 4.3 project which uses hundreds of 3D models. Now we are upgrading it to Unity 5. With that it's replacing all the shaders as Standard which makes them look different and darker relative to the previous shaders we had. To get the same look we need to replace the Standard shader with the Legacy shaders they had.
In Unity 5, is there a way to determine the legacy shader a material had prior to the upgrade? Since we are loading the models dynamically, we don't know which shaders they had in Unity 4. Is there a way to read it programatically in Unity 5 or is there a mapping of Standard to Legacy shaders?
Upvotes: 1
Views: 3308
Reputation: 5353
A possibility would be to read the name of the shader, and try to replace it with the old legacy shader, e.g. by using a lookup table. Compile a list of what new Unity5 shader corresponds to the old Unity4 legacy shader. Optionally, you mave have to transfer some textures or values from the new Unity "Standard" shader to your legacy shader, if you e.g. detect, that a normal map was used, you must choose the apriopate legacy shader for that and reassign the shader variables. Example script:
using UnityEngine;
using System.Collections.Generic;
[RequireComponent(typeof(Renderer))]
public class ChangeToLegacyShader : MonoBehaviour {
// At startup..
void Start () {
var oldShaderName = GetComponent<Renderer>().material.shader.name;
//try to search for the shader name in our lookup table
if (shaderTable.ContainsKey(oldShaderName))
{
//Replace the shader
var newShader = Shader.Find(shaderTable[oldShaderName]);
GetComponent<Renderer>().material.shader = newShader;
//Additional stuff: Set new paramers, modifiy textures, correct shader variables,...
}
else
{
Debug.LogWarning("Couldn't find replacement for shader: " + oldShaderName);
}
//Remove this script after we're done.
Destroy(this);
}
public static Dictionary<string, string> shaderTable = new Dictionary<string, string>()
{
{"Standard", "Legacy Shaders/Diffuse"}, //map standard to the diffuse shader, good enough for this small example
{"Standard (Specular Setup)", "Legacy Shaders/Specular"}
//more...
};
}
Tested on a red cube and a sphere with an earth texture. Materials before script was executed:
After:
Upvotes: 2