ABModelAssetInfo.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor.IMGUI.Controls;
  6. namespace AssetBundleBrowser.AssetBundleModel
  7. {
  8. internal sealed class AssetTreeItem : TreeViewItem
  9. {
  10. private AssetInfo m_asset;
  11. internal AssetInfo asset
  12. {
  13. get { return m_asset; }
  14. }
  15. internal AssetTreeItem() : base(-1, -1) { }
  16. internal AssetTreeItem(AssetInfo a) : base(a != null ? a.fullAssetName.GetHashCode() : Random.Range(int.MinValue, int.MaxValue), 0, a != null ? a.displayName : "failed")
  17. {
  18. m_asset = a;
  19. if (a != null)
  20. icon = AssetDatabase.GetCachedIcon(a.fullAssetName) as Texture2D;
  21. }
  22. private Color m_color = new Color(0, 0, 0, 0);
  23. internal Color itemColor
  24. {
  25. get
  26. {
  27. if (m_color.a == 0.0f && m_asset != null)
  28. {
  29. m_color = m_asset.GetColor();
  30. }
  31. return m_color;
  32. }
  33. set { m_color = value; }
  34. }
  35. internal Texture2D MessageIcon()
  36. {
  37. return MessageSystem.GetIcon(HighestMessageLevel());
  38. }
  39. internal MessageType HighestMessageLevel()
  40. {
  41. return m_asset != null ?
  42. m_asset.HighestMessageLevel() : MessageType.Error;
  43. }
  44. internal bool ContainsChild(AssetInfo asset)
  45. {
  46. bool contains = false;
  47. if (children == null)
  48. return contains;
  49. if (asset == null)
  50. return false;
  51. foreach (var child in children)
  52. {
  53. var c = child as AssetTreeItem;
  54. if (c != null && c.asset != null && c.asset.fullAssetName == asset.fullAssetName)
  55. {
  56. contains = true;
  57. break;
  58. }
  59. }
  60. return contains;
  61. }
  62. }
  63. internal class AssetInfo
  64. {
  65. internal bool isScene { get; set; }
  66. internal bool isFolder { get; set; }
  67. internal long fileSize;
  68. private HashSet<string> m_Parents;
  69. private string m_AssetName;
  70. private string m_DisplayName;
  71. private string m_BundleName;
  72. private MessageSystem.MessageState m_AssetMessages = new MessageSystem.MessageState();
  73. internal AssetInfo(string inName, string bundleName="")
  74. {
  75. fullAssetName = inName;
  76. m_BundleName = bundleName;
  77. m_Parents = new HashSet<string>();
  78. isScene = false;
  79. isFolder = false;
  80. }
  81. internal string fullAssetName
  82. {
  83. get { return m_AssetName; }
  84. set
  85. {
  86. m_AssetName = value;
  87. m_DisplayName = System.IO.Path.GetFileNameWithoutExtension(m_AssetName);
  88. //TODO - maybe there's a way to ask the AssetDatabase for this size info.
  89. System.IO.FileInfo fileInfo = new System.IO.FileInfo(m_AssetName);
  90. if (fileInfo.Exists)
  91. fileSize = fileInfo.Length;
  92. else
  93. fileSize = 0;
  94. }
  95. }
  96. internal string displayName
  97. {
  98. get { return m_DisplayName; }
  99. }
  100. internal string bundleName
  101. { get { return System.String.IsNullOrEmpty(m_BundleName) ? "auto" : m_BundleName; } }
  102. internal Color GetColor()
  103. {
  104. if (System.String.IsNullOrEmpty(m_BundleName))
  105. return Model.k_LightGrey;
  106. else
  107. return Color.white;
  108. }
  109. internal bool IsMessageSet(MessageSystem.MessageFlag flag)
  110. {
  111. return m_AssetMessages.IsSet(flag);
  112. }
  113. internal void SetMessageFlag(MessageSystem.MessageFlag flag, bool on)
  114. {
  115. m_AssetMessages.SetFlag(flag, on);
  116. }
  117. internal MessageType HighestMessageLevel()
  118. {
  119. return m_AssetMessages.HighestMessageLevel();
  120. }
  121. internal IEnumerable<MessageSystem.Message> GetMessages()
  122. {
  123. List<MessageSystem.Message> messages = new List<MessageSystem.Message>();
  124. if(IsMessageSet(MessageSystem.MessageFlag.SceneBundleConflict))
  125. {
  126. var message = displayName + "\n";
  127. if (isScene)
  128. message += "Is a scene that is in a bundle with non-scene assets. Scene bundles must have only one or more scene assets.";
  129. else
  130. message += "Is included in a bundle with a scene. Scene bundles must have only one or more scene assets.";
  131. messages.Add(new MessageSystem.Message(message, MessageType.Error));
  132. }
  133. if(IsMessageSet(MessageSystem.MessageFlag.DependencySceneConflict))
  134. {
  135. var message = displayName + "\n";
  136. message += MessageSystem.GetMessage(MessageSystem.MessageFlag.DependencySceneConflict).message;
  137. messages.Add(new MessageSystem.Message(message, MessageType.Error));
  138. }
  139. if (IsMessageSet(MessageSystem.MessageFlag.AssetsDuplicatedInMultBundles))
  140. {
  141. var bundleNames = AssetBundleModel.Model.CheckDependencyTracker(this);
  142. string message = displayName + "\n" + "Is auto-included in multiple bundles:\n";
  143. foreach(var bundleName in bundleNames)
  144. {
  145. message += bundleName + ", ";
  146. }
  147. message = message.Substring(0, message.Length - 2);//remove trailing comma.
  148. messages.Add(new MessageSystem.Message(message, MessageType.Warning));
  149. }
  150. if (System.String.IsNullOrEmpty(m_BundleName) && m_Parents.Count > 0)
  151. {
  152. //TODO - refine the parent list to only include those in the current asset list
  153. var message = displayName + "\n" + "Is auto included in bundle(s) due to parent(s): \n";
  154. foreach (var parent in m_Parents)
  155. {
  156. message += parent + ", ";
  157. }
  158. message = message.Substring(0, message.Length - 2);//remove trailing comma.
  159. messages.Add(new MessageSystem.Message(message, MessageType.Info));
  160. }
  161. if (m_dependencies != null && m_dependencies.Count > 0)
  162. {
  163. var message = string.Empty;
  164. var sortedDependencies = m_dependencies.OrderBy(d => d.bundleName);
  165. foreach (var dependent in sortedDependencies)
  166. {
  167. if (dependent.bundleName != bundleName)
  168. {
  169. message += dependent.bundleName + " : " + dependent.displayName + "\n";
  170. }
  171. }
  172. if (string.IsNullOrEmpty(message) == false)
  173. {
  174. message = message.Insert(0, displayName + "\n" + "Is dependent on other bundle's asset(s) or auto included asset(s): \n");
  175. message = message.Substring(0, message.Length - 1);//remove trailing line break.
  176. messages.Add(new MessageSystem.Message(message, MessageType.Info));
  177. }
  178. }
  179. messages.Add(new MessageSystem.Message(displayName + "\n" + "Path: " + fullAssetName, MessageType.Info));
  180. return messages;
  181. }
  182. internal void AddParent(string name)
  183. {
  184. m_Parents.Add(name);
  185. }
  186. internal void RemoveParent(string name)
  187. {
  188. m_Parents.Remove(name);
  189. }
  190. internal string GetSizeString()
  191. {
  192. if (fileSize == 0)
  193. return "--";
  194. return EditorUtility.FormatBytes(fileSize);
  195. }
  196. List<AssetInfo> m_dependencies = null;
  197. internal List<AssetInfo> GetDependencies()
  198. {
  199. //TODO - not sure this refreshes enough. need to build tests around that.
  200. if (m_dependencies == null)
  201. {
  202. m_dependencies = new List<AssetInfo>();
  203. if (AssetDatabase.IsValidFolder(m_AssetName))
  204. {
  205. //if we have a folder, its dependencies were already pulled in through alternate means. no need to GatherFoldersAndFiles
  206. //GatherFoldersAndFiles();
  207. }
  208. else
  209. {
  210. foreach (var dep in AssetDatabase.GetDependencies(m_AssetName, true))
  211. {
  212. if (dep != m_AssetName)
  213. {
  214. var asset = Model.CreateAsset(dep, this);
  215. if (asset != null)
  216. m_dependencies.Add(asset);
  217. }
  218. }
  219. }
  220. }
  221. return m_dependencies;
  222. }
  223. }
  224. }