【m】测试

This commit is contained in:
2025-07-22 16:08:43 +08:00
parent b068422bed
commit 8a1b341cfc
3 changed files with 148 additions and 93 deletions

View File

@@ -495,26 +495,32 @@ namespace Stary.Evo.Editor
Debug.Log( Debug.Log(
$"UnityEvo:【{packageName}】VariantCount : {collection.variantCount}"); $"UnityEvo:【{packageName}】VariantCount : {collection.variantCount}");
string groupname = "ShaderVariants"; CreateShaderVariantsGroup(packageName, localSavePath);
AssetBundleCollectorGroup collectorGroup = null;
if (collectorGroupDic.ContainsKey(groupname))
{
collectorGroupDic[groupname].Collectors.Clear();
collectorGroup = collectorGroupDic[groupname];
} }
else else
{ {
throw new Exception("Failed to Collect shader Variants.");
}
};
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) foreach (var package in AssetBundleCollectorSettingData.Setting.Packages)
{ {
if (package.PackageName == packageName) if (package.PackageName == packageName)
{ {
collectorGroup = collectorGroup = YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(package, groupname);
YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(package, "ShaderVariants"); break;
} }
} }
}
var guid = AssetDatabase.AssetPathToGUID(localSavePath); var guid = AssetDatabase.AssetPathToGUID(localSavePath);
AssetBundleCollector collector = new AssetBundleCollector() AssetBundleCollector collector = new AssetBundleCollector()
{ {
@@ -526,15 +532,9 @@ namespace Stary.Evo.Editor
FilterRuleName = nameof(CollectShaderVariants), FilterRuleName = nameof(CollectShaderVariants),
AssetTags = groupname, AssetTags = groupname,
}; };
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(collectorGroup, collector); YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(collectorGroup, collector);
YooAsset.Editor.AssetBundleCollectorSettingData.SaveFile(); YooAsset.Editor.AssetBundleCollectorSettingData.SaveFile();
} }
else
{
throw new Exception("Failed to Collect shader Variants.");
}
};
ShaderVariantCollector.Run(localSavePath, packageName, 1000, completedCallback);
}
} }
} }

View File

@@ -22,25 +22,37 @@ public static class ShaderVariantCollector
private const float WaitMilliseconds = 3000f; private const float WaitMilliseconds = 3000f;
private const float SleepMilliseconds = 3000f; private const float SleepMilliseconds = 3000f;
private static string _savePath; // private static string _savePath;
private static string _packageName; // private static string _packageName;
private static int _processMaxNum; // private static int _processMaxNum;
private static Action _completedCallback; // private static Action _completedCallback;
//
// private static ESteps _steps = ESteps.None;
// private static Stopwatch _elapsedTime;
//private static List<string> _allMaterials;
// private static List<GameObject> _allSpheres = new List<GameObject>(1000);
private static ESteps _steps = ESteps.None; // 新增任务队列
private static Stopwatch _elapsedTime; private static Queue<CollectTask> _taskQueue = new Queue<CollectTask>();
private static List<string> _allMaterials; private static CollectTask _currentTask;
private static List<GameObject> _allSpheres = new List<GameObject>(1000);
// 新增初始化方法
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;
}
/// <summary> /// <summary>
/// 开始收集 /// 开始收集
/// </summary> /// </summary>
public static void Run(string savePath, string packageName, int processMaxNum, Action completedCallback) public static void Run(string savePath, string packageName, int processMaxNum, Action completedCallback)
{ {
if (_steps != ESteps.None)
return;
if (Path.HasExtension(savePath) == false) if (Path.HasExtension(savePath) == false)
savePath = $"{savePath}.shadervariants"; savePath = $"{savePath}.shadervariants";
if (Path.GetExtension(savePath) != ".shadervariants") if (Path.GetExtension(savePath) != ".shadervariants")
@@ -48,98 +60,115 @@ public static class ShaderVariantCollector
if (string.IsNullOrEmpty(packageName)) if (string.IsNullOrEmpty(packageName))
throw new System.Exception("Package name is null or empty !"); 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(); _taskQueue.Enqueue(new CollectTask
// {
// // 创建临时测试场景 SavePath = savePath,
// CreateTempScene(); PackageName = packageName,
ProcessMaxNum = processMaxNum,
_steps = ESteps.Prepare; CompletedCallback = completedCallback
});
// 如果当前没有正在执行的任务,开始执行队列
if (_currentTask == null)
{
EditorApplication.update -= EditorUpdate;
EditorApplication.update += EditorUpdate; EditorApplication.update += EditorUpdate;
} }
}
private static void EditorUpdate() private static void EditorUpdate()
{ {
if (_steps == ESteps.None) // 当前没有活动任务时从队列获取新任务
if (_currentTask == null && _taskQueue.Count > 0)
{
_currentTask = _taskQueue.Dequeue();
InitializeTask(_currentTask);
}
if (_currentTask == null)
return; return;
if (_steps == ESteps.Prepare)
if (_currentTask.Steps == ESteps.None)
return;
if (_currentTask.Steps == ESteps.Prepare)
{ {
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection(); ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
_steps = ESteps.CollectAllMaterial; _currentTask.Steps = ESteps.CollectAllMaterial;
return; //等待一帧 return; //等待一帧
} }
if (_steps == ESteps.CollectAllMaterial) if (_currentTask.Steps == ESteps.CollectAllMaterial)
{ {
_allMaterials = GetAllMaterials(); _currentTask.AllMaterials = GetAllMaterials();
_steps = ESteps.CollectVariants; _currentTask.Steps = ESteps.CollectVariants;
return; //等待一帧 return; //等待一帧
} }
if (_steps == ESteps.CollectVariants) if (_currentTask.Steps == ESteps.CollectVariants)
{ {
int count = Mathf.Min(_processMaxNum, _allMaterials.Count); int count = Mathf.Min(_currentTask.ProcessMaxNum, _currentTask.AllMaterials.Count);
List<string> range = _allMaterials.GetRange(0, count); List<string> range = _currentTask.AllMaterials.GetRange(0, count);
_allMaterials.RemoveRange(0, count); _currentTask.AllMaterials.RemoveRange(0, count);
CollectVariants(range); CollectVariants(range);
if (_allMaterials.Count > 0) if ( _currentTask.AllMaterials.Count > 0)
{ {
_elapsedTime = Stopwatch.StartNew(); _currentTask.ElapsedTime = Stopwatch.StartNew();
_steps = ESteps.CollectSleeping; _currentTask.Steps = ESteps.CollectSleeping;
} }
else else
{ {
_elapsedTime = Stopwatch.StartNew(); _currentTask.ElapsedTime = Stopwatch.StartNew();
_steps = ESteps.WaitingDone; _currentTask.Steps = ESteps.WaitingDone;
} }
} }
if (_steps == ESteps.CollectSleeping) if (_currentTask.Steps == ESteps.CollectSleeping)
{ {
if (_elapsedTime.ElapsedMilliseconds > SleepMilliseconds) if (_currentTask.ElapsedTime.ElapsedMilliseconds > SleepMilliseconds)
{ {
DestroyAllSpheres(); DestroyAllSpheres();
_elapsedTime.Stop(); _currentTask.ElapsedTime.Stop();
_steps = ESteps.CollectVariants; _currentTask.Steps = ESteps.CollectVariants;
} }
} }
if (_steps == ESteps.WaitingDone) if ( _currentTask.Steps == ESteps.WaitingDone)
{ {
// 注意:一定要延迟保存才会起效 // 注意:一定要延迟保存才会起效
if (_elapsedTime.ElapsedMilliseconds > WaitMilliseconds) if ( _currentTask.ElapsedTime.ElapsedMilliseconds > WaitMilliseconds)
{ {
_elapsedTime.Stop(); _currentTask.ElapsedTime.Stop();
_steps = ESteps.None; _currentTask.Steps = ESteps.None;
// 保存结果并创建清单 // 保存结果并创建清单
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_savePath); ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_currentTask.SavePath);
CreateManifest(); CreateManifest();
UnityEngine.Debug.Log($"搜集SVC完毕"); UnityEngine.Debug.Log($"搜集SVC完毕");
EditorApplication.update -= EditorUpdate;
_completedCallback?.Invoke(); _currentTask.CompletedCallback?.Invoke();
_currentTask = null;
// 如果队列还有任务,立即触发下一次更新
if (_taskQueue.Count > 0)
EditorApplication.QueuePlayerLoopUpdate();
} }
} }
} }
private static void CreateTempScene() private static void CreateTempScene()
{ {
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects); EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
} }
private static List<string> GetAllMaterials() private static List<string> GetAllMaterials()
{ {
// 获取所有打包的资源 // 获取所有打包的资源
CollectResult collectResult = AssetBundleCollectorSettingData.Setting.BeginCollect(_packageName, false, false); CollectResult collectResult = AssetBundleCollectorSettingData.Setting.BeginCollect(_currentTask.PackageName, false, false);
// 搜集所有材质球 // 搜集所有材质球
int progressValue = 0; int progressValue = 0;
@@ -152,7 +181,8 @@ public static class ShaderVariantCollector
if (result.Contains(assetPath) == false) if (result.Contains(assetPath) == false)
result.Add(assetPath); result.Add(assetPath);
} }
foreach(var dependAssetInfo in collectAssetInfo.DependAssets)
foreach (var dependAssetInfo in collectAssetInfo.DependAssets)
{ {
if (dependAssetInfo.AssetType == typeof(UnityEngine.Material)) if (dependAssetInfo.AssetType == typeof(UnityEngine.Material))
{ {
@@ -161,13 +191,16 @@ public static class ShaderVariantCollector
result.Add(assetPath); result.Add(assetPath);
} }
} }
EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, collectResult.CollectAssets.Count); EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, collectResult.CollectAssets.Count);
} }
EditorTools.ClearProgressBar(); EditorTools.ClearProgressBar();
// 返回结果 // 返回结果
return result.ToList(); return result.ToList();
} }
private static void CollectVariants(List<string> materials) private static void CollectVariants(List<string> materials)
{ {
Camera camera = Camera.main; Camera camera = Camera.main;
@@ -195,7 +228,7 @@ public static class ShaderVariantCollector
var position = new Vector3(x - halfWidth + 1f, y - halfHeight + 1f, 0f); var position = new Vector3(x - halfWidth + 1f, y - halfHeight + 1f, 0f);
var go = CreateSphere(material, position, i); var go = CreateSphere(material, position, i);
if (go != null) if (go != null)
_allSpheres.Add(go); _currentTask.AllSpheres.Add(go);
if (x == xMax) if (x == xMax)
{ {
x = 0; x = 0;
@@ -205,10 +238,13 @@ public static class ShaderVariantCollector
{ {
x++; x++;
} }
EditorTools.DisplayProgressBar("照射所有材质球", ++progressValue, materials.Count); EditorTools.DisplayProgressBar("照射所有材质球", ++progressValue, materials.Count);
} }
EditorTools.ClearProgressBar(); EditorTools.ClearProgressBar();
} }
private static GameObject CreateSphere(string assetPath, Vector3 position, int index) private static GameObject CreateSphere(string assetPath, Vector3 position, int index)
{ {
var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath); var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
@@ -222,30 +258,49 @@ public static class ShaderVariantCollector
go.name = $"Sphere_{index} | {material.name}"; go.name = $"Sphere_{index} | {material.name}";
return go; return go;
} }
private static void DestroyAllSpheres() private static void DestroyAllSpheres()
{ {
foreach (var go in _allSpheres) foreach (var go in _currentTask.AllSpheres)
{ {
GameObject.DestroyImmediate(go); GameObject.DestroyImmediate(go);
} }
_allSpheres.Clear();
_currentTask.AllSpheres.Clear();
// 尝试释放编辑器加载的资源 // 尝试释放编辑器加载的资源
EditorUtility.UnloadUnusedAssetsImmediate(true); EditorUtility.UnloadUnusedAssetsImmediate(true);
} }
private static void CreateManifest() private static void CreateManifest()
{ {
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_savePath); ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_currentTask.SavePath);
if (svc != null) if (svc != null)
{ {
var wrapper = ShaderVariantCollectionManifest.Extract(svc); var wrapper = ShaderVariantCollectionManifest.Extract(svc);
string jsonData = JsonUtility.ToJson(wrapper, true); string jsonData = JsonUtility.ToJson(wrapper, true);
string savePath = _savePath.Replace(".shadervariants", ".json"); string savePath = _currentTask.SavePath.Replace(".shadervariants", ".json");
File.WriteAllText(savePath, jsonData); File.WriteAllText(savePath, jsonData);
} }
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
} }
// 新增任务类
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<string> AllMaterials;
public List<GameObject> AllSpheres = new List<GameObject>();
}
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "com.staryevo.main", "name": "com.staryevo.main",
"version": "1.3.19", "version": "1.3.20",
"displayName": "00.StaryEvo", "displayName": "00.StaryEvo",
"description": "This is an Framework package(后台服务器版本端口9527)", "description": "This is an Framework package(后台服务器版本端口9527)",
"unity": "2021.3", "unity": "2021.3",