Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

GridBrushPickStoreSettings.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. internal class GridBrushPickStoreSettings : ScriptableObject
  8. {
  9. [SerializeField]
  10. public GridBrushPickStore m_UserPickStore;
  11. }
  12. [Serializable]
  13. internal class GridBrushPickStoreSettingsProvider : ScriptableSingleton<GridBrushPickStoreSettingsProvider>
  14. {
  15. private const string kSettingsAssetPath = "ProjectSettings/TilePaletteBrushPicks.asset";
  16. internal class SettingsProperties
  17. {
  18. public static GUIStyle header = null;
  19. public static readonly GUIContent brushPicksLabel = EditorGUIUtility.TrTextContent("Brush Picks");
  20. public static readonly GUIContent brushPicksAssetLabel = EditorGUIUtility.TrTextContent("Brush Picks Asset", "Asset for storing Brush Picks for the Project.");
  21. public static readonly GUIContent createNewLabel = EditorGUIUtility.TrTextContent("New", "Creates a new Brush Picks Asset in the Project.");
  22. public static readonly GUIContent createCloneLabel = EditorGUIUtility.TrTextContent("Clone", "Creates a new Brush Picks Asset in the Project from the current Brush Picks Asset (From default if there is none set).");
  23. }
  24. [SerializeField]
  25. private GridBrushPickStore m_PickStorePreference;
  26. [SerializeField]
  27. private GridBrushPickStoreSettings m_PickStoreSettings;
  28. private void OnEnable()
  29. {
  30. LoadGridBrushPickStoreSettings();
  31. }
  32. private void LoadGridBrushPickStoreSettings()
  33. {
  34. var objs = InternalEditorUtility.LoadSerializedFileAndForget(kSettingsAssetPath);
  35. if (objs != null && objs.Length > 0)
  36. {
  37. m_PickStoreSettings = objs[0] as GridBrushPickStoreSettings;
  38. m_PickStorePreference = m_PickStoreSettings != null ? m_PickStoreSettings.m_UserPickStore : null;
  39. }
  40. }
  41. internal static GridBrushPickStore GetUserBrushPickStore()
  42. {
  43. var settings = GetGridBrushPickStoreSettings();
  44. if (settings != null)
  45. return settings.m_UserPickStore;
  46. return null;
  47. }
  48. internal static GridBrushPickStoreSettings GetGridBrushPickStoreSettings()
  49. {
  50. if (instance.m_PickStoreSettings == null && instance.m_PickStoreSettings is not null)
  51. {
  52. instance.LoadGridBrushPickStoreSettings();
  53. }
  54. return instance.m_PickStoreSettings;
  55. }
  56. private static GridBrushPickStoreSettings LoadOrCreateGridBrushPickStoreSettings()
  57. {
  58. var settings = GetGridBrushPickStoreSettings();
  59. if (settings == null)
  60. {
  61. instance.m_PickStoreSettings = ScriptableObject.CreateInstance<GridBrushPickStoreSettings>();
  62. InternalEditorUtility.SaveToSerializedFileAndForget(new [] { instance.m_PickStoreSettings }, kSettingsAssetPath, EditorSettings.serializationMode != SerializationMode.ForceBinary);
  63. settings = instance.m_PickStoreSettings;
  64. }
  65. return settings;
  66. }
  67. internal static void PreferencesGUI()
  68. {
  69. using (new SettingsWindow.GUIScope())
  70. {
  71. if (SettingsProperties.header == null)
  72. SettingsProperties.header = "SettingsHeader";
  73. GUILayout.Label(SettingsProperties.brushPicksLabel, SettingsProperties.header, GUILayout.MinWidth(160));
  74. EditorGUILayout.BeginHorizontal();
  75. var pickStorePreference = (GridBrushPickStore) EditorGUILayout.ObjectField(SettingsProperties.brushPicksAssetLabel, instance.m_PickStorePreference, typeof(GridBrushPickStore), false);
  76. if (pickStorePreference != instance.m_PickStorePreference)
  77. {
  78. instance.m_PickStorePreference = pickStorePreference;
  79. var settings = LoadOrCreateGridBrushPickStoreSettings();
  80. settings.m_UserPickStore = instance.m_PickStorePreference;
  81. Apply();
  82. }
  83. if (GUILayout.Button(SettingsProperties.createNewLabel, GUILayout.Width(55)))
  84. {
  85. CreateNew();
  86. }
  87. if (GUILayout.Button(SettingsProperties.createCloneLabel, GUILayout.Width(55)))
  88. {
  89. CreateClone();
  90. }
  91. EditorGUILayout.EndHorizontal();
  92. EditorGUILayout.Space();
  93. }
  94. }
  95. private static void Apply()
  96. {
  97. if (instance.m_PickStoreSettings == null)
  98. return;
  99. InternalEditorUtility.SaveToSerializedFileAndForget(new [] { instance.m_PickStoreSettings }, kSettingsAssetPath, EditorSettings.serializationMode != SerializationMode.ForceBinary);
  100. GridPaletteBrushes.FlushCache();
  101. GridPaintingState.brushPickStore = GetUserBrushPickStore();
  102. }
  103. private static void CreateNew()
  104. {
  105. var brushStore = ScriptableObject.CreateInstance<GridBrushPickStore>();
  106. var defaultPath = ProjectBrowser.s_LastInteractedProjectBrowser ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath() : "Assets";
  107. var filePath = EditorUtility.SaveFilePanel("Create Brush Picks Asset into folder ", defaultPath, "BrushPick", "asset");
  108. if (string.IsNullOrEmpty(filePath))
  109. return;
  110. filePath = FileUtil.GetProjectRelativePath(filePath);
  111. var fileName = FileUtil.UnityGetFileNameWithoutExtension(filePath);
  112. brushStore.name = fileName;
  113. AssetDatabase.CreateAsset(brushStore, filePath);
  114. var settings = LoadOrCreateGridBrushPickStoreSettings();
  115. settings.m_UserPickStore = brushStore;
  116. Apply();
  117. instance.m_PickStorePreference = brushStore;
  118. Selection.activeObject = brushStore;
  119. }
  120. private static void CreateClone()
  121. {
  122. var defaultPath = ProjectBrowser.s_LastInteractedProjectBrowser
  123. ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath()
  124. : "Assets";
  125. var filePath = EditorUtility.SaveFilePanel("Create Brush Picks Asset into folder ", defaultPath,
  126. "BrushPick", "asset");
  127. if (string.IsNullOrEmpty(filePath))
  128. return;
  129. GridBrushPickStore newBrushStore;
  130. var currentBrushStore = GetUserBrushPickStore();
  131. if (currentBrushStore == null)
  132. {
  133. newBrushStore = ScriptableObject.CreateInstance<GridBrushPickStore>();
  134. }
  135. else
  136. {
  137. newBrushStore = Object.Instantiate(currentBrushStore);
  138. }
  139. filePath = FileUtil.GetProjectRelativePath(filePath);
  140. var fileName = FileUtil.UnityGetFileNameWithoutExtension(filePath);
  141. newBrushStore.name = fileName;
  142. AssetDatabase.CreateAsset(newBrushStore, filePath);
  143. // Clone Library Brushes if cloning from default instance
  144. if (currentBrushStore == null)
  145. {
  146. currentBrushStore = GridBrushPickStore.LoadOrCreateLibraryGridBrushPickAsset();
  147. foreach (var userBrush in currentBrushStore.userSavedBrushes)
  148. {
  149. newBrushStore.AddNewUserSavedBrush(userBrush);
  150. }
  151. }
  152. var settings = LoadOrCreateGridBrushPickStoreSettings();
  153. settings.m_UserPickStore = newBrushStore;
  154. Apply();
  155. instance.m_PickStorePreference = newBrushStore;
  156. Selection.activeObject = newBrushStore;
  157. }
  158. }
  159. }