diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionHelper.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionHelper.cs index b03fe1a..816079a 100644 --- a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionHelper.cs +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionHelper.cs @@ -10,103 +10,31 @@ namespace YooAsset.Editor { public static class ShaderVariantCollectionHelper { - [Serializable] - public class ShaderVariantWrapper + public static void ClearCurrentShaderVariantCollection() { - /// - /// Shader asset path in editor. - /// - public string AssetPath; - - /// - /// Shader name. - /// - public string ShaderName; - - /// - /// Pass type to use in this variant. - /// - public PassType PassType; - - /// - /// Array of shader keywords to use in this variant. - /// - public string[] Keywords; - - public ShaderVariantWrapper(string assetPath, string shaderName, PassType passType, params string[] keywords) - { - AssetPath = assetPath; - ShaderName = shaderName; - PassType = passType; - Keywords = keywords; - } + EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "ClearCurrentShaderVariantCollection"); + } + public static void SaveCurrentShaderVariantCollection(string savePath) + { + EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "SaveCurrentShaderVariantCollection", savePath); + } + public static int GetCurrentShaderVariantCollectionShaderCount() + { + return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionShaderCount"); + } + public static int GetCurrentShaderVariantCollectionVariantCount() + { + return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionVariantCount"); } - [Serializable] - public class ShaderVariantCollectionWrapper + /// + /// 获取着色器的变种总数量 + /// + public static string GetShaderVariantCount(string assetPath) { - /// - /// Number of shaders in this collection - /// - public int ShaderCount; - - /// - /// Number of total varians in this collection - /// - public int VariantCount; - - /// - /// Shader variants list. - /// - public List ShaderVariants = new List(1000); - - public void Add(ShaderVariantWrapper variant) - { - ShaderVariants.Add(variant); - } - } - - - public static ShaderVariantCollectionWrapper Extract(ShaderVariantCollection svc) - { - var result = new ShaderVariantCollectionWrapper(); - using (var so = new SerializedObject(svc)) - { - var shaderArray = so.FindProperty("m_Shaders.Array"); - if (shaderArray != null && shaderArray.isArray) - { - for (int i = 0; i < shaderArray.arraySize; ++i) - { - var shaderRef = shaderArray.FindPropertyRelative($"data[{i}].first"); - var shaderVariantsArray = shaderArray.FindPropertyRelative($"data[{i}].second.variants"); - if (shaderRef != null && shaderRef.propertyType == SerializedPropertyType.ObjectReference && shaderVariantsArray != null && shaderVariantsArray.isArray) - { - var shader = shaderRef.objectReferenceValue as Shader; - if (shader == null) - { - throw new Exception("Invalid shader in ShaderVariantCollection file."); - } - - string shaderAssetPath = AssetDatabase.GetAssetPath(shader); - string shaderName = shader.name; - - // 添加变种信息 - for (int j = 0; j < shaderVariantsArray.arraySize; ++j) - { - var propKeywords = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].keywords"); - var propPassType = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].passType"); - if (propKeywords != null && propPassType != null && propKeywords.propertyType == SerializedPropertyType.String) - { - string[] keywords = propKeywords.stringValue.Split(' '); - PassType pathType = (PassType)propPassType.intValue; - result.Add(new ShaderVariantWrapper(shaderAssetPath, shaderName, pathType, keywords)); - } - } - } - } - } - } - return result; + Shader shader = AssetDatabase.LoadAssetAtPath(assetPath); + var variantCount = EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetVariantCount", shader, true); + return variantCount.ToString(); } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs new file mode 100644 index 0000000..402f195 --- /dev/null +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using UnityEngine; +using UnityEngine.Rendering; +using UnityEditor; + +namespace YooAsset.Editor +{ + public static class ShaderVariantCollectionReadme + { + [Serializable] + public class ShaderVariantElement + { + /// + /// Pass type to use in this variant. + /// + public PassType PassType; + + /// + /// Array of shader keywords to use in this variant. + /// + public string[] Keywords; + } + + [Serializable] + public class ShaderVariantInfo + { + /// + /// Shader asset path in editor. + /// + public string AssetPath; + + /// + /// Shader name. + /// + public string ShaderName; + + /// + /// Shader variants elements list. + /// + public List ShaderVariantElements = new List(1000); + } + + [Serializable] + public class ShaderVariantCollectionManifest + { + /// + /// Number of shaders in this collection + /// + public int ShaderTotalCount; + + /// + /// Number of total varians in this collection + /// + public int VariantTotalCount; + + /// + /// Shader variants info list. + /// + public List ShaderVariantInfos = new List(1000); + + /// + /// 添加着色器变种信息 + /// + public void AddShaderVariant(string assetPath, string shaderName, PassType passType, string[] keywords) + { + var info = GetOrCreateShaderVariantInfo(assetPath, shaderName); + ShaderVariantElement element = new ShaderVariantElement(); + element.PassType = passType; + element.Keywords = keywords; + info.ShaderVariantElements.Add(element); + } + private ShaderVariantInfo GetOrCreateShaderVariantInfo(string assetPath, string shaderName) + { + var selectList = ShaderVariantInfos.Where(t => t.ShaderName == shaderName && t.AssetPath == assetPath).ToList(); + if (selectList.Count == 0) + { + ShaderVariantInfo newInfo = new ShaderVariantInfo(); + newInfo.AssetPath = assetPath; + newInfo.ShaderName = shaderName; + ShaderVariantInfos.Add(newInfo); + return newInfo; + } + + if (selectList.Count != 1) + throw new Exception("Should never get here !"); + + return selectList[0]; + } + } + + + /// + /// 解析SVC文件并将数据写入到清单 + /// + public static ShaderVariantCollectionManifest Extract(ShaderVariantCollection svc) + { + var manifest = new ShaderVariantCollectionManifest(); + manifest.ShaderTotalCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount(); + manifest.VariantTotalCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount(); + + using (var so = new SerializedObject(svc)) + { + var shaderArray = so.FindProperty("m_Shaders.Array"); + if (shaderArray != null && shaderArray.isArray) + { + for (int i = 0; i < shaderArray.arraySize; ++i) + { + var shaderRef = shaderArray.FindPropertyRelative($"data[{i}].first"); + var shaderVariantsArray = shaderArray.FindPropertyRelative($"data[{i}].second.variants"); + if (shaderRef != null && shaderRef.propertyType == SerializedPropertyType.ObjectReference && shaderVariantsArray != null && shaderVariantsArray.isArray) + { + var shader = shaderRef.objectReferenceValue as Shader; + if (shader == null) + { + throw new Exception("Invalid shader in ShaderVariantCollection file."); + } + + string shaderAssetPath = AssetDatabase.GetAssetPath(shader); + string shaderName = shader.name; + + // 添加变种信息 + for (int j = 0; j < shaderVariantsArray.arraySize; ++j) + { + var propKeywords = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].keywords"); + var propPassType = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].passType"); + if (propKeywords != null && propPassType != null && propKeywords.propertyType == SerializedPropertyType.String) + { + string[] keywords = propKeywords.stringValue.Split(' '); + PassType pathType = (PassType)propPassType.intValue; + manifest.AddShaderVariant(shaderAssetPath, shaderName, pathType, keywords); + } + } + } + } + } + } + + return manifest; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs.meta b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs.meta new file mode 100644 index 0000000..1ccf9c8 --- /dev/null +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 97098b04691f5c046ac4829f1d72f425 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs index bf6a598..8542d64 100644 --- a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs @@ -28,7 +28,7 @@ namespace YooAsset.Editor EditorApplication.update -= EditorUpdate; // 保存结果 - SaveCurrentShaderVariantCollection(); + ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath); // 创建说明文件 CreateReadme(); @@ -61,7 +61,7 @@ namespace YooAsset.Editor EditorTools.FocusUnityGameWindow(); // 清空旧数据 - ClearCurrentShaderVariantCollection(); + ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection(); // 创建临时测试场景 CreateTemperScene(); @@ -181,7 +181,6 @@ namespace YooAsset.Editor go.transform.position = position; go.name = $"Sphere_{index}|{material.name}"; } - private static void CreateReadme() { AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); @@ -189,29 +188,13 @@ namespace YooAsset.Editor ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath(_saveFilePath); if (svc != null) { - var wrapper = ShaderVariantCollectionHelper.Extract(svc); + var wrapper = ShaderVariantCollectionReadme.Extract(svc); string jsonContents = JsonUtility.ToJson(wrapper, true); - string savePath = _saveFilePath.Replace(".shadervariants", "_Readme.json"); + string savePath = _saveFilePath.Replace(".shadervariants", "Manifest.json"); File.WriteAllText(savePath, jsonContents); } AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); } - private static void ClearCurrentShaderVariantCollection() - { - EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "ClearCurrentShaderVariantCollection"); - } - private static void SaveCurrentShaderVariantCollection() - { - EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "SaveCurrentShaderVariantCollection", _saveFilePath); - } - public static int GetCurrentShaderVariantCollectionShaderCount() - { - return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionShaderCount"); - } - public static int GetCurrentShaderVariantCollectionVariantCount() - { - return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionVariantCount"); - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs index 13aa1e6..8ca66d7 100644 --- a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs @@ -25,8 +25,8 @@ namespace YooAsset.Editor EditorGUILayout.Space(); ShaderVariantCollectorSettingData.Setting.SavePath = EditorGUILayout.TextField("收集文件保存路径", ShaderVariantCollectorSettingData.Setting.SavePath); - int currentShaderCount = ShaderVariantCollector.GetCurrentShaderVariantCollectionShaderCount(); - int currentVariantCount = ShaderVariantCollector.GetCurrentShaderVariantCollectionVariantCount(); + int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount(); + int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount(); EditorGUILayout.LabelField($"CurrentShaderCount : {currentShaderCount}"); EditorGUILayout.LabelField($"CurrentVariantCount : {currentVariantCount}");