parent
423655e1ca
commit
df27e7ba75
|
@ -12,134 +12,150 @@ namespace YooAsset.Editor
|
||||||
{
|
{
|
||||||
public static class ShaderVariantCollector
|
public static class ShaderVariantCollector
|
||||||
{
|
{
|
||||||
|
private enum ESteps
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
Prepare,
|
||||||
|
CollectAllMaterial,
|
||||||
|
CollectVariants,
|
||||||
|
CollectVariantsFinish,
|
||||||
|
WaitingDone,
|
||||||
|
}
|
||||||
|
|
||||||
private const float WaitMilliseconds = 1000f;
|
private const float WaitMilliseconds = 1000f;
|
||||||
private static string _saveFilePath;
|
private static string _savePath;
|
||||||
private static bool _isStarted = false;
|
private static string _packageName;
|
||||||
private static readonly Stopwatch _elapsedTime = new Stopwatch();
|
|
||||||
private static Action _completedCallback;
|
private static Action _completedCallback;
|
||||||
|
|
||||||
private static void EditorUpdate()
|
private static ESteps _steps = ESteps.None;
|
||||||
{
|
private static Stopwatch _elapsedTime;
|
||||||
// 注意:一定要延迟保存才会起效
|
private static List<string> _allMaterials;
|
||||||
if (_isStarted && _elapsedTime.ElapsedMilliseconds > WaitMilliseconds)
|
|
||||||
{
|
|
||||||
_isStarted = false;
|
|
||||||
_elapsedTime.Stop();
|
|
||||||
EditorApplication.update -= EditorUpdate;
|
|
||||||
|
|
||||||
// 保存结果
|
|
||||||
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath);
|
|
||||||
|
|
||||||
// 创建清单
|
|
||||||
CreateManifest();
|
|
||||||
|
|
||||||
Debug.Log($"搜集SVC完毕!");
|
|
||||||
_completedCallback?.Invoke();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 开始收集
|
/// 开始收集
|
||||||
/// </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;
|
return;
|
||||||
|
|
||||||
if (Path.HasExtension(saveFilePath) == false)
|
if (Path.HasExtension(savePath) == false)
|
||||||
saveFilePath = $"{saveFilePath}.shadervariants";
|
savePath = $"{savePath}.shadervariants";
|
||||||
if (Path.GetExtension(saveFilePath) != ".shadervariants")
|
if (Path.GetExtension(savePath) != ".shadervariants")
|
||||||
throw new System.Exception("Shader variant file extension is invalid.");
|
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内容将无法及时刷新
|
// 注意:先删除再保存,否则ShaderVariantCollection内容将无法及时刷新
|
||||||
AssetDatabase.DeleteAsset(ShaderVariantCollectorSettingData.Setting.SavePath);
|
AssetDatabase.DeleteAsset(savePath);
|
||||||
EditorTools.CreateFileDirectory(saveFilePath);
|
EditorTools.CreateFileDirectory(savePath);
|
||||||
_saveFilePath = saveFilePath;
|
_savePath = savePath;
|
||||||
|
_packageName = packageName;
|
||||||
_completedCallback = completedCallback;
|
_completedCallback = completedCallback;
|
||||||
|
|
||||||
// 聚焦到游戏窗口
|
// 聚焦到游戏窗口
|
||||||
EditorTools.FocusUnityGameWindow();
|
EditorTools.FocusUnityGameWindow();
|
||||||
|
|
||||||
// 清空旧数据
|
|
||||||
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
|
|
||||||
|
|
||||||
// 创建临时测试场景
|
// 创建临时测试场景
|
||||||
CreateTempScene();
|
CreateTempScene();
|
||||||
|
|
||||||
// 收集着色器变种
|
_steps = ESteps.Prepare;
|
||||||
var materials = GetAllMaterials();
|
|
||||||
CollectVariants(materials);
|
|
||||||
|
|
||||||
EditorApplication.update += EditorUpdate;
|
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()
|
private static void CreateTempScene()
|
||||||
{
|
{
|
||||||
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
|
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
|
||||||
}
|
}
|
||||||
private static List<Material> GetAllMaterials()
|
private static List<string> GetAllMaterials()
|
||||||
{
|
{
|
||||||
int progressValue = 0;
|
int progressValue = 0;
|
||||||
List<string> allAssets = new List<string>(1000);
|
List<string> allAssets = new List<string>(1000);
|
||||||
|
|
||||||
// 获取所有打包的资源
|
// 获取所有打包的资源
|
||||||
List<CollectAssetInfo> allCollectAssetInfos = new List<CollectAssetInfo>();
|
CollectResult collectResult = AssetBundleCollectorSettingData.Setting.GetPackageAssets(EBuildMode.DryRunBuild, _packageName);
|
||||||
List<CollectResult> collectResults = AssetBundleCollectorSettingData.Setting.GetAllPackageAssets(EBuildMode.DryRunBuild);
|
foreach (var assetInfo in collectResult.CollectAssets)
|
||||||
foreach (var collectResult in collectResults)
|
|
||||||
{
|
{
|
||||||
allCollectAssetInfos.AddRange(collectResult.CollectAssets);
|
string[] depends = AssetDatabase.GetDependencies(assetInfo.AssetPath, true);
|
||||||
}
|
foreach (var dependAsset in depends)
|
||||||
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)
|
if (allAssets.Contains(dependAsset) == false)
|
||||||
allAssets.Add(depend);
|
allAssets.Add(dependAsset);
|
||||||
}
|
}
|
||||||
EditorTools.DisplayProgressBar("获取所有打包资源", ++progressValue, allAssetPath.Count);
|
EditorTools.DisplayProgressBar("获取所有打包资源", ++progressValue, collectResult.CollectAssets.Count);
|
||||||
}
|
}
|
||||||
EditorTools.ClearProgressBar();
|
EditorTools.ClearProgressBar();
|
||||||
|
|
||||||
// 搜集所有材质球
|
// 搜集所有材质球
|
||||||
progressValue = 0;
|
progressValue = 0;
|
||||||
var shaderDic = new Dictionary<Shader, List<Material>>(100);
|
List<string> allMaterial = new List<string>(1000);
|
||||||
foreach (var assetPath in allAssets)
|
foreach (var assetPath in allAssets)
|
||||||
{
|
{
|
||||||
System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
|
System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
|
||||||
if (assetType == typeof(UnityEngine.Material))
|
if (assetType == typeof(UnityEngine.Material))
|
||||||
{
|
{
|
||||||
var material = AssetDatabase.LoadAssetAtPath<Material>(assetPath);
|
allMaterial.Add(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, allAssets.Count);
|
EditorTools.DisplayProgressBar("搜集所有材质球", ++progressValue, allAssets.Count);
|
||||||
}
|
}
|
||||||
EditorTools.ClearProgressBar();
|
EditorTools.ClearProgressBar();
|
||||||
|
|
||||||
// 返回结果
|
// 返回结果
|
||||||
var materials = new List<Material>(1000);
|
return allMaterial;
|
||||||
foreach (var valuePair in shaderDic)
|
|
||||||
{
|
|
||||||
materials.AddRange(valuePair.Value);
|
|
||||||
}
|
|
||||||
return materials;
|
|
||||||
}
|
}
|
||||||
private static void CollectVariants(List<Material> materials)
|
private static void CollectVariants(List<string> materials)
|
||||||
{
|
{
|
||||||
Camera camera = Camera.main;
|
Camera camera = Camera.main;
|
||||||
if (camera == null)
|
if (camera == null)
|
||||||
|
@ -178,23 +194,28 @@ namespace YooAsset.Editor
|
||||||
}
|
}
|
||||||
EditorTools.ClearProgressBar();
|
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);
|
var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||||
go.GetComponent<Renderer>().material = material;
|
go.GetComponent<Renderer>().material = material;
|
||||||
go.transform.position = position;
|
go.transform.position = position;
|
||||||
go.name = $"Sphere_{index}|{material.name}";
|
go.name = $"Sphere_{index} | {material.name}";
|
||||||
}
|
}
|
||||||
private static void CreateManifest()
|
private static void CreateManifest()
|
||||||
{
|
{
|
||||||
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
|
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
|
||||||
|
|
||||||
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_saveFilePath);
|
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_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 = _saveFilePath.Replace(".shadervariants", ".json");
|
string savePath = _savePath.Replace(".shadervariants", ".json");
|
||||||
File.WriteAllText(savePath, jsonData);
|
File.WriteAllText(savePath, jsonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,5 +8,10 @@ namespace YooAsset.Editor
|
||||||
/// 文件存储路径
|
/// 文件存储路径
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string SavePath = "Assets/MyShaderVariants.shadervariants";
|
public string SavePath = "Assets/MyShaderVariants.shadervariants";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 收集的包裹名称
|
||||||
|
/// </summary>
|
||||||
|
public string CollectPackage = string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,10 +18,13 @@ namespace YooAsset.Editor
|
||||||
window.minSize = new Vector2(800, 600);
|
window.minSize = new Vector2(800, 600);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<string> _packageNames;
|
||||||
|
|
||||||
private Button _collectButton;
|
private Button _collectButton;
|
||||||
private TextField _collectOutputField;
|
private TextField _collectOutputField;
|
||||||
private Label _currentShaderCountField;
|
private Label _currentShaderCountField;
|
||||||
private Label _currentVariantCountField;
|
private Label _currentVariantCountField;
|
||||||
|
private PopupField<string> _packageField;
|
||||||
|
|
||||||
public void CreateGUI()
|
public void CreateGUI()
|
||||||
{
|
{
|
||||||
|
@ -36,6 +39,9 @@ namespace YooAsset.Editor
|
||||||
|
|
||||||
visualAsset.CloneTree(root);
|
visualAsset.CloneTree(root);
|
||||||
|
|
||||||
|
// 包裹名称列表
|
||||||
|
_packageNames = GetBuildPackageNames();
|
||||||
|
|
||||||
// 文件输出目录
|
// 文件输出目录
|
||||||
_collectOutputField = root.Q<TextField>("CollectOutput");
|
_collectOutputField = root.Q<TextField>("CollectOutput");
|
||||||
_collectOutputField.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.SavePath);
|
_collectOutputField.SetValueWithoutNotify(ShaderVariantCollectorSettingData.Setting.SavePath);
|
||||||
|
@ -44,14 +50,34 @@ namespace YooAsset.Editor
|
||||||
ShaderVariantCollectorSettingData.Setting.SavePath = _collectOutputField.value;
|
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");
|
_currentShaderCountField = root.Q<Label>("CurrentShaderCount");
|
||||||
_currentVariantCountField = root.Q<Label>("CurrentVariantCount");
|
_currentVariantCountField = root.Q<Label>("CurrentVariantCount");
|
||||||
|
|
||||||
// 变种收集按钮
|
// 变种收集按钮
|
||||||
_collectButton = root.Q<Button>("CollectButton");
|
_collectButton = root.Q<Button>("CollectButton");
|
||||||
_collectButton.clicked += CollectButton_clicked;
|
_collectButton.clicked += CollectButton_clicked;
|
||||||
|
|
||||||
//RefreshWindow();
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -75,7 +101,33 @@ namespace YooAsset.Editor
|
||||||
|
|
||||||
private void CollectButton_clicked()
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;" />
|
<uie:Toolbar name="Toolbar" style="display: flex; flex-direction: row-reverse;" />
|
||||||
<ui:VisualElement name="BuildContainer">
|
<ui:VisualElement name="CollectContainer">
|
||||||
<ui:TextField picking-mode="Ignore" label="文件保存路径" name="CollectOutput" />
|
<ui:TextField picking-mode="Ignore" label="文件保存路径" name="CollectOutput" style="height: 22px;" />
|
||||||
<ui:Label text="Current Shader Count" display-tooltip-when-elided="true" name="CurrentShaderCount" />
|
<ui:VisualElement name="PackageContainer" style="height: 24px;" />
|
||||||
<ui:Label text="Current Variant Count" display-tooltip-when-elided="true" name="CurrentVariantCount" />
|
<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:Button text="开始搜集" display-tooltip-when-elided="true" name="CollectButton" style="height: 50px; background-color: rgb(40, 106, 42); margin-top: 10px;" />
|
||||||
</ui:VisualElement>
|
</ui:VisualElement>
|
||||||
</ui:UXML>
|
</ui:UXML>
|
||||||
|
|
Loading…
Reference in New Issue