using System.Linq; using System.Collections.Generic; using System; using UnityEditor; using UnityEngine; namespace BFEditor.Resource { public class ShaderDependenciesTools { public static void ClearAllShaderDependencies() { Debug.Log("清理shader依赖..."); var guids = AssetDatabase.FindAssets("t:shader", new string[] {ResourceProcessConfig.SHADER_FOLDER_PATH }); var total = guids.Length; var index = 1; foreach (var guid in guids) { var path = AssetDatabase.GUIDToAssetPath(guid); EditorUtility.DisplayProgressBar("正在检查清理shader依赖", path, (float)index / total); ClearShaderDependencies(path); index++; } EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); } public static void ClearShaderDependencies(string shaderPath) { if (!shaderPath.EndsWith(".shader")) { return; } var shaderImporter = (ShaderImporter)AssetImporter.GetAtPath(shaderPath); var shader = shaderImporter.GetShader(); var propertyCount = ShaderUtil.GetPropertyCount(shader); var names = new List(); var textures = new List(); for (var i = 0; i < propertyCount; i++) { if (ShaderUtil.GetPropertyType(shader, i) != ShaderUtil.ShaderPropertyType.TexEnv) { continue; } var propertyName = ShaderUtil.GetPropertyName(shader, i); var tex = shaderImporter.GetDefaultTexture(propertyName); if (tex != null) { Debug.LogWarning(string.Format("shader {0} has default texture {1} ", shader.name, propertyName)); } names.Add(propertyName); textures.Add(null); } shaderImporter.SetDefaultTextures(names.ToArray(), textures.ToArray()); shaderImporter.SaveAndReimport(); } public static void DoClearShaderDependencies() { ClearAllShaderDependencies(); EditorUtility.DisplayDialog("提示", "检查清理shader依赖完成", "ok"); } } }