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>
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
}

View File

@ -9,7 +9,8 @@ using UnityEditor;
namespace YooAsset.Editor
{
public static class ShaderVariantCollectionReadme
[Serializable]
public class ShaderVariantCollectionManifest
{
[Serializable]
public class ShaderVariantElement
@ -44,52 +45,49 @@ namespace YooAsset.Editor
public List<ShaderVariantElement> ShaderVariantElements = new List<ShaderVariantElement>(1000);
}
[Serializable]
public class ShaderVariantCollectionManifest
/// <summary>
/// Number of shaders in this collection
/// </summary>
public int ShaderTotalCount;
/// <summary>
/// Number of total varians in this collection
/// </summary>
public int VariantTotalCount;
/// <summary>
/// Shader variants info list.
/// </summary>
public List<ShaderVariantInfo> ShaderVariantInfos = new List<ShaderVariantInfo>(1000);
/// <summary>
/// 添加着色器变种信息
/// </summary>
public void AddShaderVariant(string assetPath, string shaderName, PassType passType, string[] keywords)
{
/// <summary>
/// Number of shaders in this collection
/// </summary>
public int ShaderTotalCount;
/// <summary>
/// Number of total varians in this collection
/// </summary>
public int VariantTotalCount;
/// <summary>
/// Shader variants info list.
/// </summary>
public List<ShaderVariantInfo> ShaderVariantInfos = new List<ShaderVariantInfo>(1000);
/// <summary>
/// 添加着色器变种信息
/// </summary>
public void AddShaderVariant(string assetPath, string shaderName, PassType passType, string[] keywords)
var info = GetOrCreateShaderVariantInfo(assetPath, shaderName);
ShaderVariantElement element = new ShaderVariantElement();
element.PassType = passType;
element.Keywords = keywords;
info.ShaderVariantElements.Add(element);
}
private ShaderVariantInfo GetOrCreateShaderVariantInfo(string assetPath, string shaderName)
{
var selectList = ShaderVariantInfos.Where(t => t.ShaderName == shaderName && t.AssetPath == assetPath).ToList();
if (selectList.Count == 0)
{
var info = GetOrCreateShaderVariantInfo(assetPath, shaderName);
ShaderVariantElement element = new ShaderVariantElement();
element.PassType = passType;
element.Keywords = keywords;
info.ShaderVariantElements.Add(element);
ShaderVariantInfo newInfo = new ShaderVariantInfo();
newInfo.AssetPath = assetPath;
newInfo.ShaderName = shaderName;
ShaderVariantInfos.Add(newInfo);
return newInfo;
}
private ShaderVariantInfo GetOrCreateShaderVariantInfo(string assetPath, string shaderName)
{
var selectList = ShaderVariantInfos.Where(t => t.ShaderName == shaderName && t.AssetPath == assetPath).ToList();
if (selectList.Count == 0)
{
ShaderVariantInfo newInfo = new ShaderVariantInfo();
newInfo.AssetPath = assetPath;
newInfo.ShaderName = shaderName;
ShaderVariantInfos.Add(newInfo);
return newInfo;
}
if (selectList.Count != 1)
throw new Exception("Should never get here !");
if (selectList.Count != 1)
throw new Exception("Should never get here !");
return selectList[0];
}
return selectList[0];
}

View File

@ -30,8 +30,8 @@ namespace YooAsset.Editor
// 保存结果
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath);
// 创建说明文件
CreateReadme();
// 创建清单
CreateManifest();
Debug.Log($"搜集SVC完毕");
_completedCallback?.Invoke();
@ -64,7 +64,7 @@ namespace YooAsset.Editor
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
// 创建临时测试场景
CreateTemperScene();
CreateTempScene();
// 收集着色器变种
var materials = GetAllMaterials();
@ -76,9 +76,8 @@ namespace YooAsset.Editor
_elapsedTime.Start();
}
private static void CreateTemperScene()
private static void CreateTempScene()
{
// 创建临时场景
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
}
private static List<Material> GetAllMaterials()
@ -186,17 +185,17 @@ namespace YooAsset.Editor
go.transform.position = position;
go.name = $"Sphere_{index}|{material.name}";
}
private static void CreateReadme()
private static void CreateManifest()
{
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_saveFilePath);
if (svc != null)
{
var wrapper = ShaderVariantCollectionReadme.Extract(svc);
string jsonContents = JsonUtility.ToJson(wrapper, true);
string savePath = _saveFilePath.Replace(".shadervariants", "Manifest.json");
File.WriteAllText(savePath, jsonContents);
var wrapper = ShaderVariantCollectionManifest.Extract(svc);
string jsonData = JsonUtility.ToJson(wrapper, true);
string savePath = _saveFilePath.Replace(".shadervariants", ".json");
File.WriteAllText(savePath, jsonData);
}
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 UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace YooAsset.Editor
{
public class ShaderVariantCollectionWindow : EditorWindow
public class ShaderVariantCollectorWindow : EditorWindow
{
static ShaderVariantCollectionWindow _thisInstance;
[MenuItem("YooAsset/ShaderVariant Collector", false, 201)]
static void ShowWindow()
public static void ShowExample()
{
if (_thisInstance == null)
{
_thisInstance = GetWindow<ShaderVariantCollectionWindow>("着色器变种收集工具");
_thisInstance.minSize = new Vector2(800, 600);
}
_thisInstance.Show();
ShaderVariantCollectorWindow window = GetWindow<ShaderVariantCollectorWindow>("着色器变种收集工具", true, EditorDefine.DockedWindowTypes);
window.minSize = new Vector2(800, 600);
}
private ShaderVariantCollection _selectSVC;
private Button _collectButton;
private TextField _collectOutputField;
private Label _currentShaderCountField;
private Label _currentVariantCountField;
private void OnGUI()
public void CreateGUI()
{
EditorGUILayout.Space();
ShaderVariantCollectorSettingData.Setting.SavePath = EditorGUILayout.TextField("收集文件保存路径", ShaderVariantCollectorSettingData.Setting.SavePath);
int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
EditorGUILayout.LabelField($"CurrentShaderCount : {currentShaderCount}");
EditorGUILayout.LabelField($"CurrentVariantCount : {currentVariantCount}");
// 搜集变种
EditorGUILayout.Space();
if (GUILayout.Button("搜集变种", GUILayout.MaxWidth(80)))
try
{
ShaderVariantCollector.Run(ShaderVariantCollectorSettingData.Setting.SavePath, null);
}
VisualElement root = this.rootVisualElement;
// 查询
EditorGUILayout.Space();
if (GUILayout.Button("查询", GUILayout.MaxWidth(80)))
{
string resultPath = EditorTools.OpenFilePath("Select File", "Assets/", "shadervariants");
if (string.IsNullOrEmpty(resultPath))
// 加载布局文件
var visualAsset = EditorHelper.LoadWindowUXML<ShaderVariantCollectorWindow>();
if (visualAsset == null)
return;
string assetPath = EditorTools.AbsolutePathToAssetPath(resultPath);
_selectSVC = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(assetPath);
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();
}
if (_selectSVC != null)
catch (Exception e)
{
EditorGUILayout.LabelField($"ShaderCount : {_selectSVC.shaderCount}");
EditorGUILayout.LabelField($"VariantCount : {_selectSVC.variantCount}");
Debug.LogError(e.ToString());
}
}
private void OnDestroy()
private void Update()
{
ShaderVariantCollectorSettingData.SaveFile();
if (_currentShaderCountField != null)
{
int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
_currentShaderCountField.text = $"Current Shader Count : {currentShaderCount}";
}
if (_currentVariantCountField != null)
{
int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
_currentVariantCountField.text = $"Current Variant Count : {currentVariantCount}";
}
}
private void CollectButton_clicked()
{
ShaderVariantCollector.Run(ShaderVariantCollectorSettingData.Setting.SavePath, null);
}
}
}
#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}