Update AssetBundleCollector

Remove files
pull/4/head
hevinci 2022-04-02 15:12:51 +08:00
parent 9de27e790d
commit 1278d76f49
19 changed files with 0 additions and 1162 deletions

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 0dbd5f557007d574e80c42d4a15421a9
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,68 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset.Editor
{
public class AssetBundleCollectorSetting : ScriptableObject
{
[Serializable]
public class Collector
{
/// <summary>
/// 收集的文件夹路径
/// </summary>
public string CollectDirectory = string.Empty;
/// <summary>
/// 打包规则类名
/// </summary>
public string PackRuleName = string.Empty;
/// <summary>
/// 过滤规则类名
/// </summary>
public string FilterRuleName = string.Empty;
/// <summary>
/// 不写入资源路径到清单文件
/// 注意:对于不依赖于代码加载的收集资源,可以禁止写入资源路径信息到清单文件
/// </summary>
public bool DontWriteAssetPath = false;
/// <summary>
/// 资源标记
/// </summary>
public string AssetTags = string.Empty;
/// <summary>
/// 获取资源标记列表
/// </summary>
public List<string> GetAssetTags()
{
return StringUtility.StringToStringList(AssetTags, ';');
}
public override string ToString()
{
return $"Directory : {CollectDirectory} | {PackRuleName} | {FilterRuleName} | {DontWriteAssetPath} | {AssetTags}";
}
}
/// <summary>
/// 自动收集着色器
/// </summary>
public bool AutoCollectShaders = false;
/// <summary>
/// 自动收集的着色器资源包名
/// </summary>
public string ShadersBundleName = "myshaders";
/// <summary>
/// 收集列表
/// </summary>
public List<Collector> Collectors = new List<Collector>();
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 4cc54f0173b27fb40aee29e7eb3364da
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,477 +0,0 @@
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
namespace YooAsset.Editor
{
public static class AssetBundleCollectorSettingData
{
private static readonly Dictionary<string, System.Type> _cachePackRuleTypes = new Dictionary<string, System.Type>();
private static readonly Dictionary<string, IPackRule> _cachePackRuleInstance = new Dictionary<string, IPackRule>();
private static readonly Dictionary<string, System.Type> _cacheFilterRuleTypes = new Dictionary<string, System.Type>();
private static readonly Dictionary<string, IFilterRule> _cacheFilterRuleInstance = new Dictionary<string, IFilterRule>();
private static AssetBundleCollectorSetting _setting = null;
public static AssetBundleCollectorSetting Setting
{
get
{
if (_setting == null)
LoadSettingData();
return _setting;
}
}
public static List<string> GetPackRuleNames()
{
if (_setting == null)
LoadSettingData();
List<string> names = new List<string>();
foreach (var pair in _cachePackRuleTypes)
{
names.Add(pair.Key);
}
return names;
}
public static List<string> GetFilterRuleNames()
{
if (_setting == null)
LoadSettingData();
List<string> names = new List<string>();
foreach (var pair in _cacheFilterRuleTypes)
{
names.Add(pair.Key);
}
return names;
}
public static bool HasPackRuleName(string ruleName)
{
foreach (var pair in _cachePackRuleTypes)
{
if (pair.Key == ruleName)
return true;
}
return false;
}
public static bool HasFilterRuleName(string ruleName)
{
foreach (var pair in _cacheFilterRuleTypes)
{
if (pair.Key == ruleName)
return true;
}
return false;
}
/// <summary>
/// 加载配置文件
/// </summary>
private static void LoadSettingData()
{
// 加载配置文件
_setting = AssetDatabase.LoadAssetAtPath<AssetBundleCollectorSetting>(EditorDefine.AssetBundleCollectorSettingFilePath);
if (_setting == null)
{
Debug.LogWarning($"Create new {nameof(AssetBundleCollectorSetting)}.asset : {EditorDefine.AssetBundleCollectorSettingFilePath}");
_setting = ScriptableObject.CreateInstance<AssetBundleCollectorSetting>();
EditorTools.CreateFileDirectory(EditorDefine.AssetBundleCollectorSettingFilePath);
AssetDatabase.CreateAsset(Setting, EditorDefine.AssetBundleCollectorSettingFilePath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
else
{
Debug.Log($"Load {nameof(AssetBundleCollectorSetting)}.asset ok");
}
// IPackRule
{
// 清空缓存集合
_cachePackRuleTypes.Clear();
_cachePackRuleInstance.Clear();
// 获取所有类型
List<Type> types = new List<Type>(100)
{
typeof(PackExplicit),
typeof(PackDirectory),
typeof(PackRawFile),
};
var customTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IPackRule));
types.AddRange(customTypes);
for (int i = 0; i < types.Count; i++)
{
Type type = types[i];
if (_cachePackRuleTypes.ContainsKey(type.Name) == false)
_cachePackRuleTypes.Add(type.Name, type);
}
}
// IFilterRule
{
// 清空缓存集合
_cacheFilterRuleTypes.Clear();
_cacheFilterRuleInstance.Clear();
// 获取所有类型
List<Type> types = new List<Type>(100)
{
typeof(CollectAll),
typeof(CollectScene),
typeof(CollectPrefab),
typeof(CollectSprite)
};
var customTypes = AssemblyUtility.GetAssignableTypes(AssemblyUtility.UnityDefaultAssemblyEditorName, typeof(IFilterRule));
types.AddRange(customTypes);
for (int i = 0; i < types.Count; i++)
{
Type type = types[i];
if (_cacheFilterRuleTypes.ContainsKey(type.Name) == false)
_cacheFilterRuleTypes.Add(type.Name, type);
}
}
}
/// <summary>
/// 存储文件
/// </summary>
public static void SaveFile()
{
if (Setting != null)
{
EditorUtility.SetDirty(Setting);
AssetDatabase.SaveAssets();
}
}
// 着色器相关
public static void ModifyShader(bool isCollectAllShaders, string shadersBundleName)
{
if (string.IsNullOrEmpty(shadersBundleName))
return;
Setting.AutoCollectShaders = isCollectAllShaders;
Setting.ShadersBundleName = shadersBundleName;
SaveFile();
}
// 收集器相关
public static void ClearAllCollector()
{
Setting.Collectors.Clear();
SaveFile();
}
public static void AddCollector(string directory, string packRuleName, string filterRuleName, bool dontWriteAssetPath, string assetTags, bool saveFile = true)
{
// 末尾添加路径分隔符号
if (directory.EndsWith("/") == false)
directory = $"{directory}/";
// 检测收集器路径冲突
if (CheckConflict(directory))
return;
// 检测资源标签
if (dontWriteAssetPath && string.IsNullOrEmpty(assetTags) == false)
Debug.LogWarning($"Collector {directory} has asset tags : {assetTags}, It is not vliad when enable dontWriteAssetPath.");
AssetBundleCollectorSetting.Collector element = new AssetBundleCollectorSetting.Collector();
element.CollectDirectory = directory;
element.PackRuleName = packRuleName;
element.FilterRuleName = filterRuleName;
element.DontWriteAssetPath = dontWriteAssetPath;
element.AssetTags = assetTags;
Setting.Collectors.Add(element);
if (saveFile)
SaveFile();
}
public static void RemoveCollector(string directory)
{
for (int i = 0; i < Setting.Collectors.Count; i++)
{
if (Setting.Collectors[i].CollectDirectory == directory)
{
Setting.Collectors.RemoveAt(i);
break;
}
}
SaveFile();
}
public static void ModifyCollector(string directory, string packRuleName, string filterRuleName, bool dontWriteAssetPath, string assetTags)
{
// 检测资源标签
if (dontWriteAssetPath && string.IsNullOrEmpty(assetTags) == false)
Debug.LogWarning($"Collector '{directory}' has asset tags '{assetTags}', It is invalid when enable dontWriteAssetPath.");
for (int i = 0; i < Setting.Collectors.Count; i++)
{
var collector = Setting.Collectors[i];
if (collector.CollectDirectory == directory)
{
collector.PackRuleName = packRuleName;
collector.FilterRuleName = filterRuleName;
collector.DontWriteAssetPath = dontWriteAssetPath;
collector.AssetTags = assetTags;
break;
}
}
SaveFile();
}
public static bool IsContainsCollector(string directory)
{
for (int i = 0; i < Setting.Collectors.Count; i++)
{
if (Setting.Collectors[i].CollectDirectory == directory)
return true;
}
return false;
}
public static bool CheckConflict(string directory)
{
if (IsContainsCollector(directory))
{
Debug.LogError($"Asset collector already existed : {directory}");
return true;
}
for (int i = 0; i < Setting.Collectors.Count; i++)
{
var wrapper = Setting.Collectors[i];
string compareDirectory = wrapper.CollectDirectory;
if (directory.StartsWith(compareDirectory))
{
Debug.LogError($"New asset collector \"{directory}\" conflict with \"{compareDirectory}\"");
return true;
}
if (compareDirectory.StartsWith(directory))
{
Debug.LogError($"New asset collector {directory} conflict with {compareDirectory}");
return true;
}
}
return false;
}
/// <summary>
/// 获取所有的DLC标记
/// </summary>
public static List<string> GetAllAssetTags()
{
List<string> result = new List<string>();
for (int i = 0; i < Setting.Collectors.Count; i++)
{
var tags = Setting.Collectors[i].GetAssetTags();
foreach (var tag in tags)
{
if (result.Contains(tag) == false)
result.Add(tag);
}
}
return result;
}
/// <summary>
/// 获取收集器总数
/// </summary>
public static int GetCollecterCount()
{
return Setting.Collectors.Count;
}
/// <summary>
/// 获取所有的收集路径
/// </summary>
public static List<string> GetAllCollectDirectory()
{
List<string> result = new List<string>();
for (int i = 0; i < Setting.Collectors.Count; i++)
{
AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i];
result.Add(collector.CollectDirectory);
}
return result;
}
/// <summary>
/// 获取所有收集的资源
/// 注意:跳过了不写入资源路径的收集器
/// </summary>
public static List<CollectAssetInfo> GetAllCollectAssets()
{
Dictionary<string, CollectAssetInfo> result = new Dictionary<string, CollectAssetInfo>(10000);
for (int i = 0; i < Setting.Collectors.Count; i++)
{
AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i];
// 注意:跳过不需要写入资源路径的收集器
if (collector.DontWriteAssetPath)
continue;
bool isRawAsset = collector.PackRuleName == nameof(PackRawFile);
string[] findAssets = EditorTools.FindAssets(EAssetSearchType.All, collector.CollectDirectory);
foreach (string assetPath in findAssets)
{
if (IsValidateAsset(assetPath) == false)
continue;
if (IsCollectAsset(assetPath, collector.FilterRuleName) == false)
continue;
if (result.ContainsKey(assetPath) == false)
{
var collectInfo = new CollectAssetInfo(assetPath, collector.GetAssetTags(), isRawAsset);
result.Add(assetPath, collectInfo);
}
}
}
return result.Values.ToList();
}
private static bool IsCollectAsset(string assetPath, string filterRuleName)
{
if (Setting.AutoCollectShaders)
{
Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
if (assetType == typeof(UnityEngine.Shader))
return true;
}
// 根据规则设置获取标签名称
IFilterRule filterRuleInstance = GetFilterRuleInstance(filterRuleName);
return filterRuleInstance.IsCollectAsset(assetPath);
}
/// <summary>
/// 检测资源路径是否被收集器覆盖
/// </summary>
public static bool HasCollector(string assetPath)
{
// 如果收集全路径着色器
if (Setting.AutoCollectShaders)
{
System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
if (assetType == typeof(UnityEngine.Shader))
return true;
}
for (int i = 0; i < Setting.Collectors.Count; i++)
{
AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i];
if (assetPath.StartsWith(collector.CollectDirectory))
return true;
}
return false;
}
/// <summary>
/// 检测资源是否有效
/// </summary>
public static bool IsValidateAsset(string assetPath)
{
if (assetPath.StartsWith("Assets/") == false && assetPath.StartsWith("Packages/") == false)
return false;
if (AssetDatabase.IsValidFolder(assetPath))
return false;
// 注意:忽略编辑器下的类型资源
Type type = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
if (type == typeof(LightingDataAsset))
return false;
string ext = System.IO.Path.GetExtension(assetPath);
if (ext == "" || ext == ".dll" || ext == ".cs" || ext == ".js" || ext == ".boo" || ext == ".meta")
return false;
return true;
}
/// <summary>
/// 获取资源包名
/// </summary>
public static string GetBundleLabel(string assetPath)
{
// 如果收集全路径着色器
if (Setting.AutoCollectShaders)
{
System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
if (assetType == typeof(UnityEngine.Shader))
{
return EditorTools.GetRegularPath(Setting.ShadersBundleName);
}
}
// 获取收集器
AssetBundleCollectorSetting.Collector findCollector = null;
for (int i = 0; i < Setting.Collectors.Count; i++)
{
AssetBundleCollectorSetting.Collector collector = Setting.Collectors[i];
if (assetPath.StartsWith(collector.CollectDirectory))
{
findCollector = collector;
break;
}
}
// 如果没有找到收集器
string bundleLabel;
if (findCollector == null)
{
IPackRule defaultInstance = new PackDirectory();
bundleLabel = defaultInstance.GetAssetBundleLabel(assetPath);
}
else
{
// 根据规则设置获取标签名称
IPackRule getInstance = GetPackRuleInstance(findCollector.PackRuleName);
bundleLabel = getInstance.GetAssetBundleLabel(assetPath);
}
// 返回包名
return EditorTools.GetRegularPath(bundleLabel);
}
private static IPackRule GetPackRuleInstance(string ruleName)
{
if (_cachePackRuleInstance.TryGetValue(ruleName, out IPackRule instance))
return instance;
// 如果不存在创建类的实例
if (_cachePackRuleTypes.TryGetValue(ruleName, out Type type))
{
instance = (IPackRule)Activator.CreateInstance(type);
_cachePackRuleInstance.Add(ruleName, instance);
return instance;
}
else
{
throw new Exception($"{nameof(IPackRule)}类型无效:{ruleName}");
}
}
private static IFilterRule GetFilterRuleInstance(string ruleName)
{
if (_cacheFilterRuleInstance.TryGetValue(ruleName, out IFilterRule instance))
return instance;
// 如果不存在创建类的实例
if (_cacheFilterRuleTypes.TryGetValue(ruleName, out Type type))
{
instance = (IFilterRule)Activator.CreateInstance(type);
_cacheFilterRuleInstance.Add(ruleName, instance);
return instance;
}
else
{
throw new Exception($"{nameof(IFilterRule)}类型无效:{ruleName}");
}
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 5d4d2a4e23c5e0646a07add04fd8e18a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,243 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace YooAsset.Editor
{
public class AssetBundleCollectorWindow : EditorWindow
{
static AssetBundleCollectorWindow _thisInstance;
[MenuItem("YooAsset/AssetBundle Collector", false, 101)]
static void ShowWindow()
{
if (_thisInstance == null)
{
_thisInstance = EditorWindow.GetWindow(typeof(AssetBundleCollectorWindow), false, "资源包收集工具", true) as AssetBundleCollectorWindow;
_thisInstance.minSize = new Vector2(800, 600);
}
_thisInstance.Show();
}
/// <summary>
/// 上次打开的文件夹路径
/// </summary>
private string _lastOpenFolderPath = "Assets/";
// GUI相关
private const float GuiDirecotryMinSize = 300f;
private const float GuiDirecotryMaxSize = 800f;
private const float GuiPackRuleSize = 130f;
private const float GuiFilterRuleSize = 130f;
private const float GuiDontWriteAssetPathSize = 130f;
private const float GuiAssetTagsMinSize = 100f;
private const float GuiAssetTagsMaxSize = 300f;
private const float GuiBtnSize = 40f;
private Vector2 _scrollPos = Vector2.zero;
// 初始化相关
private string[] _packRuleArray = null;
private string[] _filterRuleArray = null;
private bool _isInit = false;
private void Init()
{
List<string> packRuleNames = AssetBundleCollectorSettingData.GetPackRuleNames();
_packRuleArray = packRuleNames.ToArray();
List<string> filterRuleNames = AssetBundleCollectorSettingData.GetFilterRuleNames();
_filterRuleArray = filterRuleNames.ToArray();
}
private int PackRuleNameToIndex(string name)
{
for (int i = 0; i < _packRuleArray.Length; i++)
{
if (_packRuleArray[i] == name)
return i;
}
return 0;
}
private string IndexToPackRuleName(int index)
{
for (int i = 0; i < _packRuleArray.Length; i++)
{
if (i == index)
return _packRuleArray[i];
}
return string.Empty;
}
private int FilterRuleNameToIndex(string name)
{
for (int i = 0; i < _filterRuleArray.Length; i++)
{
if (_filterRuleArray[i] == name)
return i;
}
return 0;
}
private string IndexToFilterRuleName(int index)
{
for (int i = 0; i < _filterRuleArray.Length; i++)
{
if (i == index)
return _filterRuleArray[i];
}
return string.Empty;
}
private void OnGUI()
{
if (_isInit == false)
{
_isInit = true;
Init();
}
OnDrawShader();
OnDrawHeadBar();
OnDrawCollector();
}
private void OnDrawShader()
{
bool isCollectAllShader = AssetBundleCollectorSettingData.Setting.AutoCollectShaders;
string shadersBundleName = AssetBundleCollectorSettingData.Setting.ShadersBundleName;
EditorGUILayout.Space();
bool newToggleValue = EditorGUILayout.Toggle("收集所有着色器", isCollectAllShader);
if (newToggleValue != isCollectAllShader)
{
isCollectAllShader = newToggleValue;
AssetBundleCollectorSettingData.ModifyShader(isCollectAllShader, shadersBundleName);
}
if (isCollectAllShader)
{
string newTextValue = EditorGUILayout.TextField("AssetBundle名称", shadersBundleName, GUILayout.MaxWidth(300));
if (newTextValue != shadersBundleName)
{
shadersBundleName = newTextValue;
AssetBundleCollectorSettingData.ModifyShader(isCollectAllShader, shadersBundleName);
}
}
}
private void OnDrawHeadBar()
{
EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Directory", GUILayout.MinWidth(GuiDirecotryMinSize), GUILayout.MaxWidth(GuiDirecotryMaxSize));
EditorGUILayout.LabelField("PackRule", GUILayout.MinWidth(GuiPackRuleSize), GUILayout.MaxWidth(GuiPackRuleSize));
EditorGUILayout.LabelField("FilterRule", GUILayout.MinWidth(GuiFilterRuleSize), GUILayout.MaxWidth(GuiFilterRuleSize));
EditorGUILayout.LabelField("DontWriteAssetPath", GUILayout.MinWidth(GuiDontWriteAssetPathSize), GUILayout.MaxWidth(GuiDontWriteAssetPathSize));
EditorGUILayout.LabelField("AssetTags", GUILayout.MinWidth(GuiAssetTagsMinSize), GUILayout.MaxWidth(GuiAssetTagsMaxSize));
EditorGUILayout.LabelField("", GUILayout.MinWidth(GuiBtnSize), GUILayout.MaxWidth(GuiBtnSize));
EditorGUILayout.EndHorizontal();
}
private void OnDrawCollector()
{
// 列表显示
EditorGUILayout.Space();
_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
for (int i = 0; i < AssetBundleCollectorSettingData.Setting.Collectors.Count; i++)
{
var collector = AssetBundleCollectorSettingData.Setting.Collectors[i];
string directory = collector.CollectDirectory;
string packRuleName = collector.PackRuleName;
string filterRuleName = collector.FilterRuleName;
bool dontWriteAssetPath = collector.DontWriteAssetPath;
string assetTags = collector.AssetTags;
EditorGUILayout.BeginHorizontal();
{
// Directory
EditorGUILayout.LabelField(directory, GUILayout.MinWidth(GuiDirecotryMinSize), GUILayout.MaxWidth(GuiDirecotryMaxSize));
// IPackRule
{
int index = PackRuleNameToIndex(packRuleName);
int newIndex = EditorGUILayout.Popup(index, _packRuleArray, GUILayout.MinWidth(GuiPackRuleSize), GUILayout.MaxWidth(GuiPackRuleSize));
if (newIndex != index)
{
packRuleName = IndexToPackRuleName(newIndex);
AssetBundleCollectorSettingData.ModifyCollector(directory, packRuleName, filterRuleName, dontWriteAssetPath, assetTags);
}
}
// IFilterRule
{
int index = FilterRuleNameToIndex(filterRuleName);
int newIndex = EditorGUILayout.Popup(index, _filterRuleArray, GUILayout.MinWidth(GuiFilterRuleSize), GUILayout.MaxWidth(GuiFilterRuleSize));
if (newIndex != index)
{
filterRuleName = IndexToFilterRuleName(newIndex);
AssetBundleCollectorSettingData.ModifyCollector(directory, packRuleName, filterRuleName, dontWriteAssetPath, assetTags);
}
}
// DontWriteAssetPath
{
bool newToggleValue = EditorGUILayout.Toggle(dontWriteAssetPath, GUILayout.MinWidth(GuiDontWriteAssetPathSize), GUILayout.MaxWidth(GuiDontWriteAssetPathSize));
if (newToggleValue != dontWriteAssetPath)
{
dontWriteAssetPath = newToggleValue;
AssetBundleCollectorSettingData.ModifyCollector(directory, packRuleName, filterRuleName, dontWriteAssetPath, assetTags);
}
}
// AssetTags
{
if (collector.DontWriteAssetPath)
{
EditorGUILayout.LabelField(assetTags, GUILayout.MinWidth(GuiAssetTagsMinSize), GUILayout.MaxWidth(GuiAssetTagsMaxSize));
}
else
{
string newTextValue = EditorGUILayout.TextField(assetTags, GUILayout.MinWidth(GuiAssetTagsMinSize), GUILayout.MaxWidth(GuiAssetTagsMaxSize));
if (newTextValue != assetTags)
{
assetTags = newTextValue;
AssetBundleCollectorSettingData.ModifyCollector(directory, packRuleName, filterRuleName, dontWriteAssetPath, assetTags);
}
}
}
if (GUILayout.Button("-", GUILayout.MinWidth(GuiBtnSize), GUILayout.MaxWidth(GuiBtnSize)))
{
AssetBundleCollectorSettingData.RemoveCollector(directory);
break;
}
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
// 添加按钮
if (GUILayout.Button("+"))
{
string resultPath = EditorTools.OpenFolderPanel("Select Folder", _lastOpenFolderPath);
if (resultPath != null)
{
_lastOpenFolderPath = EditorTools.AbsolutePathToAssetPath(resultPath);
string defaultPackRuleName = nameof(PackExplicit);
string defaultFilterRuleName = nameof(CollectAll);
bool defaultDontWriteAssetPathValue = false;
string defaultAssetTag = string.Empty;
AssetBundleCollectorSettingData.AddCollector(_lastOpenFolderPath, defaultPackRuleName, defaultFilterRuleName, defaultDontWriteAssetPathValue, defaultAssetTag);
}
}
// 导入配置按钮
if (GUILayout.Button("Import Config"))
{
string resultPath = EditorTools.OpenFilePath("Select File", "Assets/", "xml");
if (resultPath != null)
{
CollectorConfigImporter.ImportXmlConfig(resultPath);
}
}
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cfca8094e2a905a4da05586b2d7fa919
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,30 +0,0 @@
using System.Collections;
using System.Collections.Generic;
namespace YooAsset.Editor
{
public class CollectAssetInfo
{
/// <summary>
/// 资源路径
/// </summary>
public string AssetPath { private set; get; }
/// <summary>
/// 资源标记列表
/// </summary>
public List<string> AssetTags { private set; get; }
/// <summary>
/// 是否为原生资源
/// </summary>
public bool IsRawAsset { private set; get; }
public CollectAssetInfo(string assetPath, List<string> assetTags, bool isRawAsset)
{
AssetPath = assetPath;
AssetTags = assetTags;
IsRawAsset = isRawAsset;
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b44b2cc585bc59c4d879d1edac67f7c4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,99 +0,0 @@
using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace YooAsset.Editor
{
public static class CollectorConfigImporter
{
private class CollectWrapper
{
public string CollectDirectory;
public string PackRuleName;
public string FilterRuleName;
public bool DontWriteAssetPath;
public string AssetTags;
public CollectWrapper(string directory, string packRuleName, string filterRuleName, bool dontWriteAssetPath, string assetTags)
{
CollectDirectory = directory;
PackRuleName = packRuleName;
FilterRuleName = filterRuleName;
DontWriteAssetPath = dontWriteAssetPath;
AssetTags = assetTags;
}
}
public const string XmlCollector = "Collector";
public const string XmlDirectory = "Directory";
public const string XmlPackRule = "PackRule";
public const string XmlFilterRule = "FilterRule";
public const string XmlDontWriteAssetPath = "DontWriteAssetPath";
public const string XmlAssetTags = "AssetTags";
public static void ImportXmlConfig(string filePath)
{
if (File.Exists(filePath) == false)
throw new FileNotFoundException(filePath);
if (Path.GetExtension(filePath) != ".xml")
throw new Exception($"Only support xml : {filePath}");
List<CollectWrapper> wrappers = new List<CollectWrapper>();
// 加载文件
XmlDocument xml = new XmlDocument();
xml.Load(filePath);
// 解析文件
XmlElement root = xml.DocumentElement;
XmlNodeList nodeList = root.GetElementsByTagName(XmlCollector);
if (nodeList.Count == 0)
throw new Exception($"Not found any {XmlCollector}");
foreach (XmlNode node in nodeList)
{
XmlElement collect = node as XmlElement;
string directory = collect.GetAttribute(XmlDirectory);
string packRuleName = collect.GetAttribute(XmlPackRule);
string filterRuleName = collect.GetAttribute(XmlFilterRule);
string dontWriteAssetPath = collect.GetAttribute(XmlDontWriteAssetPath);
string assetTags = collect.GetAttribute(XmlAssetTags);
if (Directory.Exists(directory) == false)
throw new Exception($"Not found directory : {directory}");
if (collect.HasAttribute(XmlPackRule) == false)
throw new Exception($"Not found attribute {XmlPackRule} in collector : {directory}");
if (collect.HasAttribute(XmlFilterRule) == false)
throw new Exception($"Not found attribute {XmlFilterRule} in collector : {directory}");
if (collect.HasAttribute(XmlDontWriteAssetPath) == false)
throw new Exception($"Not found attribute {XmlDontWriteAssetPath} in collector : {directory}");
if (collect.HasAttribute(XmlAssetTags) == false)
throw new Exception($"Not found attribute {XmlAssetTags} in collector : {directory}");
if (AssetBundleCollectorSettingData.HasPackRuleName(packRuleName) == false)
throw new Exception($"Invalid {nameof(IPackRule)} class type : {packRuleName}");
if (AssetBundleCollectorSettingData.HasFilterRuleName(filterRuleName) == false)
throw new Exception($"Invalid {nameof(IFilterRule)} class type : {filterRuleName}");
bool dontWriteAssetPathValue = StringUtility.StringToBool(dontWriteAssetPath);
CollectWrapper collectWrapper = new CollectWrapper(directory, packRuleName, filterRuleName, dontWriteAssetPathValue, assetTags);
wrappers.Add(collectWrapper);
}
// 导入配置数据
AssetBundleCollectorSettingData.ClearAllCollector();
foreach (var wrapper in wrappers)
{
AssetBundleCollectorSettingData.AddCollector(wrapper.CollectDirectory, wrapper.PackRuleName, wrapper.FilterRuleName, wrapper.DontWriteAssetPath, wrapper.AssetTags, false);
}
// 保存配置数据
AssetBundleCollectorSettingData.SaveFile();
Debug.Log($"导入配置完毕,一共导入{wrappers.Count}个收集器。");
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: c35b6554a4bedd049a8868d9249846d7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,53 +0,0 @@
using UnityEngine;
using UnityEditor;
using System.IO;
namespace YooAsset.Editor
{
/// <summary>
/// 收集所有资源
/// </summary>
public class CollectAll : IFilterRule
{
public bool IsCollectAsset(string assetPath)
{
return true;
}
}
/// <summary>
/// 只收集场景
/// </summary>
public class CollectScene : IFilterRule
{
public bool IsCollectAsset(string assetPath)
{
return Path.GetExtension(assetPath) == ".unity";
}
}
/// <summary>
/// 只收集预制体
/// </summary>
public class CollectPrefab : IFilterRule
{
public bool IsCollectAsset(string assetPath)
{
return Path.GetExtension(assetPath) == ".prefab";
}
}
/// <summary>
/// 只收集精灵类型的资源
/// </summary>
public class CollectSprite : IFilterRule
{
public bool IsCollectAsset(string assetPath)
{
if (AssetDatabase.GetMainAssetTypeAtPath(assetPath) == typeof(Sprite))
return true;
else
return false;
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: a74c81d149472fb4c960a1db1fd8accc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,55 +0,0 @@
using System;
using System.IO;
using UnityEditor;
namespace YooAsset.Editor
{
/// <summary>
/// 以文件路径作为AssetBundle标签名
/// </summary>
public class PackExplicit : IPackRule
{
string IPackRule.GetAssetBundleLabel(string assetPath)
{
return StringUtility.RemoveExtension(assetPath); //"Assets/Config/test.txt" --> "Assets/Config/test"
}
}
/// <summary>
/// 以父文件夹路径作为AssetBundle标签名
/// 注意该文件夹下所有资源被打到一个AssetBundle文件里
/// </summary>
public class PackDirectory : IPackRule
{
string IPackRule.GetAssetBundleLabel(string assetPath)
{
return Path.GetDirectoryName(assetPath); //"Assets/Config/test.txt" --> "Assets/Config"
}
}
/// <summary>
/// 原生文件打包模式
/// 注意:原生文件打包支持:图片,音频,视频,文本
/// </summary>
public class PackRawFile : IPackRule
{
string IPackRule.GetAssetBundleLabel(string assetPath)
{
string extension = StringUtility.RemoveFirstChar(Path.GetExtension(assetPath));
if (extension == EAssetFileExtension.unity.ToString() || extension == EAssetFileExtension.prefab.ToString() ||
extension == EAssetFileExtension.mat.ToString() || extension == EAssetFileExtension.controller.ToString() ||
extension == EAssetFileExtension.fbx.ToString() || extension == EAssetFileExtension.anim.ToString() ||
extension == EAssetFileExtension.shader.ToString())
{
throw new Exception($"{nameof(PackRawFile)} is not support file estension : {extension}");
}
// 注意:原生文件只支持无依赖关系的资源
string[] depends = AssetDatabase.GetDependencies(assetPath, true);
if (depends.Length != 1)
throw new Exception($"{nameof(PackRawFile)} is not support estension : {extension}");
return StringUtility.RemoveExtension(assetPath);
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 4a924dc0a22fc104781bf9aaadd60c29
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,16 +0,0 @@

namespace YooAsset.Editor
{
/// <summary>
/// 资源过滤规则接口
/// </summary>
public interface IFilterRule
{
/// <summary>
/// 是否为收集资源
/// </summary>
/// <param name="assetPath">资源路径</param>
/// <returns>如果收集该资源返回TRUE</returns>
bool IsCollectAsset(string assetPath);
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: ffe1385deb0bd9844a514f1c2fd65e62
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,14 +0,0 @@

namespace YooAsset.Editor
{
/// <summary>
/// 资源打包规则接口
/// </summary>
public interface IPackRule
{
/// <summary>
/// 获取资源的打包标签
/// </summary>
string GetAssetBundleLabel(string assetPath);
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 577f7c1abcdf0a140958f1d1d44d6f40
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: