AssetBundleBrowserMain.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. [assembly: System.Runtime.CompilerServices.InternalsVisibleToAttribute("Unity.AssetBundleBrowser.Editor.Tests")]
  5. namespace AssetBundleBrowser
  6. {
  7. public class AssetBundleBrowserMain : EditorWindow, IHasCustomMenu, ISerializationCallbackReceiver
  8. {
  9. private static AssetBundleBrowserMain s_instance = null;
  10. internal static AssetBundleBrowserMain instance
  11. {
  12. get
  13. {
  14. if (s_instance == null)
  15. s_instance = GetWindow<AssetBundleBrowserMain>();
  16. return s_instance;
  17. }
  18. }
  19. internal const float kButtonWidth = 150;
  20. enum Mode
  21. {
  22. Browser,
  23. Builder,
  24. Inspect,
  25. }
  26. [SerializeField]
  27. Mode m_Mode;
  28. [SerializeField]
  29. int m_DataSourceIndex;
  30. [SerializeField]
  31. internal AssetBundleManageTab m_ManageTab;
  32. [SerializeField]
  33. internal AssetBundleBuildTab m_BuildTab;
  34. [SerializeField]
  35. internal AssetBundleInspectTab m_InspectTab;
  36. private Texture2D m_RefreshTexture;
  37. const float k_ToolbarPadding = 15;
  38. const float k_MenubarPadding = 32;
  39. [MenuItem("Window/AssetBundle Browser", priority = 2050)]
  40. static void ShowWindow()
  41. {
  42. s_instance = null;
  43. instance.titleContent = new GUIContent("AssetBundles");
  44. instance.Show();
  45. }
  46. [SerializeField]
  47. internal bool multiDataSource = false;
  48. List<AssetBundleDataSource.ABDataSource> m_DataSourceList = null;
  49. public virtual void AddItemsToMenu(GenericMenu menu)
  50. {
  51. if(menu != null)
  52. menu.AddItem(new GUIContent("Custom Sources"), multiDataSource, FlipDataSource);
  53. }
  54. internal void FlipDataSource()
  55. {
  56. multiDataSource = !multiDataSource;
  57. }
  58. private void OnEnable()
  59. {
  60. Rect subPos = GetSubWindowArea();
  61. if(m_ManageTab == null)
  62. m_ManageTab = new AssetBundleManageTab();
  63. m_ManageTab.OnEnable(subPos, this);
  64. if(m_BuildTab == null)
  65. m_BuildTab = new AssetBundleBuildTab();
  66. m_BuildTab.OnEnable(this);
  67. if (m_InspectTab == null)
  68. m_InspectTab = new AssetBundleInspectTab();
  69. m_InspectTab.OnEnable(subPos);
  70. m_RefreshTexture = EditorGUIUtility.FindTexture("Refresh");
  71. InitDataSources();
  72. }
  73. private void InitDataSources()
  74. {
  75. //determine if we are "multi source" or not...
  76. multiDataSource = false;
  77. m_DataSourceList = new List<AssetBundleDataSource.ABDataSource>();
  78. foreach (var info in AssetBundleDataSource.ABDataSourceProviderUtility.CustomABDataSourceTypes)
  79. {
  80. m_DataSourceList.AddRange(info.GetMethod("CreateDataSources").Invoke(null, null) as List<AssetBundleDataSource.ABDataSource>);
  81. }
  82. if (m_DataSourceList.Count > 1)
  83. {
  84. multiDataSource = true;
  85. if (m_DataSourceIndex >= m_DataSourceList.Count)
  86. m_DataSourceIndex = 0;
  87. AssetBundleModel.Model.DataSource = m_DataSourceList[m_DataSourceIndex];
  88. }
  89. }
  90. private void OnDisable()
  91. {
  92. if (m_BuildTab != null)
  93. m_BuildTab.OnDisable();
  94. if (m_InspectTab != null)
  95. m_InspectTab.OnDisable();
  96. }
  97. public void OnBeforeSerialize()
  98. {
  99. }
  100. public void OnAfterDeserialize()
  101. {
  102. }
  103. private Rect GetSubWindowArea()
  104. {
  105. float padding = k_MenubarPadding;
  106. if (multiDataSource)
  107. padding += k_MenubarPadding * 0.5f;
  108. Rect subPos = new Rect(0, padding, position.width, position.height - padding);
  109. return subPos;
  110. }
  111. private void Update()
  112. {
  113. switch (m_Mode)
  114. {
  115. case Mode.Builder:
  116. break;
  117. case Mode.Inspect:
  118. break;
  119. case Mode.Browser:
  120. default:
  121. m_ManageTab.Update();
  122. break;
  123. }
  124. }
  125. private void OnGUI()
  126. {
  127. ModeToggle();
  128. switch(m_Mode)
  129. {
  130. case Mode.Builder:
  131. m_BuildTab.OnGUI();
  132. break;
  133. case Mode.Inspect:
  134. m_InspectTab.OnGUI(GetSubWindowArea());
  135. break;
  136. case Mode.Browser:
  137. default:
  138. m_ManageTab.OnGUI(GetSubWindowArea());
  139. break;
  140. }
  141. }
  142. void ModeToggle()
  143. {
  144. GUILayout.BeginHorizontal();
  145. GUILayout.Space(k_ToolbarPadding);
  146. bool clicked = false;
  147. switch(m_Mode)
  148. {
  149. case Mode.Browser:
  150. clicked = GUILayout.Button(m_RefreshTexture);
  151. if (clicked)
  152. m_ManageTab.ForceReloadData();
  153. break;
  154. case Mode.Builder:
  155. GUILayout.Space(m_RefreshTexture.width + k_ToolbarPadding);
  156. break;
  157. case Mode.Inspect:
  158. clicked = GUILayout.Button(m_RefreshTexture);
  159. if (clicked)
  160. m_InspectTab.RefreshBundles();
  161. break;
  162. }
  163. float toolbarWidth = position.width - k_ToolbarPadding * 4 - m_RefreshTexture.width;
  164. //string[] labels = new string[2] { "Configure", "Build"};
  165. string[] labels = new string[3] { "Configure", "Build", "Inspect" };
  166. m_Mode = (Mode)GUILayout.Toolbar((int)m_Mode, labels, "LargeButton", GUILayout.Width(toolbarWidth) );
  167. GUILayout.FlexibleSpace();
  168. GUILayout.EndHorizontal();
  169. if(multiDataSource)
  170. {
  171. //GUILayout.BeginArea(r);
  172. GUILayout.BeginHorizontal();
  173. using (new EditorGUILayout.HorizontalScope(EditorStyles.toolbar))
  174. {
  175. GUILayout.Label("Bundle Data Source:");
  176. GUILayout.FlexibleSpace();
  177. var c = new GUIContent(string.Format("{0} ({1})", AssetBundleModel.Model.DataSource.Name, AssetBundleModel.Model.DataSource.ProviderName), "Select Asset Bundle Set");
  178. if (GUILayout.Button(c , EditorStyles.toolbarPopup) )
  179. {
  180. GenericMenu menu = new GenericMenu();
  181. for (int index = 0; index < m_DataSourceList.Count; index++)
  182. {
  183. var ds = m_DataSourceList[index];
  184. if (ds == null)
  185. continue;
  186. if (index > 0)
  187. menu.AddSeparator("");
  188. var counter = index;
  189. menu.AddItem(new GUIContent(string.Format("{0} ({1})", ds.Name, ds.ProviderName)), false,
  190. () =>
  191. {
  192. m_DataSourceIndex = counter;
  193. var thisDataSource = ds;
  194. AssetBundleModel.Model.DataSource = thisDataSource;
  195. m_ManageTab.ForceReloadData();
  196. }
  197. );
  198. }
  199. menu.ShowAsContext();
  200. }
  201. GUILayout.FlexibleSpace();
  202. if (AssetBundleModel.Model.DataSource.IsReadOnly())
  203. {
  204. GUIStyle tbLabel = new GUIStyle(EditorStyles.toolbar);
  205. tbLabel.alignment = TextAnchor.MiddleRight;
  206. GUILayout.Label("Read Only", tbLabel);
  207. }
  208. }
  209. GUILayout.EndHorizontal();
  210. //GUILayout.EndArea();
  211. }
  212. }
  213. }
  214. }