GridPaintPaletteWindowPreferences.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. internal class GridPaintPaletteWindowPreferences
  8. {
  9. [SettingsProvider]
  10. internal static SettingsProvider CreateSettingsProvider()
  11. {
  12. var settingsProvider = new SettingsProvider("Preferences/2D/Tile Palette", SettingsScope.User, SettingsProvider.GetSearchKeywordsFromGUIContentProperties<GridPaintPaletteWindowPreferences>())
  13. {
  14. guiHandler = searchContext =>
  15. {
  16. GridPaintPaletteWindow.PreferencesGUI();
  17. GridPaintActiveTargetsPreferences.PreferencesGUI();
  18. SceneViewOpenTilePaletteHelper.PreferencesGUI();
  19. TilemapPrefabStageHelper.PreferencesGUI();
  20. }
  21. };
  22. return settingsProvider;
  23. }
  24. }
  25. internal class GridPaintActiveTargetsPreferences
  26. {
  27. public static readonly string targetSortingModeEditorPref = "TilePalette.ActiveTargetsSortingMode";
  28. public static readonly string targetSortingModeLookup = "Active Targets Sorting Mode";
  29. public static readonly string targetRestoreEditModeSelectionEditorPref = "TilePalette.RestoreEditModeSelection";
  30. public static readonly string targetRestoreEditModeSelectionLookup = "Restore Edit Mode Active Target";
  31. public static readonly string createTileFromPaletteEditorPref = "TilePalette.CreateTileFromPalette";
  32. public static readonly string createTileFromPaletteLookup = "Create Tile Method";
  33. public static readonly string defaultSortingMode = L10n.Tr("None");
  34. public static readonly GUIContent targetSortingModeLabel =
  35. EditorGUIUtility.TrTextContent(targetSortingModeLookup,
  36. "Controls the sorting of the Active Targets in the Tile Palette");
  37. public static readonly GUIContent targetRestoreEditModeSelectionLabel = EditorGUIUtility.TrTextContent(
  38. targetRestoreEditModeSelectionLookup
  39. , "When exiting Play Mode, restores the Active Target in the Tile Palette to the last selected target from Edit Mode");
  40. public static readonly GUIContent createTileFromPaletteLabel = EditorGUIUtility.TrTextContent(
  41. createTileFromPaletteLookup
  42. , "Method used to create Tiles when drag and dropping assets to the Tile Palette");
  43. public static bool restoreEditModeSelection
  44. {
  45. get { return EditorPrefs.GetBool(targetRestoreEditModeSelectionEditorPref, true); }
  46. set { EditorPrefs.SetBool(targetRestoreEditModeSelectionEditorPref, value); }
  47. }
  48. private static string[] s_SortingNames;
  49. private static int s_SortingSelectionIndex;
  50. private static string[] s_CreateTileNames;
  51. private static int s_CreateTileIndex;
  52. private static bool CompareMethodName(string[] methodNames, MethodInfo method)
  53. {
  54. return methodNames.Length == 2 && methodNames[0] == method.ReflectedType.Name &&
  55. methodNames[1] == method.Name;
  56. }
  57. private static bool CompareTypeName(string typeFullName, Type type)
  58. {
  59. return typeFullName == type.FullName;
  60. }
  61. internal static void PreferencesGUI()
  62. {
  63. using (new SettingsWindow.GUIScope())
  64. {
  65. if (s_SortingNames == null)
  66. {
  67. var sortingTypeFullName = EditorPrefs.GetString(targetSortingModeEditorPref, defaultSortingMode);
  68. var sortingMethodNames = sortingTypeFullName.Split('.');
  69. s_SortingNames = new string[1 + GridPaintSortingAttribute.sortingMethods.Count +
  70. GridPaintSortingAttribute.sortingTypes.Count];
  71. int count = 0;
  72. s_SortingNames[count++] = defaultSortingMode;
  73. foreach (var sortingMethod in GridPaintSortingAttribute.sortingMethods)
  74. {
  75. if (CompareMethodName(sortingMethodNames, sortingMethod))
  76. s_SortingSelectionIndex = count;
  77. s_SortingNames[count++] = sortingMethod.Name;
  78. }
  79. foreach (var sortingType in GridPaintSortingAttribute.sortingTypes)
  80. {
  81. if (CompareTypeName(sortingTypeFullName, sortingType))
  82. s_SortingSelectionIndex = count;
  83. s_SortingNames[count++] = sortingType.Name;
  84. }
  85. }
  86. if (s_CreateTileNames == null)
  87. {
  88. var createTileFullName = EditorPrefs.GetString(createTileFromPaletteEditorPref, defaultSortingMode);
  89. var createTileMethodNames = createTileFullName.Split('.');
  90. int count = 0;
  91. s_CreateTileNames = new string[CreateTileFromPaletteAttribute.createTileFromPaletteMethods.Count];
  92. foreach (var createTileMethod in CreateTileFromPaletteAttribute.createTileFromPaletteMethods)
  93. {
  94. if (CompareMethodName(createTileMethodNames, createTileMethod))
  95. s_CreateTileIndex = count;
  96. s_CreateTileNames[count++] = createTileMethod.Name;
  97. }
  98. }
  99. EditorGUI.BeginChangeCheck();
  100. var sortingSelection =
  101. EditorGUILayout.Popup(targetSortingModeLabel, s_SortingSelectionIndex, s_SortingNames);
  102. if (EditorGUI.EndChangeCheck())
  103. {
  104. s_SortingSelectionIndex = sortingSelection;
  105. var sortingTypeFullName = defaultSortingMode;
  106. if (s_SortingSelectionIndex > 0 &&
  107. s_SortingSelectionIndex <= GridPaintSortingAttribute.sortingMethods.Count)
  108. {
  109. var sortingMethod = GridPaintSortingAttribute.sortingMethods[s_SortingSelectionIndex - 1];
  110. sortingTypeFullName =
  111. String.Format("{0}.{1}", sortingMethod.ReflectedType.Name, sortingMethod.Name);
  112. }
  113. else
  114. {
  115. var idx = s_SortingSelectionIndex - GridPaintSortingAttribute.sortingMethods.Count - 1;
  116. if (idx >= 0 && idx < GridPaintSortingAttribute.sortingTypes.Count)
  117. {
  118. var sortingType = GridPaintSortingAttribute.sortingTypes[idx];
  119. sortingTypeFullName = sortingType.FullName;
  120. }
  121. }
  122. EditorPrefs.SetString(targetSortingModeEditorPref, sortingTypeFullName);
  123. GridPaintingState.FlushCache();
  124. }
  125. EditorGUI.BeginChangeCheck();
  126. var editModeSelection = EditorGUILayout.Toggle(targetRestoreEditModeSelectionLabel, restoreEditModeSelection);
  127. if (EditorGUI.EndChangeCheck())
  128. {
  129. restoreEditModeSelection = editModeSelection;
  130. }
  131. EditorGUI.BeginChangeCheck();
  132. var createTileSelection = EditorGUILayout.Popup(createTileFromPaletteLabel, s_CreateTileIndex, s_CreateTileNames);
  133. if (EditorGUI.EndChangeCheck())
  134. {
  135. var createTileFullName = defaultSortingMode;
  136. s_CreateTileIndex = createTileSelection;
  137. if (s_CreateTileIndex < CreateTileFromPaletteAttribute.createTileFromPaletteMethods.Count)
  138. {
  139. var createTileMethod = CreateTileFromPaletteAttribute.createTileFromPaletteMethods[s_CreateTileIndex];
  140. createTileFullName = String.Format("{0}.{1}", createTileMethod.ReflectedType.Name, createTileMethod.Name);
  141. }
  142. EditorPrefs.SetString(createTileFromPaletteEditorPref, createTileFullName);
  143. }
  144. }
  145. }
  146. public static IComparer<GameObject> GetTargetComparer()
  147. {
  148. var sortingTypeFullName = EditorPrefs.GetString(targetSortingModeEditorPref, defaultSortingMode);
  149. if (!sortingTypeFullName.Equals(defaultSortingMode))
  150. {
  151. var sortingMethodNames = sortingTypeFullName.Split('.');
  152. foreach (var sortingMethod in GridPaintSortingAttribute.sortingMethods)
  153. {
  154. if (CompareMethodName(sortingMethodNames, sortingMethod))
  155. return sortingMethod.Invoke(null, null) as IComparer<GameObject>;
  156. }
  157. foreach (var sortingType in GridPaintSortingAttribute.sortingTypes)
  158. {
  159. if (CompareTypeName(sortingTypeFullName, sortingType))
  160. return Activator.CreateInstance(sortingType) as IComparer<GameObject>;
  161. }
  162. }
  163. return null;
  164. }
  165. public static MethodInfo GetCreateTileFromPaletteUsingPreferences()
  166. {
  167. var createTileFullName = EditorPrefs.GetString(createTileFromPaletteEditorPref, defaultSortingMode);
  168. if (!createTileFullName.Equals(defaultSortingMode))
  169. {
  170. var methodNames = createTileFullName.Split('.');
  171. foreach (var createTileMethod in CreateTileFromPaletteAttribute.createTileFromPaletteMethods)
  172. {
  173. if (CompareMethodName(methodNames, createTileMethod))
  174. return createTileMethod;
  175. }
  176. }
  177. return typeof(TileUtility).GetMethod("DefaultTile", BindingFlags.Static | BindingFlags.Public);
  178. }
  179. }
  180. }