工具类,这是网上找的,可以动态的创建或删除组,为资源添加label 加入到组。
using System.Collections.Generic; using UnityEditor.AddressableAssets.Settings; using UnityEngine; public class AASUtility : UnityEditor.Editor { public static UnityEditor.AddressableAssets.Settings.AddressableAssetSettings GetSettings() { //アドレサブルアセットセッティング取得 var d = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.AddressableAssets.Settings.AddressableAssetSettings>( "Assets/AddressableAssetsData/AddressableAssetSettings.asset" ); return d; } public static UnityEditor.AddressableAssets.Settings.AddressableAssetGroup CreateGroup(string groupName, bool setAsDefaultGroup = false) { //アドレサブルアセットセッティング取得 var s = GetSettings(); //スキーマ生成 List<UnityEditor.AddressableAssets.Settings.AddressableAssetGroupSchema> schema = new List<UnityEditor.AddressableAssets.Settings.AddressableAssetGroupSchema>() { ScriptableObject.CreateInstance<UnityEditor.AddressableAssets.Settings.GroupSchemas.BundledAssetGroupSchema>(), ScriptableObject.CreateInstance<UnityEditor.AddressableAssets.Settings.GroupSchemas.ContentUpdateGroupSchema>(), }; //グループの作成 var f = s.groups.Find((g) => { return g.name == groupName; }); if (f == null) { f = s.CreateGroup(groupName, setAsDefaultGroup, false, true, schema); } return f; } public static AddressableAssetEntry AddAssetToGroup(string assetGuid, string groupName) { if (assetGuid.Equals("")) { Debug.Log($"assetGuid is empty, groupName: {groupName}"); return null; } var s = GetSettings(); var g = CreateGroup(groupName); var entry = s.CreateOrMoveEntry(assetGuid, g); return entry; } public static void SetLabelToAsset(List<string> assetGuidList, string label, bool flag) { var s = GetSettings(); //ラベルを追加するように呼んでおく。追加されていないと設定されない。 s.AddLabel(label); List<UnityEditor.AddressableAssets.Settings.AddressableAssetEntry> assetList = new List<UnityEditor.AddressableAssets.Settings.AddressableAssetEntry>(); s.GetAllAssets(assetList, true); foreach (var assetGuid in assetGuidList) { var asset = assetList.Find((a) => { return a.guid == assetGuid; }); if (asset != null) { asset.SetLabel(label, flag); } } } public static void RemoveAssetFromGroup(string assetGuid) { var s = GetSettings(); s.RemoveAssetEntry(assetGuid); } public static void RemoveAllGroups() { var s = GetSettings(); var list = s.groups; List<AddressableAssetGroup> temp_list = new List<AddressableAssetGroup>(); for (int i = list.Count - 1; i >= 0; i--) { temp_list.Add(list[i]); } for (int i = temp_list.Count - 1; i >= 0; i--) { s.RemoveGroup(temp_list[i]); } } //public static void AddGroup(string groupName, bool setAsDefaultGroup, bool readOnly, bool postEvent, List<AddressableAssetGroupSchema> schemasToCopy, params Type[] types) //{ // var s = GetSettings(); // s.CreateGroup(groupName, setAsDefaultGroup,readOnly,postEvent,schemasToCopy,types); //} public static void BuildPlayerContent() { //System.Threading.Thread.Sleep(30000); var d = GetSettings(); d.ActivePlayerDataBuilderIndex = 3; //AddressableAssetSettings.CleanPlayerContent(d.ActivePlayerDataBuilder); AddressableAssetSettings.BuildPlayerContent(); } public static void CleanPlayerContent() { // var d = GetSettings(); // d.ActivePlayerDataBuilderIndex = 3; //AddressableAssetSettings.CleanPlayerContent(d.ActivePlayerDataBuilder); AddressableAssetSettings.CleanPlayerContent(); UnityEditor.Build.Pipeline.Utilities.BuildCache.PurgeCache(false); // AssetImportMgr.OnDataBuilderComplete(); } static public void Test() { var d = GetSettings(); var matguid = UnityEditor.AssetDatabase.AssetPathToGUID("Assets/Data/hogeMat.mat"); AddAssetToGroup(matguid, "CreatedGroup"); ////List<string> assetGuidList = new List<string>() { matguid }; ////SetLabelToAsset(assetGuidList, "mat", true); //CreateGroup("CreatedGroup"); } }
静态方法,提供给编辑器扩展菜单使用
using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEditor.AddressableAssets.Settings.GroupSchemas; using UnityEditor.AddressableAssets.Settings; using UnityEngine.ResourceManagement.ResourceProviders; public class AddressableTool { private static string PackPath = Config.PackPath; //图集碎片目录名字 private static string AssetDataPath = Config.AssetDataPath; //打包资源的目录名字 private static Dictionary<string, bool> originGroups; private static Dictionary<string, AddressableAssetGroup> originGroupMaps; //严禁一级目录下有需要打包文件,全用二级目录做好分配 public static void MarkAssets() { //AASUtility.RemoveAllGroups(); 不要移除所有的组 为改变guid 导致每次的bundle的hash都不一样 //记录原来的组 originGroups = new Dictionary<string, bool>(); originGroupMaps = new Dictionary<string, AddressableAssetGroup>(); var setting = AASUtility.GetSettings(); var groups = setting.groups; if (groups != null) { foreach (var g in groups) { originGroups.Add(g.Name, false); originGroupMaps.Add(g.Name, g); } } string assetPath = Path.Combine(Application.dataPath, AssetDataPath); DirectoryInfo[] levelsDataDirs = new DirectoryInfo(assetPath).GetDirectories(); foreach (var dir in levelsDataDirs) { //场景目录 MarkLevelDataDir(dir); } //对比移除无用的组 foreach (var iter in originGroups) { if (!iter.Value && originGroupMaps.ContainsKey(iter.Key)) { setting.RemoveGroup(originGroupMaps[iter.Key]); } } AssetDatabase.Refresh(); } private static void MarkLevelDataDir(DirectoryInfo dir) { string parentName = dir.Name; DirectoryInfo[] dirs = dir.GetDirectories(); foreach (var d in dirs) { //场景资源分类目录 一个打一个组 MarkDir(d, parentName); } } private static void MarkDir(DirectoryInfo dir,string parentName) { if (dir.Name == PackPath) return; bool isTipLabel = false; if (parentName == "Src")//Lua脚本 { isTipLabel = true; } string groudName = string.Format("{0}.{1}", parentName, dir.Name); AASUtility.CreateGroup(groudName); if (originGroups.ContainsKey(groudName)) { originGroups[groudName] = true; } List<string> allFiles = new List<string>(); List<string> allFileGuids = new List<string>(); GetMarkFiles(allFiles, dir); foreach(var fileStr in allFiles) { string uFilePath = UtilityTool.ChangePath(fileStr); string guid = AssetDatabase.AssetPathToGUID(uFilePath); var entry = AASUtility.AddAssetToGroup(guid, groudName); //缩减 可寻址的长度 lua脚本的同步require的路径 entry.address = uFilePath.Replace("Assets/" + "LevelsData" + "/", "").Replace("Src" + "/", ""); allFileGuids.Add(guid); } if (isTipLabel) { AASUtility.SetLabelToAsset(allFileGuids, groudName, true);//脚本设置lable 用于预加载 } AASUtility.SetLabelToAsset(allFileGuids, "default", true);//所有资源都设为default,以前是拿来检测更新的,现在好像没用了 } private static void GetMarkFiles(List<string> list, DirectoryInfo dir) { if (dir.Name == PackPath) return; FileInfo[] files = dir.GetFiles(); foreach (var file in files) { if (file.FullName.EndsWith(".meta")) continue; list.Add(file.FullName); } DirectoryInfo[] dirs = dir.GetDirectories(); foreach (var d in dirs) { GetMarkFiles(list, d); } } }