主要负责收集Bundle内使用的link.xml,防止被裁剪。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class AssetBundleChecker { [MenuItem("Tools/CreateLink.xml")] public static void Create() { var generator = new LinkXmlGenerator(); generator.SetTypeConversion(typeof(UnityEditor.Animations.AnimatorController), typeof(RuntimeAnimatorController)); foreach (var bundleName in AssetDatabase.GetAllAssetBundleNames()) { var assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(bundleName); generator.AddAssets(assetPaths); } generator.Save("Assets/link.xml"); Debug.Log("link 文件构建完毕"); AssetDatabase.Refresh(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Xml; using UnityEditor; using UnityEngine; public class LinkXmlGenerator { Dictionary<Type, Type> m_TypeConversion = new Dictionary<Type, Type>(); HashSet<Type> m_Types = new HashSet<Type>(); public void AddType(Type type) { if (type == null) return; AddTypeInternal(type); } public void AddTypes(params Type[] types) { if (types == null) return; foreach (var t in types) AddTypeInternal(t); } public void AddTypes(IEnumerable<Type> types) { if (types == null) return; foreach (var t in types) AddTypeInternal(t); } private void AddTypeInternal(Type t) { if (t == null) return; Type convertedType; if (m_TypeConversion.TryGetValue(t, out convertedType)) m_Types.Add(convertedType); else m_Types.Add(t); } public void SetTypeConversion(Type a, Type b) { m_TypeConversion[a] = b; } public void AddAsset(string assetpath) { var assets = AssetDatabase.GetDependencies(assetpath); List<Type> types = new List<Type>(); foreach (var asset in assets) { var type = AssetDatabase.GetMainAssetTypeAtPath(asset); if (type == typeof(GameObject)) { var obj = (GameObject)AssetDatabase.LoadAssetAtPath(asset, typeof(GameObject)); types.AddRange(obj.GetComponentsInChildren<Component>(true).Select(c => c.GetType())); } else { types.Add(type); } } AddTypes(types); } public void AddAssets(string[] assetPaths) { foreach (var assetPath in assetPaths) AddAsset(assetPath); } public void Save(string path) { var assemblyMap = new Dictionary<Assembly, List<Type>>(); foreach (var t in m_Types) { var a = t.Assembly; List<Type> types; if (!assemblyMap.TryGetValue(a, out types)) assemblyMap.Add(a, types = new List<Type>()); types.Add(t); } XmlDocument doc = new XmlDocument(); var linker = doc.AppendChild(doc.CreateElement("linker")); foreach (var k in assemblyMap) { if (k.Key.FullName.Contains("UnityEditor")) continue; var assembly = linker.AppendChild(doc.CreateElement("assembly")); var attr = doc.CreateAttribute("fullname"); attr.Value = k.Key.FullName; if (assembly.Attributes != null) { assembly.Attributes.Append(attr); foreach (var t in k.Value) { var typeEl = assembly.AppendChild(doc.CreateElement("type")); var tattr = doc.CreateAttribute("fullname"); tattr.Value = t.FullName; if (typeEl.Attributes != null) { typeEl.Attributes.Append(tattr); var pattr = doc.CreateAttribute("preserve"); pattr.Value = "all"; typeEl.Attributes.Append(pattr); } } } } doc.Save(path); } } |
- 本文固定链接: http://www.u3d8.com/?p=2789
- 转载请注明: 网虫虫 在 u3d8.com 发表过