update shader variant collector

优化着色器变种收集代码
pull/62/head
hevinci 2023-02-01 18:59:47 +08:00
parent 423655e1ca
commit df27e7ba75
4 changed files with 165 additions and 86 deletions

View File

@ -12,134 +12,150 @@ namespace YooAsset.Editor
{
public static class ShaderVariantCollector
{
private enum ESteps
{
None,
Prepare,
CollectAllMaterial,
CollectVariants,
CollectVariantsFinish,
WaitingDone,
}
private const float WaitMilliseconds = 1000f;
private static string _saveFilePath;
private static bool _isStarted = false;
private static readonly Stopwatch _elapsedTime = new Stopwatch();
private static string _savePath;
private static string _packageName;
private static Action _completedCallback;
private static void EditorUpdate()
{
// 注意:一定要延迟保存才会起效
if (_isStarted && _elapsedTime.ElapsedMilliseconds > WaitMilliseconds)
{
_isStarted = false;
_elapsedTime.Stop();
EditorApplication.update -= EditorUpdate;
private static ESteps _steps = ESteps.None;
private static Stopwatch _elapsedTime;
private static List<string> _allMaterials;
// 保存结果
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath);
// 创建清单
CreateManifest();
Debug.Log($"搜集SVC完毕");
_completedCallback?.Invoke();
}
}
/// <summary>
/// 开始收集
/// </summary>
public static void Run(string saveFilePath, Action completedCallback)
public static void Run(string savePath, string packageName, Action completedCallback)
{
if (_isStarted)
if (_steps != ESteps.None)
return;
if (Path.HasExtension(saveFilePath) == false)
saveFilePath = $"{saveFilePath}.shadervariants";
if (Path.GetExtension(saveFilePath) != ".shadervariants")
if (Path.HasExtension(savePath) == false)
savePath = $"{savePath}.shadervariants";
if (Path.GetExtension(savePath) != ".shadervariants")
throw new System.Exception("Shader variant file extension is invalid.");
if (string.IsNullOrEmpty(packageName))
throw new System.Exception("Package name is null or empty !");
// 注意先删除再保存否则ShaderVariantCollection内容将无法及时刷新
AssetDatabase.DeleteAsset(ShaderVariantCollectorSettingData.Setting.SavePath);
EditorTools.CreateFileDirectory(saveFilePath);
_saveFilePath = saveFilePath;
AssetDatabase.DeleteAsset(savePath);
EditorTools.CreateFileDirectory(savePath);
_savePath = savePath;
_packageName = packageName;
_completedCallback = completedCallback;
// 聚焦到游戏窗口
EditorTools.FocusUnityGameWindow();
// 清空旧数据
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
// 创建临时测试场景
CreateTempScene();
// 收集着色器变种
var materials = GetAllMaterials();
CollectVariants(materials);
_steps = ESteps.Prepare;
EditorApplication.update += EditorUpdate;
_isStarted = true;
_elapsedTime.Reset();
_elapsedTime.Start();
}
private static void EditorUpdate()
{
if (_steps == ESteps.None)
return;
if (_steps == ESteps.Prepare)
{
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
_steps = ESteps.CollectAllMaterial;
return;
}
if (_steps == ESteps.CollectAllMaterial)
{
_allMaterials = GetAllMaterials();
_steps = ESteps.CollectVariants;
return;
}
if (_steps == ESteps.CollectVariants)
{
CollectVariants(_allMaterials);
_steps = ESteps.CollectVariantsFinish;
return;
}
if (_steps == ESteps.CollectVariantsFinish)
{
_elapsedTime = Stopwatch.StartNew();
_steps = ESteps.WaitingDone;
return;
}
if (_steps == ESteps.WaitingDone)
{
// 注意:一定要延迟保存才会起效
if (_elapsedTime.ElapsedMilliseconds > WaitMilliseconds)
{
_elapsedTime.Stop();
_steps = ESteps.None;
// 保存结果并创建清单
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_savePath);
CreateManifest();
Debug.Log($"搜集SVC完毕");
EditorApplication.update -= EditorUpdate;
_completedCallback?.Invoke();
}
}
}
private static void CreateTempScene()
{
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
}
private static List<Material> GetAllMaterials()
private static List<string> GetAllMaterials()
{
int progressValue = 0;
List<string> allAssets = new List<string>(1000);
// 获取所有打包的资源
List<CollectAssetInfo> allCollectAssetInfos = new List<CollectAssetInfo>();
List<CollectResult> collectResults = AssetBundleCollectorSettingData.Setting.GetAllPackageAssets(EBuildMode.DryRunBuild);
foreach (var collectResult in collectResults)
CollectResult collectResult = AssetBundleCollectorSettingData.Setting.GetPackageAssets(EBuildMode.DryRunBuild, _packageName);
foreach (var assetInfo in collectResult.CollectAssets)
{
allCollectAssetInfos.AddRange(collectResult.CollectAssets);
string[] depends = AssetDatabase.GetDependencies(assetInfo.AssetPath, true);
foreach (var dependAsset in depends)
{
if (allAssets.Contains(dependAsset) == false)
allAssets.Add(dependAsset);
}
List<string> allAssetPath = allCollectAssetInfos.Select(t => t.AssetPath).ToList();
foreach (var assetPath in allAssetPath)
{
string[] depends = AssetDatabase.GetDependencies(assetPath, true);
foreach (var depend in depends)
{
if (allAssets.Contains(depend) == false)
allAssets.Add(depend);
}
EditorTools.DisplayProgressBar("获取所有打包资源", ++progressValue, allAssetPath.Count);
EditorTools.DisplayProgressBar("获取所有打包资源", ++progressValue, collectResult.CollectAssets.Count);
}
EditorTools.ClearProgressBar();
// 搜集所有材质球
progressValue = 0;
var shaderDic = new Dictionary<Shader, List<Material>>(100);
List<string> allMaterial = new List<string>(1000);
foreach (var assetPath in allAssets)
{
System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
if (assetType == typeof(UnityEngine.Material))
{
var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
var shader = material.shader;
if (shader == null)
continue;
if (shaderDic.ContainsKey(shader) == false)
{
shaderDic.Add(shader, new List<Material>());
}
if (shaderDic[shader].Contains(material) == false)
{
shaderDic[shader].Add(material);
}
allMaterial.Add(assetPath);
}
EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, allAssets.Count);
}
EditorTools.ClearProgressBar();
// 返回结果
var materials = new List<Material>(1000);
foreach (var valuePair in shaderDic)
{
materials.AddRange(valuePair.Value);
return allMaterial;
}
return materials;
}
private static void CollectVariants(List<Material> materials)
private static void CollectVariants(List<string> materials)
{
Camera camera = Camera.main;
if (camera == null)
@ -178,23 +194,28 @@ namespace YooAsset.Editor
}
EditorTools.ClearProgressBar();
}
private static void CreateSphere(Material material, Vector3 position, int index)
private static void CreateSphere(string assetPath, Vector3 position, int index)
{
var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
var shader = material.shader;
if (shader == null)
return;
var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go.GetComponent<Renderer>().material = material;
go.transform.position = position;
go.name = $"Sphere_{index}|{material.name}";
go.name = $"Sphere_{index} | {material.name}";
}
private static void CreateManifest()
{
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_saveFilePath);
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_savePath);
if (svc != null)
{
var wrapper = ShaderVariantCollectionManifest.Extract(svc);
string jsonData = JsonUtility.ToJson(wrapper, true);
string savePath = _saveFilePath.Replace(".shadervariants", ".json");
string savePath = _savePath.Replace(".shadervariants", ".json");
File.WriteAllText(savePath, jsonData);
}

View File

@ -8,5 +8,10 @@ namespace YooAsset.Editor
/// 文件存储路径
/// </summary>
public string SavePath = "Assets/MyShaderVariants.shadervariants";
/// <summary>
/// 收集的包裹名称
/// </summary>
public string CollectPackage = string.Empty;
}
}

View File

@ -18,10 +18,13 @@ namespace YooAsset.Editor
window.minSize = new Vector2(800, 600);
}
private List<string> _packageNames;
private Button _collectButton;
private TextField _collectOutputField;
private Label _currentShaderCountField;
private Label _currentVariantCountField;
private PopupField<string> _packageField;
public void CreateGUI()
{
@ -36,6 +39,9 @@ namespace YooAsset.Editor
visualAsset.CloneTree(root);
// 包裹名称列表
_packageNames = GetBuildPackageNames();
// 文件输出目录
_collectOutputField = root.Q<TextField>("CollectOutput");
_collectOutputField.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.SavePath);
@ -44,14 +50,34 @@ namespace YooAsset.Editor
ShaderVariantCollectorSettingData.Setting.SavePath = _collectOutputField.value;
});
// 收集的包裹
var packageContainer = root.Q("PackageContainer");
if (_packageNames.Count > 0)
{
int defaultIndex = GetDefaultPackageIndex(ShaderVariantCollectorSettingData.Setting.CollectPackage);
_packageField = new PopupField<string>(_packageNames, defaultIndex);
_packageField.label = "Package";
_packageField.style.width = 350;
_packageField.RegisterValueChangedCallback(evt =>
{
ShaderVariantCollectorSettingData.Setting.CollectPackage = _packageField.value;
});
packageContainer.Add(_packageField);
}
else
{
_packageField = new PopupField<string>();
_packageField.label = "Package";
_packageField.style.width = 350;
packageContainer.Add(_packageField);
}
_currentShaderCountField = root.Q<Label>("CurrentShaderCount");
_currentVariantCountField = root.Q<Label>("CurrentVariantCount");
// 变种收集按钮
_collectButton = root.Q<Button>("CollectButton");
_collectButton.clicked += CollectButton_clicked;
//RefreshWindow();
}
catch (Exception e)
{
@ -75,7 +101,33 @@ namespace YooAsset.Editor
private void CollectButton_clicked()
{
ShaderVariantCollector.Run(ShaderVariantCollectorSettingData.Setting.SavePath, null);
string savePath = ShaderVariantCollectorSettingData.Setting.SavePath;
string packageName = ShaderVariantCollectorSettingData.Setting.CollectPackage;
ShaderVariantCollector.Run(savePath, packageName, null);
}
// 构建包裹相关
private int GetDefaultPackageIndex(string packageName)
{
for (int index = 0; index < _packageNames.Count; index++)
{
if (_packageNames[index] == packageName)
{
return index;
}
}
ShaderVariantCollectorSettingData.Setting.CollectPackage = _packageNames[0];
return 0;
}
private List<string> GetBuildPackageNames()
{
List<string> result = new List<string>();
foreach (var package in AssetBundleCollectorSettingData.Setting.Packages)
{
result.Add(package.PackageName);
}
return result;
}
}
}

View File

@ -1,9 +1,10 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
<uie:Toolbar name="Toolbar" style="display: flex; flex-direction: row-reverse;" />
<ui:VisualElement name="BuildContainer">
<ui:TextField picking-mode="Ignore" label="文件保存路径" name="CollectOutput" />
<ui:Label text="Current Shader Count" display-tooltip-when-elided="true" name="CurrentShaderCount" />
<ui:Label text="Current Variant Count" display-tooltip-when-elided="true" name="CurrentVariantCount" />
<ui:VisualElement name="CollectContainer">
<ui:TextField picking-mode="Ignore" label="文件保存路径" name="CollectOutput" style="height: 22px;" />
<ui:VisualElement name="PackageContainer" style="height: 24px;" />
<ui:Label text="Current Shader Count" display-tooltip-when-elided="true" name="CurrentShaderCount" style="height: 20px; padding-left: 4px;" />
<ui:Label text="Current Variant Count" display-tooltip-when-elided="true" name="CurrentVariantCount" style="height: 20px; padding-left: 4px;" />
<ui:Button text="开始搜集" display-tooltip-when-elided="true" name="CollectButton" style="height: 50px; background-color: rgb(40, 106, 42); margin-top: 10px;" />
</ui:VisualElement>
</ui:UXML>