Update ShaderVariantCollector

pull/51/head
hevinci 2022-11-03 20:17:07 +08:00
parent c37b5e52ac
commit 9397a0ebae
6 changed files with 137 additions and 93 deletions

View File

@ -8,7 +8,14 @@ namespace YooAsset.Editor
/// <summary> /// <summary>
/// 停靠窗口类型集合 /// 停靠窗口类型集合
/// </summary> /// </summary>
public static readonly Type[] DockedWindowTypes = { typeof(AssetBundleBuilderWindow), typeof(AssetBundleCollectorWindow), typeof(AssetBundleDebuggerWindow), typeof(AssetBundleReporterWindow)}; public static readonly Type[] DockedWindowTypes =
{
typeof(AssetBundleBuilderWindow),
typeof(AssetBundleCollectorWindow),
typeof(AssetBundleDebuggerWindow),
typeof(AssetBundleReporterWindow),
typeof(ShaderVariantCollectorWindow)
};
#endif #endif
} }

View File

@ -9,7 +9,8 @@ using UnityEditor;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
public static class ShaderVariantCollectionReadme [Serializable]
public class ShaderVariantCollectionManifest
{ {
[Serializable] [Serializable]
public class ShaderVariantElement public class ShaderVariantElement
@ -44,9 +45,7 @@ namespace YooAsset.Editor
public List<ShaderVariantElement> ShaderVariantElements = new List<ShaderVariantElement>(1000); public List<ShaderVariantElement> ShaderVariantElements = new List<ShaderVariantElement>(1000);
} }
[Serializable]
public class ShaderVariantCollectionManifest
{
/// <summary> /// <summary>
/// Number of shaders in this collection /// Number of shaders in this collection
/// </summary> /// </summary>
@ -90,7 +89,6 @@ namespace YooAsset.Editor
return selectList[0]; return selectList[0];
} }
}
/// <summary> /// <summary>

View File

@ -30,8 +30,8 @@ namespace YooAsset.Editor
// 保存结果 // 保存结果
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath); ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath);
// 创建说明文件 // 创建清单
CreateReadme(); CreateManifest();
Debug.Log($"搜集SVC完毕"); Debug.Log($"搜集SVC完毕");
_completedCallback?.Invoke(); _completedCallback?.Invoke();
@ -64,7 +64,7 @@ namespace YooAsset.Editor
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection(); ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
// 创建临时测试场景 // 创建临时测试场景
CreateTemperScene(); CreateTempScene();
// 收集着色器变种 // 收集着色器变种
var materials = GetAllMaterials(); var materials = GetAllMaterials();
@ -76,9 +76,8 @@ namespace YooAsset.Editor
_elapsedTime.Start(); _elapsedTime.Start();
} }
private static void CreateTemperScene() private static void CreateTempScene()
{ {
// 创建临时场景
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects); EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
} }
private static List<Material> GetAllMaterials() private static List<Material> GetAllMaterials()
@ -186,17 +185,17 @@ namespace YooAsset.Editor
go.transform.position = position; go.transform.position = position;
go.name = $"Sphere_{index}|{material.name}"; go.name = $"Sphere_{index}|{material.name}";
} }
private static void CreateReadme() private static void CreateManifest()
{ {
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_saveFilePath); ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_saveFilePath);
if (svc != null) if (svc != null)
{ {
var wrapper = ShaderVariantCollectionReadme.Extract(svc); var wrapper = ShaderVariantCollectionManifest.Extract(svc);
string jsonContents = JsonUtility.ToJson(wrapper, true); string jsonData = JsonUtility.ToJson(wrapper, true);
string savePath = _saveFilePath.Replace(".shadervariants", "Manifest.json"); string savePath = _saveFilePath.Replace(".shadervariants", ".json");
File.WriteAllText(savePath, jsonContents); File.WriteAllText(savePath, jsonData);
} }
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate); AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);

View File

@ -1,61 +1,82 @@
using UnityEngine; #if UNITY_2019_4_OR_NEWER
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor; using UnityEditor;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
public class ShaderVariantCollectionWindow : EditorWindow public class ShaderVariantCollectorWindow : EditorWindow
{ {
static ShaderVariantCollectionWindow _thisInstance;
[MenuItem("YooAsset/ShaderVariant Collector", false, 201)] [MenuItem("YooAsset/ShaderVariant Collector", false, 201)]
static void ShowWindow() public static void ShowExample()
{ {
if (_thisInstance == null) ShaderVariantCollectorWindow window = GetWindow<ShaderVariantCollectorWindow>("着色器变种收集工具", true, EditorDefine.DockedWindowTypes);
{ window.minSize = new Vector2(800, 600);
_thisInstance = GetWindow<ShaderVariantCollectionWindow>("着色器变种收集工具");
_thisInstance.minSize = new Vector2(800, 600);
}
_thisInstance.Show();
} }
private ShaderVariantCollection _selectSVC; private Button _collectButton;
private TextField _collectOutputField;
private Label _currentShaderCountField;
private Label _currentVariantCountField;
private void OnGUI() public void CreateGUI()
{ {
EditorGUILayout.Space(); try
ShaderVariantCollectorSettingData.Setting.SavePath = EditorGUILayout.TextField("收集文件保存路径", ShaderVariantCollectorSettingData.Setting.SavePath); {
VisualElement root = this.rootVisualElement;
// 加载布局文件
var visualAsset = EditorHelper.LoadWindowUXML<ShaderVariantCollectorWindow>();
if (visualAsset == null)
return;
visualAsset.CloneTree(root);
// 文件输出目录
_collectOutputField = root.Q<TextField>("CollectOutput");
_collectOutputField.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.SavePath);
_collectOutputField.RegisterValueChangedCallback(evt =>
{
ShaderVariantCollectorSettingData.Setting.SavePath = _collectOutputField.value;
});
_currentShaderCountField = root.Q<Label>("CurrentShaderCount");
_currentVariantCountField = root.Q<Label>("CurrentVariantCount");
// 变种收集按钮
_collectButton = root.Q<Button>("CollectButton");
_collectButton.clicked += CollectButton_clicked;
//RefreshWindow();
}
catch (Exception e)
{
Debug.LogError(e.ToString());
}
}
private void Update()
{
if (_currentShaderCountField != null)
{
int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount(); int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount(); _currentShaderCountField.text = $"Current Shader Count : {currentShaderCount}";
EditorGUILayout.LabelField($"CurrentShaderCount : {currentShaderCount}"); }
EditorGUILayout.LabelField($"CurrentVariantCount : {currentVariantCount}");
// 搜集变种 if (_currentVariantCountField != null)
EditorGUILayout.Space(); {
if (GUILayout.Button("搜集变种", GUILayout.MaxWidth(80))) int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
_currentVariantCountField.text = $"Current Variant Count : {currentVariantCount}";
}
}
private void CollectButton_clicked()
{ {
ShaderVariantCollector.Run(ShaderVariantCollectorSettingData.Setting.SavePath, null); ShaderVariantCollector.Run(ShaderVariantCollectorSettingData.Setting.SavePath, null);
} }
// 查询
EditorGUILayout.Space();
if (GUILayout.Button("查询", GUILayout.MaxWidth(80)))
{
string resultPath = EditorTools.OpenFilePath("Select File", "Assets/", "shadervariants");
if (string.IsNullOrEmpty(resultPath))
return;
string assetPath = EditorTools.AbsolutePathToAssetPath(resultPath);
_selectSVC = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(assetPath);
}
if (_selectSVC != null)
{
EditorGUILayout.LabelField($"ShaderCount : {_selectSVC.shaderCount}");
EditorGUILayout.LabelField($"VariantCount : {_selectSVC.variantCount}");
}
}
private void OnDestroy()
{
ShaderVariantCollectorSettingData.SaveFile();
}
} }
} }
#endif

View File

@ -0,0 +1,9 @@
<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">
<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: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>

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 9bff4878063eaf04dab8713e1e662ac5
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}