|
8 | 8 |
|
9 | 9 | namespace EDT
|
10 | 10 | {
|
11 |
| - public class EditorResourceBuild |
| 11 | +public class EditorResourceBuild |
| 12 | +{ |
| 13 | + static public Dictionary<string, string> m_mapAssetList = new Dictionary<string, string>(); |
| 14 | + |
| 15 | + public static string ASSET_PATH_KEY = "fzgdhks"; |
| 16 | + |
| 17 | + static string GetPlatformName(BuildTarget buildTarget) |
12 | 18 | {
|
13 |
| - static string OUTPUTPATH = Application.dataPath.Replace("/Assets", string.Empty) + "/AssetBundles"; |
| 19 | + string szFolerName = "Windows"; |
| 20 | + switch (buildTarget) |
| 21 | + { |
| 22 | + case BuildTarget.Android: |
| 23 | + szFolerName = "Android"; |
| 24 | + break; |
14 | 25 |
|
15 |
| - static void DelBundles() |
| 26 | + case BuildTarget.iOS: |
| 27 | + szFolerName = "iOS"; |
| 28 | + break; |
| 29 | + } |
| 30 | + return szFolerName; |
| 31 | + } |
| 32 | + |
| 33 | + static string GetAssetBundleOutPutPath(BuildTarget buildTarget) |
| 34 | + { |
| 35 | + return Application.dataPath.Replace("/Assets", string.Empty) + "/AssetBundles" + "/" + GetPlatformName(buildTarget) + "/"; |
| 36 | + } |
| 37 | + static bool DeleteBundles() |
| 38 | + { |
| 39 | + if (Directory.Exists(GetAssetBundleOutPutPath(EditorUserBuildSettings.activeBuildTarget))) |
16 | 40 | {
|
17 |
| - if (Directory.Exists(OUTPUTPATH)) |
18 |
| - { |
19 |
| - Directory.Delete(OUTPUTPATH, true); |
20 |
| - } |
21 |
| - Directory.CreateDirectory(OUTPUTPATH); |
| 41 | + Directory.Delete(GetAssetBundleOutPutPath(EditorUserBuildSettings.activeBuildTarget), true); |
22 | 42 | }
|
23 | 43 |
|
24 |
| - static void DelBundleNames() |
| 44 | + Directory.CreateDirectory(GetAssetBundleOutPutPath(EditorUserBuildSettings.activeBuildTarget)); |
| 45 | + |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + static bool DeleteBundleNames() |
| 50 | + { |
| 51 | + string[] oldAssetBundleNames = AssetDatabase.GetAllAssetBundleNames(); |
| 52 | + if(oldAssetBundleNames.Length <= 0) |
25 | 53 | {
|
26 |
| - var length = AssetDatabase.GetAllAssetBundleNames().Length; |
27 |
| - var oldAssetBundleNames = new string[length]; |
28 |
| - for (var i = 0; i < length; i++) |
29 |
| - { |
30 |
| - oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i]; |
31 |
| - } |
32 |
| - for (var j = 0; j < oldAssetBundleNames.Length; j++) |
33 |
| - { |
34 |
| - AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true); |
35 |
| - } |
| 54 | + return true; |
36 | 55 | }
|
37 | 56 |
|
38 |
| - static void SetBundleConfig() |
| 57 | + for (var i = 0; i < oldAssetBundleNames.Length; i++) |
39 | 58 | {
|
40 |
| - List<GTResourceData> list = new List<GTResourceData>(); |
41 |
| - UnityEngine.Object[] assets = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets); |
42 |
| - for (int i = 0; i < assets.Length; i++) |
| 59 | + AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[i], true); |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + static bool SetBundleConfig() |
| 66 | + { |
| 67 | + UnityEngine.Object[] assets = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets); |
| 68 | + for (int i = 0; i < assets.Length; i++) |
| 69 | + { |
| 70 | + UnityEngine.Object obj = assets[i]; |
| 71 | + string assetPath = AssetDatabase.GetAssetPath(obj); |
| 72 | + string extenName = System.IO.Path.GetExtension(assetPath).ToLower(); |
| 73 | + if (string.IsNullOrEmpty(extenName) || extenName == ".meta") |
| 74 | + { |
| 75 | + continue; |
| 76 | + } |
| 77 | + switch (extenName) |
43 | 78 | {
|
44 |
| - UnityEngine.Object obj = assets[i]; |
45 |
| - string assetPath = AssetDatabase.GetAssetPath(obj); |
46 |
| - string extenName = System.IO.Path.GetExtension(assetPath).ToLower(); |
47 |
| - if (string.IsNullOrEmpty(extenName) || extenName == ".meta") |
| 79 | + case ".xml": |
| 80 | + case ".txt": |
48 | 81 | {
|
49 |
| - continue; |
| 82 | + EditorResourceBuild.m_mapAssetList[assetPath] = obj.name + extenName + ".assetbundle"; |
50 | 83 | }
|
51 |
| - GTResourceData bundle = new GTResourceData(); |
52 |
| - switch (extenName) |
| 84 | + break; |
| 85 | + case ".prefab": |
53 | 86 | {
|
54 |
| - case ".xml": |
55 |
| - case ".txt": |
56 |
| - { |
57 |
| - bundle.AssetName = obj.name + extenName; |
58 |
| - bundle.AssetBundleName = obj.name + extenName + ".assetbundle"; |
59 |
| - bundle.Path = assetPath; |
60 |
| - bundle.GUID = AssetDatabase.AssetPathToGUID(bundle.Path); |
61 |
| - } |
62 |
| - break; |
63 |
| - case ".prefab": |
64 |
| - { |
65 |
| - bundle.AssetName = obj.name + extenName; |
66 |
| - bundle.AssetBundleName = GTTools.GetParentPathName(assetPath) + ".pre.assetbundle"; |
67 |
| - bundle.Path = assetPath; |
68 |
| - bundle.GUID = AssetDatabase.AssetPathToGUID(bundle.Path); |
69 |
| - } |
70 |
| - break; |
71 |
| - case ".mp3": |
72 |
| - { |
73 |
| - bundle.AssetName = obj.name + extenName; |
74 |
| - bundle.AssetBundleName = obj.name + extenName + ".assetbundle"; |
75 |
| - bundle.Path = assetPath; |
76 |
| - bundle.GUID = AssetDatabase.AssetPathToGUID(bundle.Path); |
77 |
| - } |
78 |
| - break; |
79 |
| - case ".png": |
80 |
| - { |
81 |
| - if (assetPath.Contains("Image")) |
82 |
| - { |
83 |
| - bundle.AssetName = obj.name + extenName; |
84 |
| - bundle.AssetBundleName = GTTools.GetParentPathName(assetPath) + ".atlas.assetbundle"; |
85 |
| - bundle.Path = assetPath; |
86 |
| - bundle.GUID = AssetDatabase.AssetPathToGUID(bundle.Path); |
87 |
| - } |
88 |
| - if (assetPath.Contains("T_Background")) |
89 |
| - { |
90 |
| - bundle.AssetName = obj.name + extenName; |
91 |
| - bundle.AssetBundleName = obj.name + ".tex.assetbundle"; |
92 |
| - bundle.GUID = AssetDatabase.AssetPathToGUID(bundle.Path); |
93 |
| - } |
94 |
| - } |
95 |
| - break; |
| 87 | + EditorResourceBuild.m_mapAssetList[assetPath] = GTTools.GetParentPathName(assetPath) + ".pre.assetbundle"; |
96 | 88 | }
|
97 |
| - |
98 |
| - if (string.IsNullOrEmpty(bundle.AssetName)) |
| 89 | + break; |
| 90 | + case ".mp3": |
| 91 | + { |
| 92 | + EditorResourceBuild.m_mapAssetList[assetPath] = obj.name + extenName + ".assetbundle"; |
| 93 | + } |
| 94 | + break; |
| 95 | + case ".png": |
99 | 96 | {
|
100 |
| - continue; |
| 97 | + if (assetPath.Contains("Textures")) |
| 98 | + { |
| 99 | + EditorResourceBuild.m_mapAssetList[assetPath] = GTTools.GetParentPathName(assetPath) + ".atlas.assetbundle"; |
| 100 | + } |
101 | 101 | }
|
102 |
| - bundle.AssetBundleName = bundle.AssetBundleName.ToLower(); |
103 |
| - list.Add(bundle); |
| 102 | + break; |
104 | 103 | }
|
| 104 | + } |
105 | 105 |
|
106 |
| - list.Sort((a1, a2) => { return a1.AssetName.CompareTo(a2.AssetName); }); |
107 |
| - GTResourceManager.Instance.Data.Clear(); |
108 |
| - foreach (var current in list) |
109 |
| - { |
110 |
| - GTResourceManager.Instance.Data[current.Path] = current; |
111 |
| - } |
| 106 | + XmlDocument doc = new XmlDocument(); |
| 107 | + XmlNode root = doc.CreateElement("root"); |
| 108 | + doc.AppendChild(root); |
| 109 | + foreach (var current in EditorResourceBuild.m_mapAssetList) |
| 110 | + { |
| 111 | + XmlElement child = doc.CreateElement("row"); |
| 112 | + root.AppendChild(child); |
| 113 | + child.SetAttribute("AssetName", current.Key); |
| 114 | + child.SetAttribute("AssetBundleName", current.Value.ToLower()); |
| 115 | + } |
112 | 116 |
|
113 |
| - XmlDocument doc = new XmlDocument(); |
114 |
| - XmlNode root = doc.CreateElement("root"); |
115 |
| - doc.AppendChild(root); |
116 |
| - foreach (var current in list) |
117 |
| - { |
118 |
| - GTResourceData bundle = current; |
119 |
| - XmlElement child = doc.CreateElement("row"); |
120 |
| - root.AppendChild(child); |
121 |
| - child.SetAttribute("AssetName", bundle.AssetName); |
122 |
| - child.SetAttribute("AssetBundleName", bundle.AssetBundleName); |
123 |
| - child.SetAttribute("Path", bundle.Path); |
124 |
| - child.SetAttribute("GUID", bundle.GUID); |
125 |
| - } |
| 117 | + string fileName = GetAssetBundleOutPutPath(EditorUserBuildSettings.activeBuildTarget) + "/assetlist.xml"; |
| 118 | + doc.Save(fileName); |
| 119 | + return true; |
| 120 | + } |
126 | 121 |
|
127 |
| - string fileName = Application.streamingAssetsPath + "/Asset.xml"; |
128 |
| - FileStream fs = null; |
129 |
| - if (!File.Exists(fileName)) |
130 |
| - { |
131 |
| - fs = File.Create(fileName); |
132 |
| - } |
133 |
| - doc.Save(fileName); |
134 |
| - if (fs != null) |
| 122 | + static bool SetBundleNames() |
| 123 | + { |
| 124 | + foreach (var current in EditorResourceBuild.m_mapAssetList) |
| 125 | + { |
| 126 | + var assetImporter = AssetImporter.GetAtPath(current.Key); |
| 127 | + if (assetImporter != null) |
135 | 128 | {
|
136 |
| - fs.Flush(); |
137 |
| - fs.Dispose(); |
138 |
| - fs.Close(); |
| 129 | + assetImporter.assetBundleName = current.Value.ToLower(); |
139 | 130 | }
|
140 | 131 | }
|
141 | 132 |
|
142 |
| - static void SetBundleNames() |
| 133 | + return true; |
| 134 | + } |
| 135 | + public static string GetMD5(byte[] src) |
| 136 | + { |
| 137 | + var md5 = System.Security.Cryptography.MD5.Create(); |
| 138 | + byte[] bytes = md5.ComputeHash(src); |
| 139 | + md5.Clear(); |
| 140 | + |
| 141 | + var sb = new System.Text.StringBuilder(); |
| 142 | + for (int i = 0; i < bytes.Length; i++) |
143 | 143 | {
|
144 |
| - Dictionary<string, GTResourceData> units = GTResourceManager.Instance.Data; |
145 |
| - foreach (var current in units) |
146 |
| - { |
147 |
| - GTResourceData bundle = current.Value; |
148 |
| - var assetImporter = AssetImporter.GetAtPath(bundle.Path); |
149 |
| - if (assetImporter != null) |
150 |
| - { |
151 |
| - assetImporter.assetBundleName = bundle.AssetBundleName.ToLower(); |
152 |
| - } |
153 |
| - } |
| 144 | + sb.Append(bytes[i].ToString("x2")); |
154 | 145 | }
|
155 | 146 |
|
156 |
| - static void BuildBundles() |
| 147 | + return sb.ToString(); |
| 148 | + } |
| 149 | + public static string GetAssetPathMd5(string path) |
| 150 | + { |
| 151 | + string tmp = path + EditorResourceBuild.ASSET_PATH_KEY; |
| 152 | + byte[] data = System.Text.Encoding.UTF8.GetBytes(tmp); |
| 153 | + return GetMD5(data); |
| 154 | + } |
| 155 | + |
| 156 | + static void BuildBundles() |
| 157 | + { |
| 158 | + BuildPipeline.BuildAssetBundles(GetAssetBundleOutPutPath(EditorUserBuildSettings.activeBuildTarget), BuildAssetBundleOptions.UncompressedAssetBundle, EditorUserBuildSettings.activeBuildTarget); |
| 159 | + } |
| 160 | + |
| 161 | + public static void Build() |
| 162 | + { |
| 163 | + if(!DeleteBundles()) |
157 | 164 | {
|
158 |
| - BuildPipeline.BuildAssetBundles(OUTPUTPATH, BuildAssetBundleOptions.UncompressedAssetBundle, |
159 |
| - EditorUserBuildSettings.activeBuildTarget); |
| 165 | + return; |
160 | 166 | }
|
161 | 167 |
|
162 |
| - public static void Build() |
| 168 | + |
| 169 | + //if (!DeleteBundleNames()) |
| 170 | + //{ |
| 171 | + // return; |
| 172 | + //} |
| 173 | + |
| 174 | + //return; |
| 175 | + if (!SetBundleConfig()) |
163 | 176 | {
|
164 |
| - DelBundles(); |
165 |
| - DelBundleNames(); |
166 |
| - SetBundleConfig(); |
167 |
| - SetBundleNames(); |
168 |
| - BuildBundles(); |
| 177 | + return; |
169 | 178 | }
|
| 179 | + SetBundleNames(); |
| 180 | + BuildBundles(); |
170 | 181 | }
|
171 | 182 | }
|
| 183 | +} |
0 commit comments