AssetBundleBrowserMain.cs 8.0 KB

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