AssetsPackInfo.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #if !COMBAT_SERVER
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using LitJson;
  7. using UnityEngine;
  8. namespace Utility
  9. {
  10. /// <summary>
  11. /// 用来做JsonUtility.FromJson的容器类
  12. /// </summary>
  13. [Serializable]
  14. public class AllPackedAssetsInfo
  15. {
  16. public List<AssetInfo> assetsList;
  17. // public List<BundleInfo> bundlsList;
  18. }
  19. [Serializable]
  20. public class AssetInfo
  21. {
  22. public string name;
  23. public string bundle;
  24. public string localPath;
  25. public bool isConfig;
  26. public LitJson.JsonData ToJson()
  27. {
  28. JsonData jsonData = new JsonData();
  29. jsonData["name"] = name;
  30. jsonData["bundle"] = bundle;
  31. jsonData["isConfig"] = isConfig;
  32. if (!string.IsNullOrEmpty(localPath))
  33. {
  34. jsonData["localPath"] = localPath;
  35. }
  36. return jsonData;
  37. }
  38. }
  39. [Serializable]
  40. public struct BundleInfo
  41. {
  42. // public string name;
  43. // public string MD5;
  44. // public long size;//byte
  45. //
  46. // public JsonData ToJson()
  47. // {
  48. // JsonData jsonData = new JsonData();
  49. // jsonData["name"] = name;
  50. // jsonData["MD5"] = MD5;
  51. // jsonData["size"] = size;
  52. // return jsonData;
  53. // }
  54. }
  55. public class AssetsPacker
  56. {
  57. private static char[] splits = new char[] { '/', '.' };
  58. public static string SpliteName(string name)
  59. {
  60. string[] splitName = name.Split(splits);
  61. if (splitName.Length > 2)
  62. {
  63. return splitName[splitName.Length - 2];
  64. }
  65. else
  66. {
  67. return "";
  68. }
  69. }
  70. // public static BundleInfo CreateBundleInfo(string name, List<string> dependence, string MD5Value)
  71. // {
  72. // BundleInfo bundle = new BundleInfo();
  73. // bundle.name = SpliteName(name);
  74. //
  75. // if (dependence != null)
  76. // {
  77. // dependence.ForEach(str => SpliteName(str));
  78. // }
  79. //
  80. // bundle.MD5 = MD5Value;
  81. // return bundle;
  82. // }
  83. #region Write Files
  84. /*
  85. private static XmlAttribute addXmlAttribute(XmlDocument doc, string name, string val)
  86. {
  87. XmlAttribute attr = doc.CreateAttribute(name);
  88. attr.Value = val;
  89. return attr;
  90. }
  91. private static XmlNode CreateNode(AssetInfo asset, XmlDocument doc)
  92. {
  93. XmlNode node = doc.CreateElement("AssetConfig");
  94. node.Attributes.Append(addXmlAttribute(doc, "AssetName", asset.name));
  95. node.Attributes.Append(addXmlAttribute(doc, "BundleName", asset.bundle));
  96. if (!string.IsNullOrEmpty(asset.localPath))
  97. {
  98. node.Attributes.Append(addXmlAttribute(doc, "AssetPath", asset.localPath));
  99. }
  100. return node;
  101. }
  102. private static XmlNode CreateNode(BundleInfo bundle, XmlDocument doc)
  103. {
  104. XmlNode node = doc.CreateElement("BundleConfig");
  105. node.Attributes.Append(addXmlAttribute(doc, "BundleName", bundle.name));
  106. node.Attributes.Append(addXmlAttribute(doc, "MD5", bundle.MD5));
  107. node.Attributes.Append(addXmlAttribute(doc, "Size", bundle.size.SplitToString()));
  108. // if (bundle.depedenceBundles != null)
  109. // {
  110. // node.Attributes.Append(addXmlAttribute(doc, "DependenceCount", bundle.depedenceBundles.Count.SplitToString()));
  111. // for (int i = 0; i < bundle.depedenceBundles.Count; i++)
  112. // {
  113. // node.Attributes.Append(addXmlAttribute(doc, "DependenceBundle" + i, bundle.depedenceBundles [i]));
  114. // }
  115. // }
  116. return node;
  117. }
  118. [ExecuteInEditMode]
  119. public static void WriteInfoToXMLFile(Dictionary<string, AssetInfo> assetList, Dictionary<string, BundleInfo> bundleList, string savePath)
  120. {
  121. if (null == assetList ||
  122. null == bundleList)
  123. {
  124. return;
  125. }
  126. XmlDocument doc = new XmlDocument();
  127. XmlDeclaration desc = doc.CreateXmlDeclaration("1.0", "utf-8", null);
  128. doc.AppendChild(desc);
  129. XmlElement root = doc.CreateElement("PackConfig");
  130. doc.AppendChild(root);
  131. XmlElement assetsRoot = doc.CreateElement("Assets");
  132. root.AppendChild(assetsRoot);
  133. foreach (var kvp in assetList)
  134. {
  135. XmlNode node = CreateNode(kvp.Value, doc);
  136. assetsRoot.AppendChild(node);
  137. }
  138. XmlElement bundlesRoot = doc.CreateElement("Bundles");
  139. root.AppendChild(bundlesRoot);
  140. foreach (var kvp in bundleList)
  141. {
  142. XmlNode node = CreateNode(kvp.Value, doc);
  143. bundlesRoot.AppendChild(node);
  144. }
  145. if (File.Exists(savePath))
  146. {
  147. File.Delete(savePath);
  148. }
  149. doc.Save(savePath);
  150. }
  151. */
  152. [ExecuteInEditMode]
  153. public static AllPackedAssetsInfo WriteInfoToJsonFile(Dictionary<string, AssetInfo> assetDict,
  154. string savePath)
  155. {
  156. AllPackedAssetsInfo allPackedInfo = new AllPackedAssetsInfo();
  157. allPackedInfo.assetsList = new List<AssetInfo>();
  158. allPackedInfo.assetsList.AddRange(assetDict.Values);
  159. // allPackedInfo.bundlsList = new List<BundleInfo>();
  160. // allPackedInfo.bundlsList.AddRange(bundleDict.Values);
  161. string content = JsonUtility.ToJson(allPackedInfo);
  162. if (File.Exists(savePath))
  163. {
  164. File.Delete(savePath);
  165. }
  166. Encoding encoding = new UTF8Encoding(false);
  167. File.WriteAllText(savePath, content, encoding);
  168. return allPackedInfo;
  169. // JsonData jsonRoot = new JsonData();
  170. // JsonData assetsRoot = new JsonData();
  171. // JsonData bundleRoot = new JsonData();
  172. // jsonRoot["Assets"] = assetsRoot;
  173. // jsonRoot["Bundles"] = bundleRoot;
  174. //
  175. // foreach (var kvp in assetDict)
  176. // {
  177. // assetsRoot[kvp.Value.name] = kvp.Value.ToJson();
  178. // }
  179. //
  180. // foreach (var kvp in bundleDict)
  181. // {
  182. // bundleRoot[kvp.Value.name] = kvp.Value.ToJson();
  183. // }
  184. //
  185. // if (File.Exists(savePath))
  186. // {
  187. // File.Delete(savePath);
  188. // }
  189. // Encoding encoding = new UTF8Encoding(false);
  190. // File.WriteAllText(savePath, jsonRoot.ToJson(), encoding);
  191. }
  192. #endregion
  193. }//AssetsPackInfo
  194. }
  195. #endif