mirror of https://github.com/tuyoogame/YooAsset
Update ShaderVariantCollector
parent
706e8601a3
commit
188957ee91
|
@ -10,103 +10,31 @@ namespace YooAsset.Editor
|
|||
{
|
||||
public static class ShaderVariantCollectionHelper
|
||||
{
|
||||
[Serializable]
|
||||
public class ShaderVariantWrapper
|
||||
public static void ClearCurrentShaderVariantCollection()
|
||||
{
|
||||
/// <summary>
|
||||
/// Shader asset path in editor.
|
||||
/// </summary>
|
||||
public string AssetPath;
|
||||
|
||||
/// <summary>
|
||||
/// Shader name.
|
||||
/// </summary>
|
||||
public string ShaderName;
|
||||
|
||||
/// <summary>
|
||||
/// Pass type to use in this variant.
|
||||
/// </summary>
|
||||
public PassType PassType;
|
||||
|
||||
/// <summary>
|
||||
/// Array of shader keywords to use in this variant.
|
||||
/// </summary>
|
||||
public string[] Keywords;
|
||||
|
||||
public ShaderVariantWrapper(string assetPath, string shaderName, PassType passType, params string[] keywords)
|
||||
{
|
||||
AssetPath = assetPath;
|
||||
ShaderName = shaderName;
|
||||
PassType = passType;
|
||||
Keywords = keywords;
|
||||
}
|
||||
EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "ClearCurrentShaderVariantCollection");
|
||||
}
|
||||
public static void SaveCurrentShaderVariantCollection(string savePath)
|
||||
{
|
||||
EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "SaveCurrentShaderVariantCollection", savePath);
|
||||
}
|
||||
public static int GetCurrentShaderVariantCollectionShaderCount()
|
||||
{
|
||||
return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionShaderCount");
|
||||
}
|
||||
public static int GetCurrentShaderVariantCollectionVariantCount()
|
||||
{
|
||||
return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionVariantCount");
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class ShaderVariantCollectionWrapper
|
||||
/// <summary>
|
||||
/// 获取着色器的变种总数量
|
||||
/// </summary>
|
||||
public static string GetShaderVariantCount(string assetPath)
|
||||
{
|
||||
/// <summary>
|
||||
/// Number of shaders in this collection
|
||||
/// </summary>
|
||||
public int ShaderCount;
|
||||
|
||||
/// <summary>
|
||||
/// Number of total varians in this collection
|
||||
/// </summary>
|
||||
public int VariantCount;
|
||||
|
||||
/// <summary>
|
||||
/// Shader variants list.
|
||||
/// </summary>
|
||||
public List<ShaderVariantWrapper> ShaderVariants = new List<ShaderVariantWrapper>(1000);
|
||||
|
||||
public void Add(ShaderVariantWrapper variant)
|
||||
{
|
||||
ShaderVariants.Add(variant);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ShaderVariantCollectionWrapper Extract(ShaderVariantCollection svc)
|
||||
{
|
||||
var result = new ShaderVariantCollectionWrapper();
|
||||
using (var so = new SerializedObject(svc))
|
||||
{
|
||||
var shaderArray = so.FindProperty("m_Shaders.Array");
|
||||
if (shaderArray != null && shaderArray.isArray)
|
||||
{
|
||||
for (int i = 0; i < shaderArray.arraySize; ++i)
|
||||
{
|
||||
var shaderRef = shaderArray.FindPropertyRelative($"data[{i}].first");
|
||||
var shaderVariantsArray = shaderArray.FindPropertyRelative($"data[{i}].second.variants");
|
||||
if (shaderRef != null && shaderRef.propertyType == SerializedPropertyType.ObjectReference && shaderVariantsArray != null && shaderVariantsArray.isArray)
|
||||
{
|
||||
var shader = shaderRef.objectReferenceValue as Shader;
|
||||
if (shader == null)
|
||||
{
|
||||
throw new Exception("Invalid shader in ShaderVariantCollection file.");
|
||||
}
|
||||
|
||||
string shaderAssetPath = AssetDatabase.GetAssetPath(shader);
|
||||
string shaderName = shader.name;
|
||||
|
||||
// 添加变种信息
|
||||
for (int j = 0; j < shaderVariantsArray.arraySize; ++j)
|
||||
{
|
||||
var propKeywords = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].keywords");
|
||||
var propPassType = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].passType");
|
||||
if (propKeywords != null && propPassType != null && propKeywords.propertyType == SerializedPropertyType.String)
|
||||
{
|
||||
string[] keywords = propKeywords.stringValue.Split(' ');
|
||||
PassType pathType = (PassType)propPassType.intValue;
|
||||
result.Add(new ShaderVariantWrapper(shaderAssetPath, shaderName, pathType, keywords));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
Shader shader = AssetDatabase.LoadAssetAtPath<Shader>(assetPath);
|
||||
var variantCount = EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetVariantCount", shader, true);
|
||||
return variantCount.ToString();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEditor;
|
||||
|
||||
namespace YooAsset.Editor
|
||||
{
|
||||
public static class ShaderVariantCollectionReadme
|
||||
{
|
||||
[Serializable]
|
||||
public class ShaderVariantElement
|
||||
{
|
||||
/// <summary>
|
||||
/// Pass type to use in this variant.
|
||||
/// </summary>
|
||||
public PassType PassType;
|
||||
|
||||
/// <summary>
|
||||
/// Array of shader keywords to use in this variant.
|
||||
/// </summary>
|
||||
public string[] Keywords;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class ShaderVariantInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Shader asset path in editor.
|
||||
/// </summary>
|
||||
public string AssetPath;
|
||||
|
||||
/// <summary>
|
||||
/// Shader name.
|
||||
/// </summary>
|
||||
public string ShaderName;
|
||||
|
||||
/// <summary>
|
||||
/// Shader variants elements list.
|
||||
/// </summary>
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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 !");
|
||||
|
||||
return selectList[0];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解析SVC文件并将数据写入到清单
|
||||
/// </summary>
|
||||
public static ShaderVariantCollectionManifest Extract(ShaderVariantCollection svc)
|
||||
{
|
||||
var manifest = new ShaderVariantCollectionManifest();
|
||||
manifest.ShaderTotalCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
|
||||
manifest.VariantTotalCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
|
||||
|
||||
using (var so = new SerializedObject(svc))
|
||||
{
|
||||
var shaderArray = so.FindProperty("m_Shaders.Array");
|
||||
if (shaderArray != null && shaderArray.isArray)
|
||||
{
|
||||
for (int i = 0; i < shaderArray.arraySize; ++i)
|
||||
{
|
||||
var shaderRef = shaderArray.FindPropertyRelative($"data[{i}].first");
|
||||
var shaderVariantsArray = shaderArray.FindPropertyRelative($"data[{i}].second.variants");
|
||||
if (shaderRef != null && shaderRef.propertyType == SerializedPropertyType.ObjectReference && shaderVariantsArray != null && shaderVariantsArray.isArray)
|
||||
{
|
||||
var shader = shaderRef.objectReferenceValue as Shader;
|
||||
if (shader == null)
|
||||
{
|
||||
throw new Exception("Invalid shader in ShaderVariantCollection file.");
|
||||
}
|
||||
|
||||
string shaderAssetPath = AssetDatabase.GetAssetPath(shader);
|
||||
string shaderName = shader.name;
|
||||
|
||||
// 添加变种信息
|
||||
for (int j = 0; j < shaderVariantsArray.arraySize; ++j)
|
||||
{
|
||||
var propKeywords = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].keywords");
|
||||
var propPassType = shaderVariantsArray.FindPropertyRelative($"Array.data[{j}].passType");
|
||||
if (propKeywords != null && propPassType != null && propKeywords.propertyType == SerializedPropertyType.String)
|
||||
{
|
||||
string[] keywords = propKeywords.stringValue.Split(' ');
|
||||
PassType pathType = (PassType)propPassType.intValue;
|
||||
manifest.AddShaderVariant(shaderAssetPath, shaderName, pathType, keywords);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return manifest;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 97098b04691f5c046ac4829f1d72f425
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -28,7 +28,7 @@ namespace YooAsset.Editor
|
|||
EditorApplication.update -= EditorUpdate;
|
||||
|
||||
// 保存结果
|
||||
SaveCurrentShaderVariantCollection();
|
||||
ShaderVariantCollectionHelper.SaveCurrentShaderVariantCollection(_saveFilePath);
|
||||
|
||||
// 创建说明文件
|
||||
CreateReadme();
|
||||
|
@ -61,7 +61,7 @@ namespace YooAsset.Editor
|
|||
EditorTools.FocusUnityGameWindow();
|
||||
|
||||
// 清空旧数据
|
||||
ClearCurrentShaderVariantCollection();
|
||||
ShaderVariantCollectionHelper.ClearCurrentShaderVariantCollection();
|
||||
|
||||
// 创建临时测试场景
|
||||
CreateTemperScene();
|
||||
|
@ -181,7 +181,6 @@ namespace YooAsset.Editor
|
|||
go.transform.position = position;
|
||||
go.name = $"Sphere_{index}|{material.name}";
|
||||
}
|
||||
|
||||
private static void CreateReadme()
|
||||
{
|
||||
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
|
||||
|
@ -189,29 +188,13 @@ namespace YooAsset.Editor
|
|||
ShaderVariantCollection svc = AssetDatabase.LoadAssetAtPath<ShaderVariantCollection>(_saveFilePath);
|
||||
if (svc != null)
|
||||
{
|
||||
var wrapper = ShaderVariantCollectionHelper.Extract(svc);
|
||||
var wrapper = ShaderVariantCollectionReadme.Extract(svc);
|
||||
string jsonContents = JsonUtility.ToJson(wrapper, true);
|
||||
string savePath = _saveFilePath.Replace(".shadervariants", "_Readme.json");
|
||||
string savePath = _saveFilePath.Replace(".shadervariants", "Manifest.json");
|
||||
File.WriteAllText(savePath, jsonContents);
|
||||
}
|
||||
|
||||
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
private static void ClearCurrentShaderVariantCollection()
|
||||
{
|
||||
EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "ClearCurrentShaderVariantCollection");
|
||||
}
|
||||
private static void SaveCurrentShaderVariantCollection()
|
||||
{
|
||||
EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "SaveCurrentShaderVariantCollection", _saveFilePath);
|
||||
}
|
||||
public static int GetCurrentShaderVariantCollectionShaderCount()
|
||||
{
|
||||
return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionShaderCount");
|
||||
}
|
||||
public static int GetCurrentShaderVariantCollectionVariantCount()
|
||||
{
|
||||
return (int)EditorTools.InvokeNonPublicStaticMethod(typeof(ShaderUtil), "GetCurrentShaderVariantCollectionVariantCount");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -25,8 +25,8 @@ namespace YooAsset.Editor
|
|||
EditorGUILayout.Space();
|
||||
ShaderVariantCollectorSettingData.Setting.SavePath = EditorGUILayout.TextField("收集文件保存路径", ShaderVariantCollectorSettingData.Setting.SavePath);
|
||||
|
||||
int currentShaderCount = ShaderVariantCollector.GetCurrentShaderVariantCollectionShaderCount();
|
||||
int currentVariantCount = ShaderVariantCollector.GetCurrentShaderVariantCollectionVariantCount();
|
||||
int currentShaderCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionShaderCount();
|
||||
int currentVariantCount = ShaderVariantCollectionHelper.GetCurrentShaderVariantCollectionVariantCount();
|
||||
EditorGUILayout.LabelField($"CurrentShaderCount : {currentShaderCount}");
|
||||
EditorGUILayout.LabelField($"CurrentVariantCount : {currentVariantCount}");
|
||||
|
||||
|
|
Loading…
Reference in New Issue