2015-11-13 125 views
1

我有一個使用數百個3D模型的Unity 4.3項目。現在我們將其升級到Unity 5.由於它將所有着色器替換爲標準,使得它們相對於之前的着色器看起來不同且更暗。爲了獲得相同的外觀,我們需要用標準着色器替換它們的遺留着色器。統一5對傳統着色器的標準着色器

在Unity 5中,有沒有一種方法可以確定材質在升級之前具有的遺留着色器?由於我們正在動態加載模型,我們不知道他們在Unity 4中具有哪些着色器。有沒有一種方法可以在Unity 5中以編程方式讀取它,還是存在Standard到Legacy着色器的映射?

回答

2

可能會讀取着色器的名稱,並嘗試用舊的着色器替換它,例如,通過使用查找表。編譯Unity5着色器與舊版Unity4着色器對應的列表。或者,您必須從新的Unity「標準」着色器中將某些紋理或值傳輸到舊着色器,例如,檢測到使用了一個法線貼圖,您必須選擇適當的遺留着色器並重新指定着色器變量。示例腳本:

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... 
    }; 

} 

測試紅色立方體和具有地球紋理的球體。腳本之前材料被執行: bef1

後: af1

在遊戲中顯示視圖之前: bef2

後:

af2

+0

非常感謝你的詳細解答。我有2個問題。 1.我如何找到映射放在上面可見的地方。我認爲現在有大約20個或更多的遺留着色器,現在都使用標準着色器。從哪裏可以找到它們的新標準着色器名稱,例如diifuse transparent,vertex lit等... – Madhu

+0

2.導入模型時,我嘗試讀取OnAssignMaterialModel函數中AssetPostprocessor類中的舊着色器,但它顯示新的不是舊的。爲了像你一樣獲得oldShaderName,我需要在哪裏設置這個腳本?謝謝 – Madhu