码迷,mamicode.com
首页 > 编程语言 > 详细

Unity 之 Addressable Asset System 之用工具创建group

时间:2021-02-26 13:02:22      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:false   html   rect   leo   entry   lse   getdir   正则   tin   

代码如下:

  1 using System.Linq;
  2 using System.IO;
  3 using UnityEditor;
  4 using UnityEditor.AddressableAssets;
  5 using UnityEditor.AddressableAssets.Settings;
  6 using UnityEngine;
  7 using System;
  8 using AssetBundle;
  9 using System.Collections.Generic;
 10 using System.Text.RegularExpressions;
 11 using UObject = UnityEngine.Object;
 12 
 13 public class AddressableGroupSetter : ScriptableObject
 14 {
 15     static AddressableAssetSettings Settings
 16     {
 17         get { return AddressableAssetSettingsDefaultObject.Settings; }
 18     }
 19 
 20 
 21     [MenuItem("Test/Reset Groups")]
 22     static void ResetGroups()
 23     {
 24         // ui
 25         ResetGroup<GameObject>("ui_prefab", "Assets/UI/Prefab/", "f:*.prefab", assetPath =>
 26         {
 27             string fileName = Path.GetFileNameWithoutExtension(assetPath);
 28             string dirPath = Path.GetDirectoryName(assetPath);
 29             string dirName = Path.GetFileNameWithoutExtension(dirPath);
 30             return $"{dirName}/{fileName}";
 31         });
 32         
 33         // ...
 34     }
 35 
 36     /// <summary>
 37     /// 重置某分组
 38     /// </summary>
 39     /// <typeparam name="T">资源类型</typeparam>
 40     /// <param name="groupName">组名</param>
 41     /// <param name="assetFolder">资源目录</param>
 42     /// <param name="filter">过滤器:
 43     /// 若以t:开头,表示用unity的方式过滤; 
 44     /// 若以f:开头,表示用windows的SearchPattern方式过滤; 
 45     /// 若以r:开头,表示用正则表达式的方式过滤。</param>
 46     /// <param name="getAddress">通过 asset path 得到地址名</param>
 47     static void ResetGroup<T>(string groupName, string assetFolder, string filter, Func<string, string> getAddress)
 48     {
 49         string[] assets = GetAssets(assetFolder, filter);
 50 #if Debug
 51         foreach (var assetPath in assets)
 52         {
 53             string address = getAddress(assetPath).ToLower();
 54             Debug.Log($"{assetPath}\t{address}");
 55         }
 56 #else
 57         AddressableAssetGroup group = CreateGroup<T>(groupName);
 58         foreach (var assetPath in assets)
 59         {
 60             string address = getAddress(assetPath).ToLower();
 61             AddAssetEntry(group, assetPath, address);
 62         }
 63 #endif
 64 
 65         Debug.Log($"Reset group finished, group: {groupName}, asset folder: {assetFolder}, filter: {filter}, count: {assets.Length}");
 66     }
 67 
 68     // 创建分组
 69     static AddressableAssetGroup CreateGroup<T>(string groupName)
 70     {
 71         AddressableAssetGroup group = Settings.FindGroup(groupName);
 72         if (group == null)
 73             group = Settings.CreateGroup(groupName, false, false, false, null, typeof(T));
 74         Settings.AddLabel(groupName, false);
 75         return group;
 76     }
 77 
 78     // 给某分组添加资源
 79     static AddressableAssetEntry AddAssetEntry(AddressableAssetGroup group, string assetPath, string address)
 80     {
 81         string guid = AssetDatabase.AssetPathToGUID(assetPath);
 82 
 83         AddressableAssetEntry entry = group.entries.FirstOrDefault(e => e.guid == guid);
 84         if (entry == null)
 85         {
 86             entry = Settings.CreateOrMoveEntry(guid, group, false, false);
 87         }
 88 
 89         entry.address = address;
 90         entry.SetLabel(group.Name, true, false, false);
 91         return entry;
 92     }
 93 
 94     /// <summary>
 95     /// 获取指定目录的资源
 96     /// </summary>
 97     /// <param name="filter">过滤器:
 98     /// 若以t:开头,表示用unity的方式过滤; 
 99     /// 若以f:开头,表示用windows的SearchPattern方式过滤; 
100     /// 若以r:开头,表示用正则表达式的方式过滤。</param>
101     public static string[] GetAssets(string folder, string filter)
102     {
103         if (string.IsNullOrEmpty(folder))
104             throw new ArgumentException("folder");
105         if (string.IsNullOrEmpty(filter))
106             throw new ArgumentException("filter");
107 
108         folder = folder.TrimEnd(/).TrimEnd(\\);
109 
110         if (filter.StartsWith("t:"))
111         {
112             string[] guids = AssetDatabase.FindAssets(filter, new string[] { folder });
113             string[] paths = new string[guids.Length];
114             for (int i = 0; i < guids.Length; i++)
115                 paths[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
116             return paths;
117         }
118         else if (filter.StartsWith("f:"))
119         {
120             string folderFullPath = BuildingBundle.GetFullPath(folder);
121             string searchPattern = filter.Substring(2);
122             string[] files = Directory.GetFiles(folderFullPath, searchPattern, SearchOption.AllDirectories);
123             string[] paths = new string[files.Length];
124             for (int i = 0; i < files.Length; i++)
125                 paths[i] = BuildingBundle.GetAssetPath(files[i]);
126             return paths;
127         }
128         else if (filter.StartsWith("r:"))
129         {
130             string folderFullPath = BuildingBundle.GetFullPath(folder);
131             string pattern = filter.Substring(2);
132             string[] files = Directory.GetFiles(folderFullPath, "*.*", SearchOption.AllDirectories);
133             List<string> list = new List<string>();
134             for (int i = 0; i < files.Length; i++)
135             {
136                 string name = Path.GetFileName(files[i]);
137                 if (Regex.IsMatch(name, pattern))
138                 {
139                     string p = BuildingBundle.GetAssetPath(files[i]);
140                     list.Add(p);
141                 }
142             }
143             return list.ToArray();
144         }
145         else
146         {
147             throw new InvalidOperationException("Unexpected filter: " + filter);
148         }
149     }
150 }

 

执行菜单 Test/Reset Group 后效果如下:

 

转载请注明出处:https://www.cnblogs.com/jietian331/p/14446825.html

Unity 之 Addressable Asset System 之用工具创建group

标签:false   html   rect   leo   entry   lse   getdir   正则   tin   

原文地址:https://www.cnblogs.com/jietian331/p/14446825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!