【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(
$"UnityEvo:【{packageName}】VariantCount : {collection.variantCount}");
string groupname = "ShaderVariants";
AssetBundleCollectorGroup collectorGroup = null;
if (collectorGroupDic.ContainsKey(groupname))
{
collectorGroupDic[groupname].Collectors.Clear();
collectorGroup = collectorGroupDic[groupname];
CreateShaderVariantsGroup(packageName, localSavePath);
}
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)
{
if (package.PackageName == packageName)
{
collectorGroup =
YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(package, "ShaderVariants");
collectorGroup = YooAsset.Editor.AssetBundleCollectorSettingData.CreateGroup(package, groupname);
break;
}
}
}
var guid = AssetDatabase.AssetPathToGUID(localSavePath);
AssetBundleCollector collector = new AssetBundleCollector()
{
@@ -526,15 +532,9 @@ namespace Stary.Evo.Editor
FilterRuleName = nameof(CollectShaderVariants),
AssetTags = groupname,
};
YooAsset.Editor.AssetBundleCollectorSettingData.CreateCollector(collectorGroup, collector);
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 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<string> _allMaterials;
// private static List<GameObject> _allSpheres = new List<GameObject>(1000);
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 Queue<CollectTask> _taskQueue = new Queue<CollectTask>();
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;
}
/// <summary>
/// 开始收集
/// </summary>
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();
_steps = ESteps.Prepare;
// 将新任务加入队列
_taskQueue.Enqueue(new CollectTask
{
SavePath = savePath,
PackageName = packageName,
ProcessMaxNum = processMaxNum,
CompletedCallback = completedCallback
});
// 如果当前没有正在执行的任务,开始执行队列
if (_currentTask == null)
{
EditorApplication.update -= EditorUpdate;
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<string> range = _allMaterials.GetRange(0, count);
_allMaterials.RemoveRange(0, count);
int count = Mathf.Min(_currentTask.ProcessMaxNum, _currentTask.AllMaterials.Count);
List<string> 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<string> 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<string> 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<Material>(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<ShaderVariantCollection>(_savePath);
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_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);
}
// 新增任务类
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",
"version": "1.3.19",
"version": "1.3.20",
"displayName": "00.StaryEvo",
"description": "This is an Framework package(后台服务器版本端口9527)",
"unity": "2021.3",