【unity进阶】一键查找shader并进行替换

制作的过程中偶尔会遇到自带的shader出现问题需要批量替换,这时候如果一个个的去找很麻烦而且容易有遗漏,我这里提供一个思路来批量替换,关键代码如下:

static void ShaderSoftEdgeUnlit()
    {
        Shader shader = Shader.Find("Unlit/Soft Edge Unlit");
        string[] tempMaterialsPath =  AssetDatabase.GetAllAssetPaths();
        List tempMaterials = new List();
        for (int i = 0; i < tempMaterialsPath.Length; i++)
        {
            string ext = Path.GetExtension(tempMaterialsPath[i]);
            if (ext != ".mat")
            {
                continue;
            }
            tempMaterials.Add(AssetDatabase.LoadAssetAtPath(tempMaterialsPath[i], typeof (Material)) as Material);
        }
        if (tempMaterials.Count != 0)
        {
            for (int i = 0; i < tempMaterials.Count; i++)
            {
                if (tempMaterials[i] == null)
                {
                    continue;
                }
                if (tempMaterials[i].shader.name == "Legacy Shaders/Transparent/Cutout/Soft Edge Unlit")
                {
                    tempMaterials[i].shader = shader;
                }
            }
        }
    }

你可能感兴趣的:(【unity进阶】一键查找shader并进行替换)