From 2cbfca4f3b4e6f69b34e0bdb41db29d2d572b8cf Mon Sep 17 00:00:00 2001 From: hevinci Date: Mon, 15 Jul 2024 18:51:32 +0800 Subject: [PATCH] update extension sample MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 着色器变种文件增加内部排序 --- .../ShaderVariantCollectionManifest.cs | 45 ++++++++++++++++--- .../ShaderVariantCollector.cs | 4 +- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs b/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs index 49414bf..7175ce2 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollectionManifest.cs @@ -11,8 +11,10 @@ using UnityEditor; public class ShaderVariantCollectionManifest { [Serializable] - public class ShaderVariantElement + public class ShaderVariantElement : IComparable { + public string SortValue { private set; get; } + /// /// Pass type to use in this variant. /// @@ -22,11 +24,31 @@ public class ShaderVariantCollectionManifest /// Array of shader keywords to use in this variant. /// public string[] Keywords; + + public void MakeSortValue() + { + string combineKeyword = string.Empty; + for (int i = 0; i < Keywords.Length; i++) + { + if (i == 0) + combineKeyword = Keywords[0]; + else + combineKeyword = $"{combineKeyword}+{Keywords[0]}"; + } + + SortValue = $"{PassType}+{combineKeyword}"; + } + public int CompareTo(ShaderVariantElement other) + { + return SortValue.CompareTo(other.SortValue); + } } [Serializable] public class ShaderVariantInfo : IComparable { + public string SortValue { private set; get; } + /// /// 着色器资源路径. /// @@ -47,11 +69,13 @@ public class ShaderVariantCollectionManifest /// public List ShaderVariantElements = new List(1000); + public void MakeSortValue() + { + SortValue = AssetPath + "+" + ShaderName; + } public int CompareTo(ShaderVariantInfo other) { - string thisStr = AssetPath + "+" +ShaderName; - string otherStr = other.AssetPath + "+" + other.ShaderName; - return thisStr.CompareTo(otherStr); + return SortValue.CompareTo(other.SortValue); } } @@ -76,10 +100,15 @@ public class ShaderVariantCollectionManifest /// public void AddShaderVariant(string assetPath, string shaderName, PassType passType, string[] keywords) { + // 排序Keyword列表 + List temper = new List(keywords); + temper.Sort(); + var info = GetOrCreateShaderVariantInfo(assetPath, shaderName); ShaderVariantElement element = new ShaderVariantElement(); element.PassType = passType; - element.Keywords = keywords; + element.Keywords = temper.ToArray(); + element.MakeSortValue(); info.ShaderVariantElements.Add(element); info.ShaderVariantCount++; } @@ -91,6 +120,7 @@ public class ShaderVariantCollectionManifest ShaderVariantInfo newInfo = new ShaderVariantInfo(); newInfo.AssetPath = assetPath; newInfo.ShaderName = shaderName; + newInfo.MakeSortValue(); ShaderVariantInfos.Add(newInfo); return newInfo; } @@ -150,6 +180,11 @@ public class ShaderVariantCollectionManifest // 重新排序 manifest.ShaderVariantInfos.Sort(); + foreach (var shaderVariantInfo in manifest.ShaderVariantInfos) + { + shaderVariantInfo.ShaderVariantElements.Sort(); + } + return manifest; } } \ No newline at end of file diff --git a/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollector.cs b/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollector.cs index 2eada21..f0161dc 100644 --- a/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollector.cs +++ b/Assets/YooAsset/Samples~/Extension Sample/Editor/ShaderVariantCollector/ShaderVariantCollector.cs @@ -21,8 +21,8 @@ public static class ShaderVariantCollector WaitingDone, } - private const float WaitMilliseconds = 1000f; - private const float SleepMilliseconds = 2000f; + private const float WaitMilliseconds = 3000f; + private const float SleepMilliseconds = 3000f; private static string _savePath; private static string _packageName; private static int _processMaxNum;