From 9397a0ebae2d503d6a00506fc0467d22bc3dc4fe Mon Sep 17 00:00:00 2001 From: hevinci Date: Thu, 3 Nov 2022 20:17:07 +0800 Subject: [PATCH] Update ShaderVariantCollector --- Assets/YooAsset/Editor/EditorDefine.cs | 9 +- .../ShaderVariantCollectionManifest.cs | 82 +++++++------- .../ShaderVariantCollector.cs | 19 ++-- .../ShaderVariantCollectorWindow.cs | 101 +++++++++++------- .../ShaderVariantCollectorWindow.uxml | 9 ++ .../ShaderVariantCollectorWindow.uxml.meta | 10 ++ 6 files changed, 137 insertions(+), 93 deletions(-) create mode 100644 Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.uxml create mode 100644 Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.uxml.meta diff --git a/Assets/YooAsset/Editor/EditorDefine.cs b/Assets/YooAsset/Editor/EditorDefine.cs index 202a49f..cbb03fc 100644 --- a/Assets/YooAsset/Editor/EditorDefine.cs +++ b/Assets/YooAsset/Editor/EditorDefine.cs @@ -8,7 +8,14 @@ namespace YooAsset.Editor /// /// 停靠窗口类型集合 /// - public static readonly Type[] DockedWindowTypes = { typeof(AssetBundleBuilderWindow), typeof(AssetBundleCollectorWindow), typeof(AssetBundleDebuggerWindow), typeof(AssetBundleReporterWindow)}; + public static readonly Type[] DockedWindowTypes = + { + typeof(AssetBundleBuilderWindow), + typeof(AssetBundleCollectorWindow), + typeof(AssetBundleDebuggerWindow), + typeof(AssetBundleReporterWindow), + typeof(ShaderVariantCollectorWindow) + }; #endif } diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs index 402f195..54ff134 100644 --- a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs @@ -9,7 +9,8 @@ using UnityEditor; namespace YooAsset.Editor { - public static class ShaderVariantCollectionReadme + [Serializable] + public class ShaderVariantCollectionManifest { [Serializable] public class ShaderVariantElement @@ -44,52 +45,49 @@ namespace YooAsset.Editor 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) { - /// - /// 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) { - var info = GetOrCreateShaderVariantInfo(assetPath, shaderName); - ShaderVariantElement element = new ShaderVariantElement(); - element.PassType = passType; - element.Keywords = keywords; - info.ShaderVariantElements.Add(element); + ShaderVariantInfo newInfo = new ShaderVariantInfo(); + newInfo.AssetPath = assetPath; + newInfo.ShaderName = shaderName; + ShaderVariantInfos.Add(newInfo); + return newInfo; } - 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 !"); + if (selectList.Count != 1) + throw new Exception("Should never get here !"); - return selectList[0]; - } + return selectList[0]; } diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs index aa83772..1d69b1b 100644 --- a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollector.cs @@ -30,8 +30,8 @@ namespace YooAsset.Editor // 保存结果 ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath); - // 创建说明文件 - CreateReadme(); + // 创建清单 + CreateManifest(); Debug.Log($"搜集SVC完毕!"); _completedCallback?.Invoke(); @@ -64,7 +64,7 @@ namespace YooAsset.Editor ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection(); // 创建临时测试场景 - CreateTemperScene(); + CreateTempScene(); // 收集着色器变种 var materials = GetAllMaterials(); @@ -76,9 +76,8 @@ namespace YooAsset.Editor _elapsedTime.Start(); } - private static void CreateTemperScene() + private static void CreateTempScene() { - // 创建临时场景 EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects); } private static List GetAllMaterials() @@ -186,17 +185,17 @@ namespace YooAsset.Editor go.transform.position = position; go.name = $"Sphere_{index}|{material.name}"; } - private static void CreateReadme() + private static void CreateManifest() { AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath(_saveFilePath); if (svc != null) { - var wrapper = ShaderVariantCollectionReadme.Extract(svc); - string jsonContents = JsonUtility.ToJson(wrapper, true); - string savePath = _saveFilePath.Replace(".shadervariants", "Manifest.json"); - File.WriteAllText(savePath, jsonContents); + var wrapper = ShaderVariantCollectionManifest.Extract(svc); + string jsonData = JsonUtility.ToJson(wrapper, true); + string savePath = _saveFilePath.Replace(".shadervariants", ".json"); + File.WriteAllText(savePath, jsonData); } AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); diff --git a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs index 8ca66d7..9d8b9d9 100644 --- a/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs +++ b/Assets/YooAsset/Editor/ShaderVariantCollector/ShaderVariantCollectorWindow.cs @@ -1,61 +1,82 @@ -using UnityEngine; +#if UNITY_2019_4_OR_NEWER +using System; +using System.Linq; +using System.Collections.Generic; using UnityEditor; +using UnityEngine; +using UnityEditor.UIElements; +using UnityEngine.UIElements; namespace YooAsset.Editor { - public class ShaderVariantCollectionWindow : EditorWindow + public class ShaderVariantCollectorWindow : EditorWindow { - static ShaderVariantCollectionWindow _thisInstance; - [MenuItem("YooAsset/ShaderVariant Collector", false, 201)] - static void ShowWindow() + public static void ShowExample() { - if (_thisInstance == null) - { - _thisInstance = GetWindow("着色器变种收集工具"); - _thisInstance.minSize = new Vector2(800, 600); - } - _thisInstance.Show(); + ShaderVariantCollectorWindow window = GetWindow("着色器变种收集工具", true, EditorDefine.DockedWindowTypes); + window.minSize = new Vector2(800, 600); } - private ShaderVariantCollection _selectSVC; + private Button _collectButton; + private TextField _collectOutputField; + private Label _currentShaderCountField; + private Label _currentVariantCountField; - private void OnGUI() + public void CreateGUI() { - EditorGUILayout.Space(); - ShaderVariantCollectorSettingData.Setting.SavePath = EditorGUILayout.TextField("收集文件保存路径", ShaderVariantCollectorSettingData.Setting.SavePath); - - int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount(); - int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount(); - EditorGUILayout.LabelField($"CurrentShaderCount : {currentShaderCount}"); - EditorGUILayout.LabelField($"CurrentVariantCount : {currentVariantCount}"); - - // 搜集变种 - EditorGUILayout.Space(); - if (GUILayout.Button("搜集变种", GUILayout.MaxWidth(80))) + try { - ShaderVariantCollector.Run(ShaderVariantCollectorSettingData.Setting.SavePath, null); - } + VisualElement root = this.rootVisualElement; - // 查询 - EditorGUILayout.Space(); - if (GUILayout.Button("查询", GUILayout.MaxWidth(80))) - { - string resultPath = EditorTools.OpenFilePath("Select File", "Assets/", "shadervariants"); - if (string.IsNullOrEmpty(resultPath)) + // 加载布局文件 + var visualAsset = EditorHelper.LoadWindowUXML(); + if (visualAsset == null) return; - string assetPath = EditorTools.AbsolutePathToAssetPath(resultPath); - _selectSVC = AssetDatabase.LoadAssetAtPath(assetPath); + + visualAsset.CloneTree(root); + + // 文件输出目录 + _collectOutputField = root.Q("CollectOutput"); + _collectOutputField.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.SavePath); + _collectOutputField.RegisterValueChangedCallback(evt => + { + ShaderVariantCollectorSettingData.Setting.SavePath = _collectOutputField.value; + }); + + _currentShaderCountField = root.Q