Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridPaintPaletteWindowPreferences.cs 9.8KB

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