From 8a1b341cfc1cc787c81117718161419eb1e37c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=93=AE?= <834207172@qq.com> Date: Tue, 22 Jul 2025 16:08:43 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90m=E3=80=91=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/MarkAdressable/MarkAdressable.cs | 66 +++---- .../Editor/YooAsset/ShaderVariantCollector.cs | 173 ++++++++++++------ Assets/00.StaryEvo/package.json | 2 +- 3 files changed, 148 insertions(+), 93 deletions(-) diff --git a/Assets/00.StaryEvo/Editor/MarkAdressable/MarkAdressable.cs b/Assets/00.StaryEvo/Editor/MarkAdressable/MarkAdressable.cs index 496eafd..4e8f72d 100644 --- a/Assets/00.StaryEvo/Editor/MarkAdressable/MarkAdressable.cs +++ b/Assets/00.StaryEvo/Editor/MarkAdressable/MarkAdressable.cs @@ -495,39 +495,7 @@ namespace Stary.Evo.Editor Debug.Log( $"UnityEvo:【{packageName}】VariantCount : {collection.variantCount}"); - string groupname = "ShaderVariants"; - AssetBundleCollectorGroup collectorGroup = null; - if (collectorGroupDic.ContainsKey(groupname)) - { - collectorGroupDic[groupname].Collectors.Clear(); - collectorGroup = collectorGroupDic[groupname]; - } - else - { - foreach (var package in AssetBundleCollectorSettingData.Setting.Packages) - { - if (package.PackageName == packageName) - { - collectorGroup = - YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(package, "ShaderVariants"); - } - } - - } - - var guid = AssetDatabase.AssetPathToGUID(localSavePath); - AssetBundleCollector collector = new AssetBundleCollector() - { - CollectPath = localSavePath, - CollectorGUID = guid, - CollectorType = ECollectorType.MainAssetCollector, - AddressRuleName = nameof(AddressByFolderAndFileName), - PackRuleName = nameof(PackShaderVariants), - FilterRuleName = nameof(CollectShaderVariants), - AssetTags = groupname, - }; - YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(collectorGroup, collector); - YooAsset.Editor.AssetBundleCollectorSettingData.SaveFile(); + CreateShaderVariantsGroup(packageName, localSavePath); } else { @@ -536,5 +504,37 @@ namespace Stary.Evo.Editor }; ShaderVariantCollector.Run(localSavePath, packageName, 1000, completedCallback); } + + // 新增方法:创建独立的ShaderVariants分组(防止多个package冲突) + private static void CreateShaderVariantsGroup(string packageName, string localSavePath) + { + string groupname = $"ShaderVariants_{packageName}"; + AssetBundleCollectorGroup collectorGroup = null; + + // 查找或创建package专属分组 + foreach (var package in AssetBundleCollectorSettingData.Setting.Packages) + { + if (package.PackageName == packageName) + { + collectorGroup = YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(package, groupname); + break; + } + } + + var guid = AssetDatabase.AssetPathToGUID(localSavePath); + AssetBundleCollector collector = new AssetBundleCollector() + { + CollectPath = localSavePath, + CollectorGUID = guid, + CollectorType = ECollectorType.MainAssetCollector, + AddressRuleName = nameof(AddressByFolderAndFileName), + PackRuleName = nameof(PackShaderVariants), + FilterRuleName = nameof(CollectShaderVariants), + AssetTags = groupname, + }; + + YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(collectorGroup, collector); + YooAsset.Editor.AssetBundleCollectorSettingData.SaveFile(); + } } } \ No newline at end of file diff --git a/Assets/00.StaryEvo/Editor/YooAsset/ShaderVariantCollector.cs b/Assets/00.StaryEvo/Editor/YooAsset/ShaderVariantCollector.cs index ce6ff97..fe3530c 100644 --- a/Assets/00.StaryEvo/Editor/YooAsset/ShaderVariantCollector.cs +++ b/Assets/00.StaryEvo/Editor/YooAsset/ShaderVariantCollector.cs @@ -22,25 +22,37 @@ public static class ShaderVariantCollector private const float WaitMilliseconds = 3000f; private const float SleepMilliseconds = 3000f; - private static string _savePath; - private static string _packageName; - private static int _processMaxNum; - private static Action _completedCallback; + // private static string _savePath; + // private static string _packageName; + // private static int _processMaxNum; + // private static Action _completedCallback; + // + // private static ESteps _steps = ESteps.None; + // private static Stopwatch _elapsedTime; + //private static List _allMaterials; + // private static List _allSpheres = new List(1000); - private static ESteps _steps = ESteps.None; - private static Stopwatch _elapsedTime; - private static List _allMaterials; - private static List _allSpheres = new List(1000); + // 新增任务队列 + private static Queue _taskQueue = new Queue(); + private static CollectTask _currentTask; + // 新增初始化方法 + private static void InitializeTask(CollectTask task) + { + // 原有Run方法中的初始化逻辑迁移到这里 + if (Path.HasExtension(task.SavePath) == false) + task.SavePath = $"{task.SavePath}.shadervariants"; + AssetDatabase.DeleteAsset(task.SavePath); + EditorTools.CreateFileDirectory(task.SavePath); + + task.Steps = ESteps.Prepare; + } /// /// 开始收集 /// public static void Run(string savePath, string packageName, int processMaxNum, Action completedCallback) { - if (_steps != ESteps.None) - return; - if (Path.HasExtension(savePath) == false) savePath = $"{savePath}.shadervariants"; if (Path.GetExtension(savePath) != ".shadervariants") @@ -48,98 +60,115 @@ public static class ShaderVariantCollector if (string.IsNullOrEmpty(packageName)) throw new System.Exception("Package name is null or empty !"); - // 注意:先删除再保存,否则ShaderVariantCollection内容将无法及时刷新 - AssetDatabase.DeleteAsset(savePath); - EditorTools.CreateFileDirectory(savePath); - _savePath = savePath; - _packageName = packageName; - _processMaxNum = processMaxNum; - _completedCallback = completedCallback; - // // 聚焦到游戏窗口 - // EditorTools.FocusUnityGameWindow(); - // - // // 创建临时测试场景 - // CreateTempScene(); + // 将新任务加入队列 + _taskQueue.Enqueue(new CollectTask + { + SavePath = savePath, + PackageName = packageName, + ProcessMaxNum = processMaxNum, + CompletedCallback = completedCallback + }); + // 如果当前没有正在执行的任务,开始执行队列 + if (_currentTask == null) + { + EditorApplication.update -= EditorUpdate; + EditorApplication.update += EditorUpdate; + } - _steps = ESteps.Prepare; - EditorApplication.update += EditorUpdate; } private static void EditorUpdate() { - if (_steps == ESteps.None) + // 当前没有活动任务时从队列获取新任务 + if (_currentTask == null && _taskQueue.Count > 0) + { + _currentTask = _taskQueue.Dequeue(); + InitializeTask(_currentTask); + } + if (_currentTask == null) return; - if (_steps == ESteps.Prepare) + + if (_currentTask.Steps == ESteps.None) + return; + + if (_currentTask.Steps == ESteps.Prepare) { ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection(); - _steps = ESteps.CollectAllMaterial; + _currentTask.Steps = ESteps.CollectAllMaterial; return; //等待一帧 } - if (_steps == ESteps.CollectAllMaterial) + if (_currentTask.Steps == ESteps.CollectAllMaterial) { - _allMaterials = GetAllMaterials(); - _steps = ESteps.CollectVariants; + _currentTask.AllMaterials = GetAllMaterials(); + _currentTask.Steps = ESteps.CollectVariants; return; //等待一帧 } - if (_steps == ESteps.CollectVariants) + if (_currentTask.Steps == ESteps.CollectVariants) { - int count = Mathf.Min(_processMaxNum, _allMaterials.Count); - List range = _allMaterials.GetRange(0, count); - _allMaterials.RemoveRange(0, count); + int count = Mathf.Min(_currentTask.ProcessMaxNum, _currentTask.AllMaterials.Count); + List range = _currentTask.AllMaterials.GetRange(0, count); + _currentTask.AllMaterials.RemoveRange(0, count); CollectVariants(range); - if (_allMaterials.Count > 0) + if ( _currentTask.AllMaterials.Count > 0) { - _elapsedTime = Stopwatch.StartNew(); - _steps = ESteps.CollectSleeping; + _currentTask.ElapsedTime = Stopwatch.StartNew(); + _currentTask.Steps = ESteps.CollectSleeping; } else { - _elapsedTime = Stopwatch.StartNew(); - _steps = ESteps.WaitingDone; + _currentTask.ElapsedTime = Stopwatch.StartNew(); + _currentTask.Steps = ESteps.WaitingDone; } } - if (_steps == ESteps.CollectSleeping) + if (_currentTask.Steps == ESteps.CollectSleeping) { - if (_elapsedTime.ElapsedMilliseconds > SleepMilliseconds) + if (_currentTask.ElapsedTime.ElapsedMilliseconds > SleepMilliseconds) { DestroyAllSpheres(); - _elapsedTime.Stop(); - _steps = ESteps.CollectVariants; + _currentTask.ElapsedTime.Stop(); + _currentTask.Steps = ESteps.CollectVariants; } } - if (_steps == ESteps.WaitingDone) + if ( _currentTask.Steps == ESteps.WaitingDone) { // 注意:一定要延迟保存才会起效 - if (_elapsedTime.ElapsedMilliseconds > WaitMilliseconds) + if ( _currentTask.ElapsedTime.ElapsedMilliseconds > WaitMilliseconds) { - _elapsedTime.Stop(); - _steps = ESteps.None; + _currentTask.ElapsedTime.Stop(); + _currentTask.Steps = ESteps.None; // 保存结果并创建清单 - ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_savePath); + ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_currentTask.SavePath); CreateManifest(); UnityEngine.Debug.Log($"搜集SVC完毕!"); - EditorApplication.update -= EditorUpdate; - _completedCallback?.Invoke(); + + _currentTask.CompletedCallback?.Invoke(); + _currentTask = null; + + // 如果队列还有任务,立即触发下一次更新 + if (_taskQueue.Count > 0) + EditorApplication.QueuePlayerLoopUpdate(); } } } + private static void CreateTempScene() { EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects); } + private static List GetAllMaterials() { // 获取所有打包的资源 - CollectResult collectResult = AssetBundleCollectorSettingData.Setting.BeginCollect(_packageName, false, false); + CollectResult collectResult = AssetBundleCollectorSettingData.Setting.BeginCollect(_currentTask.PackageName, false, false); // 搜集所有材质球 int progressValue = 0; @@ -152,7 +181,8 @@ public static class ShaderVariantCollector if (result.Contains(assetPath) == false) result.Add(assetPath); } - foreach(var dependAssetInfo in collectAssetInfo.DependAssets) + + foreach (var dependAssetInfo in collectAssetInfo.DependAssets) { if (dependAssetInfo.AssetType == typeof(UnityEngine.Material)) { @@ -161,13 +191,16 @@ public static class ShaderVariantCollector result.Add(assetPath); } } + EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, collectResult.CollectAssets.Count); } + EditorTools.ClearProgressBar(); // 返回结果 return result.ToList(); } + private static void CollectVariants(List materials) { Camera camera = Camera.main; @@ -195,7 +228,7 @@ public static class ShaderVariantCollector var position = new Vector3(x - halfWidth + 1f, y - halfHeight + 1f, 0f); var go = CreateSphere(material, position, i); if (go != null) - _allSpheres.Add(go); + _currentTask.AllSpheres.Add(go); if (x == xMax) { x = 0; @@ -205,10 +238,13 @@ public static class ShaderVariantCollector { x++; } + EditorTools.DisplayProgressBar("照射所有材质球", ++progressValue, materials.Count); } + EditorTools.ClearProgressBar(); } + private static GameObject CreateSphere(string assetPath, Vector3 position, int index) { var material = AssetDatabase.LoadAssetAtPath(assetPath); @@ -222,30 +258,49 @@ public static class ShaderVariantCollector go.name = $"Sphere_{index} | {material.name}"; return go; } + private static void DestroyAllSpheres() { - foreach (var go in _allSpheres) + foreach (var go in _currentTask.AllSpheres) { GameObject.DestroyImmediate(go); } - _allSpheres.Clear(); + + _currentTask.AllSpheres.Clear(); // 尝试释放编辑器加载的资源 EditorUtility.UnloadUnusedAssetsImmediate(true); } + private static void CreateManifest() { AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); - ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath(_savePath); + ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath(_currentTask.SavePath); if (svc != null) { var wrapper = ShaderVariantCollectionManifest.Extract(svc); string jsonData = JsonUtility.ToJson(wrapper, true); - string savePath = _savePath.Replace(".shadervariants", ".json"); + string savePath = _currentTask.SavePath.Replace(".shadervariants", ".json"); File.WriteAllText(savePath, jsonData); } AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); } -} \ No newline at end of file + + + + // 新增任务类 + private class CollectTask + { + public string SavePath; + public string PackageName; + public int ProcessMaxNum; + public Action CompletedCallback; + public ESteps Steps = ESteps.None; + public Stopwatch ElapsedTime; + public List AllMaterials; + public List AllSpheres = new List(); + } +} + diff --git a/Assets/00.StaryEvo/package.json b/Assets/00.StaryEvo/package.json index 8167d75..90cbedc 100644 --- a/Assets/00.StaryEvo/package.json +++ b/Assets/00.StaryEvo/package.json @@ -1,6 +1,6 @@ { "name": "com.staryevo.main", - "version": "1.3.19", + "version": "1.3.20", "displayName": "00.StaryEvo", "description": "This is an Framework package(后台服务器版本,端口9527)", "unity": "2021.3",