AddressableAssetGroupTemplateInspector.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.AddressableAssets.Settings;
  4. using UnityEngine;
  5. // ReSharper disable DelegateSubtraction
  6. namespace UnityEditor.AddressableAssets.GUI
  7. {
  8. //[CustomEditor(typeof(AddressableAssetGroupTemplate)), CanEditMultipleObjects]
  9. [CustomEditor(typeof(AddressableAssetGroupTemplate))]
  10. class AddressableAssetGroupTemplateInspector : Editor
  11. {
  12. List<Type> m_SchemaTypes;
  13. bool[] m_FoldoutState;
  14. AddressableAssetGroupTemplate m_AddressableAssetGroupTarget;
  15. // // Used for Multi-group editing
  16. // AddressableAssetGroupTemplate[] m_AddressableAssetGroupTargets;
  17. // bool[] m_SchemaState;
  18. // int m_NumSchemasVisible = -1;
  19. // // Indicates whether not some schemas are hidden
  20. // bool m_HiddenSchemas = false;
  21. // Stores a 2D list of schemas found on the other selected asset groups.
  22. // Each schema list contains only schemas of the same type (e.g. BundledAssetGroupSchema).
  23. List<List<AddressableAssetGroupSchema>> m_GroupSchemas;
  24. void OnEnable()
  25. {
  26. // Single group editing
  27. if (targets.Length == 1)
  28. {
  29. m_AddressableAssetGroupTarget = target as AddressableAssetGroupTemplate;
  30. }
  31. // // Multi-group editing
  32. // if (targets.Length > 1)
  33. // {
  34. // m_AddressableAssetGroupTargets = new AddressableAssetGroupTemplate[targets.Length];
  35. // for (int i = 0; i < targets.Length; i++)
  36. // {
  37. // m_AddressableAssetGroupTargets[i] = targets[i] as AddressableAssetGroupTemplate;
  38. // }
  39. // // use item with largest index as base
  40. // m_AddressableAssetGroupTarget = m_AddressableAssetGroupTargets[m_AddressableAssetGroupTargets.Length - 1];
  41. // InitializeMultiSelectGroupSchemas();
  42. // }
  43. if (m_AddressableAssetGroupTarget != null)
  44. {
  45. m_SchemaTypes = AddressableAssetUtility.GetTypes<AddressableAssetGroupSchema>();
  46. m_FoldoutState = new bool[m_AddressableAssetGroupTarget.SchemaObjects.Count];
  47. }
  48. for (int i = 0; i < m_FoldoutState.Length; i++)
  49. m_FoldoutState[i] = true;
  50. }
  51. // void InitializeMultiSelectGroupSchemas()
  52. // {
  53. // var schemas = m_AddressableAssetGroupTarget.SchemaObjects;
  54. // if (schemas.Count == 0)
  55. // {
  56. // m_HiddenSchemas = false;
  57. // return;
  58. // }
  59. //
  60. // m_SchemaState = new bool[schemas.Count];
  61. // m_GroupSchemas = new List<List<AddressableAssetGroupSchema>>(schemas.Count);
  62. //
  63. // // For each m_GroupTarget schema, check if the other selected groups also have the same schema.
  64. // bool allGroupsHaveSchema;
  65. // for (int i = 0; i < schemas.Count; i++)
  66. // {
  67. // m_GroupSchemas.Add(new List<AddressableAssetGroupSchema>());
  68. // Type schema = schemas[i].GetType();
  69. //
  70. // allGroupsHaveSchema = true;
  71. // // Skip last group because it's the same group as m_GroupTarget
  72. // for (int j = 0; j < m_AddressableAssetGroupTargets.Length - 1; j++)
  73. // {
  74. // // Group has other schemas, which will not be shown because the m_GroupTarget doesn't have this schema
  75. // if (m_AddressableAssetGroupTargets[j].SchemaObjects.Count != schemas.Count)
  76. // m_HiddenSchemas = true;
  77. //
  78. // // Check if other group also has this schema
  79. // if (m_AddressableAssetGroupTargets[j].HasSchema(schema))
  80. // m_GroupSchemas[i].Add(m_AddressableAssetGroupTargets[j].GetSchemaByType(schema));
  81. // else
  82. // allGroupsHaveSchema = false;
  83. // }
  84. //
  85. // // All selected groups have this schema
  86. // if (allGroupsHaveSchema)
  87. // {
  88. // m_NumSchemasVisible++;
  89. // m_SchemaState[i] = true;
  90. // }
  91. // }
  92. // }
  93. void DrawDivider()
  94. {
  95. GUILayout.Space(1.5f);
  96. Rect r = EditorGUILayout.GetControlRect(GUILayout.Height(2.5f));
  97. r.height = 1;
  98. if (EditorGUIUtility.isProSkin)
  99. EditorGUI.DrawRect(r, Color.black);
  100. else
  101. EditorGUI.DrawRect(r, Color.gray);
  102. }
  103. public override void OnInspectorGUI()
  104. {
  105. try
  106. {
  107. serializedObject.Update();
  108. if (targets.Length == 1)
  109. {
  110. DrawSingleGroup();
  111. }
  112. // else if (targets.Length > 1)
  113. // {
  114. // DrawMultipleGroups();
  115. // }
  116. serializedObject.ApplyModifiedProperties();
  117. }
  118. catch (UnityEngine.ExitGUIException)
  119. {
  120. throw;
  121. }
  122. catch (Exception e)
  123. {
  124. Debug.LogException(e);
  125. }
  126. }
  127. void DrawSingleGroup()
  128. {
  129. EditorGUILayout.LabelField("Group Template Description");
  130. m_AddressableAssetGroupTarget.Description = EditorGUILayout.TextArea(m_AddressableAssetGroupTarget.Description);
  131. int objectCount = m_AddressableAssetGroupTarget.SchemaObjects.Count;
  132. if (m_FoldoutState == null || m_FoldoutState.Length != objectCount)
  133. m_FoldoutState = new bool[objectCount];
  134. for (int i = 0; i < objectCount; i++)
  135. {
  136. var schema = m_AddressableAssetGroupTarget.SchemaObjects[i];
  137. int currentIndex = i;
  138. DrawDivider();
  139. EditorGUILayout.BeginHorizontal();
  140. m_FoldoutState[i] = EditorGUILayout.Foldout(m_FoldoutState[i], AddressableAssetUtility.GetCachedTypeDisplayName(m_AddressableAssetGroupTarget.SchemaObjects[i].GetType()));
  141. GUILayout.FlexibleSpace();
  142. GUIStyle gearIconStyle = UnityEngine.GUI.skin.FindStyle("IconButton") ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("IconButton");
  143. if (EditorGUILayout.DropdownButton(EditorGUIUtility.IconContent("_Popup"), FocusType.Keyboard, gearIconStyle))
  144. {
  145. var menu = new GenericMenu();
  146. menu.AddItem(AddressableAssetGroup.RemoveSchemaContent, false, () =>
  147. {
  148. var schemaName = AddressableAssetUtility.GetCachedTypeDisplayName(m_AddressableAssetGroupTarget.SchemaObjects[currentIndex].GetType());
  149. if (EditorUtility.DisplayDialog("Remove selected schema?", "Are you sure you want to remove " + schemaName + " schema?\n\nYou cannot undo this action.", "Yes", "No"))
  150. {
  151. m_AddressableAssetGroupTarget.RemoveSchema(currentIndex);
  152. var newFoldoutstate = new bool[objectCount - 1];
  153. for (int j = 0; j < newFoldoutstate.Length; j++)
  154. {
  155. if (j < i)
  156. newFoldoutstate[j] = m_FoldoutState[j];
  157. else
  158. newFoldoutstate[j] = m_FoldoutState[currentIndex + 1];
  159. }
  160. m_FoldoutState = newFoldoutstate;
  161. }
  162. });
  163. menu.AddItem(AddressableAssetGroup.MoveSchemaUpContent, false, () =>
  164. {
  165. if (currentIndex > 0)
  166. {
  167. m_AddressableAssetGroupTarget.SchemaObjects[currentIndex] = m_AddressableAssetGroupTarget.SchemaObjects[currentIndex - 1];
  168. m_AddressableAssetGroupTarget.SchemaObjects[currentIndex - 1] = schema;
  169. return;
  170. }
  171. });
  172. menu.AddItem(AddressableAssetGroup.MoveSchemaDownContent, false, () =>
  173. {
  174. if (currentIndex < m_AddressableAssetGroupTarget.SchemaObjects.Count - 1)
  175. {
  176. m_AddressableAssetGroupTarget.SchemaObjects[currentIndex] = m_AddressableAssetGroupTarget.SchemaObjects[currentIndex + 1];
  177. m_AddressableAssetGroupTarget.SchemaObjects[currentIndex + 1] = schema;
  178. return;
  179. }
  180. });
  181. menu.AddSeparator("");
  182. menu.AddItem(AddressableAssetGroup.ExpandSchemaContent, false, () =>
  183. {
  184. m_FoldoutState[currentIndex] = true;
  185. m_AddressableAssetGroupTarget.SchemaObjects[currentIndex].ShowAllProperties();
  186. });
  187. menu.ShowAsContext();
  188. }
  189. EditorGUILayout.EndHorizontal();
  190. if (m_FoldoutState[i])
  191. {
  192. try
  193. {
  194. EditorGUI.indentLevel++;
  195. m_AddressableAssetGroupTarget.SchemaObjects[i].OnGUI();
  196. EditorGUI.indentLevel--;
  197. }
  198. catch (Exception se)
  199. {
  200. Debug.LogException(se);
  201. }
  202. }
  203. }
  204. DrawDivider();
  205. EditorGUILayout.BeginHorizontal();
  206. GUILayout.FlexibleSpace();
  207. GUIStyle addSchemaButton = new GUIStyle(UnityEngine.GUI.skin.button);
  208. addSchemaButton.fontSize = 12;
  209. addSchemaButton.fixedWidth = 225;
  210. addSchemaButton.fixedHeight = 22;
  211. if (EditorGUILayout.DropdownButton(new GUIContent("Add Schema", "Add new schema to this group."), FocusType.Keyboard, addSchemaButton))
  212. {
  213. var menu = new GenericMenu();
  214. for (int i = 0; i < m_SchemaTypes.Count; i++)
  215. {
  216. var type = m_SchemaTypes[i];
  217. if (Array.IndexOf(m_AddressableAssetGroupTarget.GetTypes(), type) == -1)
  218. {
  219. menu.AddItem(new GUIContent(AddressableAssetUtility.GetCachedTypeDisplayName(type), ""), false, () => OnAddSchema(type));
  220. }
  221. else
  222. {
  223. menu.AddDisabledItem(new GUIContent(AddressableAssetUtility.GetCachedTypeDisplayName(type), ""), true);
  224. }
  225. }
  226. menu.ShowAsContext();
  227. }
  228. GUILayout.FlexibleSpace();
  229. EditorGUILayout.EndHorizontal();
  230. }
  231. // void DrawMultipleGroups()
  232. // {
  233. // // Group Template Description
  234. // EditorGUILayout.LabelField("Group Template Description");
  235. // for(int i = 0; i < m_AddressableAssetGroupTargets.Length - 1; i++)
  236. // {
  237. // if (m_AddressableAssetGroupTargets[i].Description != m_AddressableAssetGroupTarget.Description)
  238. // {
  239. // EditorGUI.showMixedValue = true;
  240. // break;
  241. // }
  242. // }
  243. // EditorGUI.BeginChangeCheck();
  244. // m_AddressableAssetGroupTarget.Description = EditorGUILayout.TextArea(m_AddressableAssetGroupTarget.Description);
  245. // EditorGUI.showMixedValue = false;
  246. // if (EditorGUI.EndChangeCheck())
  247. // {
  248. // for (int i = 0; i < m_AddressableAssetGroupTargets.Length - 1; i++)
  249. // {
  250. // m_AddressableAssetGroupTargets[i].Description = m_AddressableAssetGroupTarget.Description;
  251. // }
  252. // }
  253. //
  254. // // Schemas
  255. // int objectCount = m_AddressableAssetGroupTarget.SchemaObjects.Count;
  256. // if (m_FoldoutState == null || m_FoldoutState.Length != objectCount)
  257. // m_FoldoutState = new bool[objectCount];
  258. //
  259. // for (int i = 0; i < objectCount; i++)
  260. // {
  261. // if (!m_SchemaState[i]) continue;
  262. //
  263. // var schema = m_AddressableAssetGroupTarget.SchemaObjects[i];
  264. // int currentIndex = i;
  265. //
  266. // DrawDivider();
  267. // EditorGUILayout.BeginHorizontal();
  268. // m_FoldoutState[i] = EditorGUILayout.Foldout(m_FoldoutState[i], m_AddressableAssetGroupTarget.SchemaObjects[i].DisplayName());
  269. //
  270. // GUILayout.FlexibleSpace();
  271. // GUIStyle gearIconStyle = UnityEngine.GUI.skin.FindStyle("IconButton") ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("IconButton");
  272. //
  273. // if (EditorGUILayout.DropdownButton(EditorGUIUtility.IconContent("_Popup"), FocusType.Keyboard, gearIconStyle))
  274. // {
  275. // var menu = new GenericMenu();
  276. // menu.AddItem(AddressableAssetGroup.RemoveSchemaContent, false, () =>
  277. // {
  278. // var schemaName = m_AddressableAssetGroupTarget.SchemaObjects[currentIndex].DisplayName();
  279. // if (EditorUtility.DisplayDialog("Remove selected schema?", "Are you sure you want to remove " + schemaName + " schema?\n\nYou cannot undo this action.", "Yes", "No"))
  280. // {
  281. // Type schemaType = m_AddressableAssetGroupTarget.SchemaObjects[currentIndex].GetType();
  282. // m_AddressableAssetGroupTarget.RemoveSchema(currentIndex);
  283. // for (int j = 0; j < m_AddressableAssetGroupTargets.Length - 1; j++)
  284. // {
  285. // int removeIndex = m_AddressableAssetGroupTargets[j].FindSchema(schemaType);
  286. // m_AddressableAssetGroupTargets[j].RemoveSchema(removeIndex);
  287. // }
  288. //
  289. // InitializeMultiSelectGroupSchemas();
  290. //
  291. // var newFoldoutstate = new bool[objectCount - 1];
  292. // for (int j = 0; j < newFoldoutstate.Length; j++)
  293. // {
  294. // if (j < i)
  295. // newFoldoutstate[j] = m_FoldoutState[j];
  296. // else
  297. // newFoldoutstate[j] = m_FoldoutState[currentIndex + 1];
  298. // }
  299. //
  300. // m_FoldoutState = newFoldoutstate;
  301. // return;
  302. // }
  303. // });
  304. // menu.AddItem(AddressableAssetGroup.MoveSchemaUpContent, false, () =>
  305. // {
  306. // foreach (var group in m_AddressableAssetGroupTargets)
  307. // {
  308. // int index = group.FindSchema(schema.GetType());
  309. // if (index > 0)
  310. // {
  311. // var temp = group.SchemaObjects[index];
  312. // group.SchemaObjects[index] = group.SchemaObjects[index - 1];
  313. // group.SchemaObjects[index - 1] = temp;
  314. // }
  315. // }
  316. // InitializeMultiSelectGroupSchemas();
  317. // return;
  318. // });
  319. // menu.AddItem(AddressableAssetGroup.MoveSchemaDownContent, false, () =>
  320. // {
  321. // foreach (var group in m_AddressableAssetGroupTargets)
  322. // {
  323. // int index = group.FindSchema(schema.GetType());
  324. // if (index >= 0 && index < group.SchemaObjects.Count - 1)
  325. // {
  326. // var temp = group.SchemaObjects[index];
  327. // group.SchemaObjects[index] = group.SchemaObjects[index + 1];
  328. // group.SchemaObjects[index + 1] = temp;
  329. // }
  330. // }
  331. // InitializeMultiSelectGroupSchemas();
  332. // return;
  333. // });
  334. // menu.AddSeparator("");
  335. // menu.AddItem(AddressableAssetGroup.ExpandSchemaContent, false, () =>
  336. // {
  337. // m_FoldoutState[currentIndex] = true;
  338. // foreach (var group in m_AddressableAssetGroupTargets)
  339. // {
  340. // int index = group.FindSchema(schema.GetType());
  341. // if (index != -1)
  342. // {
  343. // group.SchemaObjects[index].ShowAllProperties();
  344. // }
  345. // }
  346. // });
  347. // menu.ShowAsContext();
  348. // }
  349. //
  350. // EditorGUILayout.EndHorizontal();
  351. //
  352. // if (m_FoldoutState[i])
  353. // {
  354. // try
  355. // {
  356. // EditorGUI.indentLevel++;
  357. // m_AddressableAssetGroupTarget.SchemaObjects[i].OnGUIMultiple(m_GroupSchemas[i]);
  358. // EditorGUI.indentLevel--;
  359. // }
  360. // catch (Exception se)
  361. // {
  362. // Debug.LogException(se);
  363. // }
  364. // }
  365. // }
  366. //
  367. // if (m_HiddenSchemas)
  368. // {
  369. // DrawDivider();
  370. // EditorGUILayout.HelpBox(new GUIContent("Only schemas that are on all selected groups can be multi-edited."));
  371. // }
  372. //
  373. // DrawDivider();
  374. // EditorGUILayout.BeginHorizontal();
  375. // GUILayout.FlexibleSpace();
  376. // GUIStyle addSchemaButton = new GUIStyle(UnityEngine.GUI.skin.button);
  377. // addSchemaButton.fontSize = 12;
  378. // addSchemaButton.fixedWidth = 225;
  379. // addSchemaButton.fixedHeight = 22;
  380. //
  381. // if (EditorGUILayout.DropdownButton(new GUIContent("Add Schema", "Add new schema to this group."), FocusType.Keyboard, addSchemaButton))
  382. // {
  383. // var menu = new GenericMenu();
  384. // for (int i = 0; i < m_SchemaTypes.Count; i++)
  385. // {
  386. // var type = m_SchemaTypes[i];
  387. // var schema = (AddressableAssetGroupSchema)CreateInstance(type);
  388. //
  389. // bool allGroupsDoNotHave = true;
  390. // foreach (var group in m_AddressableAssetGroupTargets)
  391. // {
  392. // if (group.HasSchema(type))
  393. // allGroupsDoNotHave = false;
  394. // }
  395. //
  396. // if (allGroupsDoNotHave)
  397. // {
  398. // menu.AddItem(new GUIContent(schema.DisplayName(), ""), false, () =>
  399. // {
  400. // OnAddSchema(type, true);
  401. // return;
  402. // });
  403. // }
  404. // else
  405. // {
  406. // menu.AddDisabledItem(new GUIContent(schema.DisplayName(), ""), true);
  407. // }
  408. // }
  409. //
  410. // menu.ShowAsContext();
  411. // }
  412. //
  413. // GUILayout.FlexibleSpace();
  414. // EditorGUILayout.EndHorizontal();
  415. // }
  416. void OnAddSchema(Type schemaType, bool multiSelect = false)
  417. {
  418. if (!m_AddressableAssetGroupTarget.AddSchema(schemaType))
  419. return;
  420. // if (multiSelect)
  421. // {
  422. // for (int i = 0; i < m_AddressableAssetGroupTargets.Length - 1; i++)
  423. // {
  424. // if(!m_AddressableAssetGroupTargets[i].AddSchema(schemaType))
  425. // return;
  426. // }
  427. // InitializeMultiSelectGroupSchemas();
  428. // }
  429. var newFoldoutState = new bool[m_AddressableAssetGroupTarget.SchemaObjects.Count];
  430. for (int i = 0; i < m_FoldoutState.Length; i++)
  431. newFoldoutState[i] = m_FoldoutState[i];
  432. m_FoldoutState = newFoldoutState;
  433. m_FoldoutState[m_FoldoutState.Length - 1] = true;
  434. }
  435. }
  436. }