Unity 删除Shader默认材质,防止打包反向引用

2024/06 07 18:06
using UnityEditor;
using UnityEngine;

public static class StripShaderDefaultTextures
{
    private static string[] kEmptyNames = new string[0];
    private static Texture[] kEmtpyTextures = new Texture[0];

    [MenuItem("Tools/删除所有Shader的默认纹理")]
    public static void StripAllShaderDefaultTextures()
    {
        var allShaders = AssetDatabase.FindAssets("t:Shader");
        foreach(var guid in allShaders)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            ShaderImporter shaderImporter = ShaderImporter.GetAtPath(assetPath) as ShaderImporter;
            if (shaderImporter != null)
            {
                shaderImporter.SetDefaultTextures(kEmptyNames, kEmtpyTextures);
                shaderImporter.SaveAndReimport();
            }
        }
    }
}