AddressableAssetGroupInspector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.AddressableAssets.Settings;
  5. using UnityEngine;
  6. // ReSharper disable DelegateSubtraction
  7. namespace UnityEditor.AddressableAssets.GUI
  8. {
  9. [CustomEditor(typeof(AddressableAssetGroup)), CanEditMultipleObjects]
  10. class AddressableAssetGroupInspector : Editor
  11. {
  12. AddressableAssetGroup m_GroupTarget;
  13. List<Type> m_SchemaTypes;
  14. bool[] m_FoldoutState;
  15. // Used for Multi-group editing
  16. AddressableAssetGroup[] m_GroupTargets;
  17. // Stores a 2D list of schemas found on the other selected asset groups.
  18. // Each schema list contains only schemas of the same type (e.g. BundledAssetGroupSchema).
  19. List<List<AddressableAssetGroupSchema>> m_GroupSchemas;
  20. void OnEnable()
  21. {
  22. m_GroupTargets = new AddressableAssetGroup[targets.Length];
  23. for (int i = 0; i < targets.Length; i++)
  24. {
  25. m_GroupTargets[i] = targets[i] as AddressableAssetGroup;
  26. }
  27. // use item with largest index as base
  28. m_GroupTarget = m_GroupTargets[m_GroupTargets.Length - 1];
  29. if (m_GroupTarget != null)
  30. {
  31. m_GroupTarget.Settings.OnModification += OnSettingsModification;
  32. m_SchemaTypes = AddressableAssetUtility.GetTypes<AddressableAssetGroupSchema>();
  33. m_FoldoutState = new bool[m_GroupTarget.Schemas.Count];
  34. }
  35. for (int i = 0; i < m_FoldoutState.Length; i++)
  36. m_FoldoutState[i] = true;
  37. }
  38. void OnDisable()
  39. {
  40. if (m_GroupTarget != null)
  41. m_GroupTarget.Settings.OnModification -= OnSettingsModification;
  42. }
  43. void OnSettingsModification(AddressableAssetSettings settings, AddressableAssetSettings.ModificationEvent evnt, object o)
  44. {
  45. switch (evnt)
  46. {
  47. case AddressableAssetSettings.ModificationEvent.GroupAdded:
  48. case AddressableAssetSettings.ModificationEvent.GroupRemoved:
  49. case AddressableAssetSettings.ModificationEvent.GroupRenamed:
  50. case AddressableAssetSettings.ModificationEvent.BatchModification:
  51. case AddressableAssetSettings.ModificationEvent.ActiveProfileSet:
  52. case AddressableAssetSettings.ModificationEvent.GroupSchemaAdded:
  53. case AddressableAssetSettings.ModificationEvent.GroupSchemaModified:
  54. case AddressableAssetSettings.ModificationEvent.GroupSchemaRemoved:
  55. Repaint();
  56. break;
  57. }
  58. }
  59. void DrawDivider()
  60. {
  61. GUILayout.Space(1.5f);
  62. Rect r = EditorGUILayout.GetControlRect(GUILayout.Height(2.5f));
  63. r.x = 0;
  64. r.width = EditorGUIUtility.currentViewWidth;
  65. r.height = 1;
  66. Color color = new Color(0.6f, 0.6f, 0.6f, 1.333f);
  67. if (EditorGUIUtility.isProSkin)
  68. {
  69. color.r = 0.12f;
  70. color.g = 0.12f;
  71. color.b = 0.12f;
  72. }
  73. EditorGUI.DrawRect(r, color);
  74. }
  75. public override void OnInspectorGUI()
  76. {
  77. try
  78. {
  79. serializedObject.Update();
  80. DrawSchemas(GetSchemasToDraw());
  81. serializedObject.ApplyModifiedProperties();
  82. }
  83. catch (UnityEngine.ExitGUIException)
  84. {
  85. throw;
  86. }
  87. catch (Exception e)
  88. {
  89. Debug.LogException(e);
  90. }
  91. }
  92. List<AddressableAssetGroupSchema> GetSchemasToDraw()
  93. {
  94. List<AddressableAssetGroupSchema> values = new List<AddressableAssetGroupSchema>();
  95. if (m_GroupTargets == null || m_GroupTargets.Length == 0)
  96. return values;
  97. values.AddRange(m_GroupTarget.Schemas);
  98. foreach (var group in m_GroupTargets)
  99. {
  100. if (group != m_GroupTarget)
  101. values = values.Intersect(group.Schemas, new GroupSchemasCompare()).ToList();
  102. }
  103. return values;
  104. }
  105. List<AddressableAssetGroupSchema> GetSchemasForOtherTargets(AddressableAssetGroupSchema schema)
  106. {
  107. List<AddressableAssetGroupSchema> values = m_GroupTargets
  108. .Where(t => t.HasSchema(schema.GetType()) && t != m_GroupTarget)
  109. .Select(t => t.GetSchema(schema.GetType())).ToList();
  110. return values;
  111. }
  112. void DrawSchemas(List<AddressableAssetGroupSchema> schemas)
  113. {
  114. GUILayout.Space(6);
  115. EditorGUILayout.BeginHorizontal();
  116. var activeProfileName = m_GroupTarget.Settings.profileSettings.GetProfileName(m_GroupTarget.Settings.activeProfileId);
  117. if (string.IsNullOrEmpty(activeProfileName))
  118. {
  119. m_GroupTarget.Settings.activeProfileId = null; //this will reset it to default.
  120. activeProfileName = m_GroupTarget.Settings.profileSettings.GetProfileName(m_GroupTarget.Settings.activeProfileId);
  121. }
  122. EditorGUILayout.PrefixLabel("Active Profile: " + activeProfileName);
  123. if (GUILayout.Button("Inspect Top Level Settings"))
  124. {
  125. EditorGUIUtility.PingObject(AddressableAssetSettingsDefaultObject.Settings);
  126. Selection.activeObject = AddressableAssetSettingsDefaultObject.Settings;
  127. }
  128. EditorGUILayout.EndHorizontal();
  129. GUILayout.Space(6);
  130. if (m_FoldoutState == null || m_FoldoutState.Length != schemas.Count)
  131. m_FoldoutState = new bool[schemas.Count];
  132. EditorGUILayout.BeginVertical();
  133. for (int i = 0; i < schemas.Count; i++)
  134. {
  135. var schema = schemas[i];
  136. int currentIndex = i;
  137. DrawDivider();
  138. EditorGUILayout.BeginHorizontal();
  139. m_FoldoutState[i] = EditorGUILayout.Foldout(m_FoldoutState[i], AddressableAssetUtility.GetCachedTypeDisplayName(schema.GetType()));
  140. if (!m_GroupTarget.ReadOnly)
  141. {
  142. GUILayout.FlexibleSpace();
  143. GUIStyle gearIconStyle = UnityEngine.GUI.skin.FindStyle("IconButton") ?? EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).FindStyle("IconButton");
  144. if (EditorGUILayout.DropdownButton(EditorGUIUtility.IconContent("_Popup"), FocusType.Keyboard, gearIconStyle))
  145. {
  146. var menu = new GenericMenu();
  147. menu.AddItem(AddressableAssetGroup.RemoveSchemaContent, false, () =>
  148. {
  149. if (EditorUtility.DisplayDialog("Remove selected schema?", "Are you sure you want to remove " + AddressableAssetUtility.GetCachedTypeDisplayName(schema.GetType()) + " schema?\n\nYou cannot undo this action.", "Yes", "No"))
  150. {
  151. m_GroupTarget.RemoveSchema(schema.GetType());
  152. var newFoldoutstate = new bool[schemas.Count];
  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[i + 1];
  159. }
  160. m_FoldoutState = newFoldoutstate;
  161. }
  162. });
  163. menu.AddItem(AddressableAssetGroup.MoveSchemaUpContent, false, () =>
  164. {
  165. if (currentIndex > 0)
  166. {
  167. m_GroupTarget.Schemas[currentIndex] = m_GroupTarget.Schemas[currentIndex - 1];
  168. m_GroupTarget.Schemas[currentIndex - 1] = schema;
  169. return;
  170. }
  171. });
  172. menu.AddItem(AddressableAssetGroup.MoveSchemaDownContent, false, () =>
  173. {
  174. if (currentIndex < m_GroupTarget.Schemas.Count - 1)
  175. {
  176. m_GroupTarget.Schemas[currentIndex] = m_GroupTarget.Schemas[currentIndex + 1];
  177. m_GroupTarget.Schemas[currentIndex + 1] = schema;
  178. return;
  179. }
  180. });
  181. menu.AddSeparator("");
  182. menu.AddItem(AddressableAssetGroup.ExpandSchemaContent, false, () =>
  183. {
  184. m_FoldoutState[currentIndex] = true;
  185. foreach (var targetSchema in m_GroupTarget.Schemas)
  186. targetSchema.ShowAllProperties();
  187. });
  188. menu.ShowAsContext();
  189. }
  190. }
  191. EditorGUILayout.EndHorizontal();
  192. if (m_FoldoutState[i])
  193. {
  194. try
  195. {
  196. EditorGUI.indentLevel++;
  197. if (m_GroupTargets.Length == 1)
  198. schema.OnGUI();
  199. else
  200. schema.OnGUIMultiple(GetSchemasForOtherTargets(schema));
  201. EditorGUI.indentLevel--;
  202. }
  203. catch (Exception se)
  204. {
  205. Debug.LogException(se);
  206. }
  207. }
  208. }
  209. if (schemas.Count > 0)
  210. DrawDivider();
  211. GUILayout.Space(4);
  212. EditorGUILayout.BeginHorizontal();
  213. GUILayout.FlexibleSpace();
  214. GUIStyle addSchemaButton = new GUIStyle(UnityEngine.GUI.skin.button);
  215. addSchemaButton.fontSize = 12;
  216. addSchemaButton.fixedWidth = 225;
  217. addSchemaButton.fixedHeight = 22;
  218. if (!m_GroupTarget.ReadOnly)
  219. {
  220. if (EditorGUILayout.DropdownButton(new GUIContent("Add Schema", "Add new schema to this group."), FocusType.Keyboard, addSchemaButton))
  221. {
  222. var menu = new GenericMenu();
  223. for (int i = 0; i < m_SchemaTypes.Count; i++)
  224. {
  225. var type = m_SchemaTypes[i];
  226. if (m_GroupTarget.GetSchema(type) == null)
  227. {
  228. menu.AddItem(new GUIContent(AddressableAssetUtility.GetCachedTypeDisplayName(type), ""), false, () => OnAddSchema(type));
  229. }
  230. else
  231. {
  232. menu.AddDisabledItem(new GUIContent(AddressableAssetUtility.GetCachedTypeDisplayName(type), ""), true);
  233. }
  234. }
  235. menu.ShowAsContext();
  236. }
  237. }
  238. GUILayout.FlexibleSpace();
  239. EditorGUILayout.EndHorizontal();
  240. EditorGUILayout.EndVertical();
  241. }
  242. void OnAddSchema(Type schemaType, bool multiSelect = false)
  243. {
  244. if (targets.Length > 1)
  245. {
  246. foreach (var t in m_GroupTargets)
  247. if (!t.HasSchema(schemaType))
  248. t.AddSchema(schemaType);
  249. }
  250. else
  251. m_GroupTarget.AddSchema(schemaType);
  252. var newFoldoutState = new bool[m_GroupTarget.Schemas.Count];
  253. for (int i = 0; i < m_FoldoutState.Length; i++)
  254. newFoldoutState[i] = m_FoldoutState[i];
  255. m_FoldoutState = newFoldoutState;
  256. m_FoldoutState[m_FoldoutState.Length - 1] = true;
  257. }
  258. class GroupSchemasCompare : IEqualityComparer<AddressableAssetGroupSchema>
  259. {
  260. public bool Equals(AddressableAssetGroupSchema x, AddressableAssetGroupSchema y)
  261. {
  262. if (x.GetType() == y.GetType())
  263. return true;
  264. return false;
  265. }
  266. public int GetHashCode(AddressableAssetGroupSchema obj)
  267. {
  268. return obj.GetType().GetHashCode();
  269. }
  270. }
  271. }
  272. }