SceneViewOpenTilePaletteHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using UnityEngine;
  2. using UnityEngine.Tilemaps;
  3. using Object = UnityEngine.Object;
  4. namespace UnityEditor.Tilemaps
  5. {
  6. internal class SceneViewOpenTilePaletteHelper : ScriptableSingleton<SceneViewOpenTilePaletteHelper>
  7. {
  8. private class Styles
  9. {
  10. public static readonly GUIContent overlayTitleLabel = EditorGUIUtility.TrTextContent("Open Tile Palette");
  11. public static readonly GUIContent openContent = EditorGUIUtility.IconContent("Tilemap Icon", "Open Tile Palette|Opens Tile Palette Window");
  12. }
  13. private bool m_RegisteredEventHandlers;
  14. private bool m_IsSelectionValid;
  15. [InitializeOnLoadMethod]
  16. private static void Initialize()
  17. {
  18. instance.RegisterEventHandlers();
  19. }
  20. private void OnEnable()
  21. {
  22. RegisterEventHandlers();
  23. }
  24. private void RegisterEventHandlers()
  25. {
  26. if (m_RegisteredEventHandlers)
  27. return;
  28. SceneView.duringSceneGui += DuringSceneGUI;
  29. Selection.selectionChanged += SelectionChanged;
  30. EditorApplication.hierarchyChanged += SelectionChanged;
  31. m_IsSelectionValid = IsSelectionValid();
  32. m_RegisteredEventHandlers = true;
  33. }
  34. private void OnDisable()
  35. {
  36. SceneView.duringSceneGui -= DuringSceneGUI;
  37. Selection.selectionChanged -= SelectionChanged;
  38. EditorApplication.hierarchyChanged -= SelectionChanged;
  39. m_RegisteredEventHandlers = false;
  40. }
  41. internal static void OpenTilePalette()
  42. {
  43. GridPaintPaletteWindow.OpenTilemapPalette();
  44. var target = Selection.activeGameObject;
  45. if (target != null)
  46. {
  47. if (PrefabUtility.IsPartOfPrefabAsset(target))
  48. {
  49. var path = AssetDatabase.GetAssetPath(target);
  50. if (AssetDatabase.LoadAssetAtPath<GridPalette>(path))
  51. {
  52. GridPaintingState.palette = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  53. }
  54. }
  55. else if (GridPaintingState.validTargets != null)
  56. {
  57. var grid = target.GetComponent<GridLayout>();
  58. if (grid != null)
  59. {
  60. foreach (var validTarget in GridPaintingState.validTargets)
  61. {
  62. if (validTarget == target)
  63. {
  64. GridPaintingState.scenePaintTarget = target;
  65. break;
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. internal static bool IsActive()
  73. {
  74. if (GridPaintPaletteWindow.isActive)
  75. return false;
  76. return instance.m_IsSelectionValid;
  77. }
  78. internal static bool IsSelectionValid()
  79. {
  80. if (Selection.activeObject == null)
  81. return false;
  82. if (Selection.activeObject is TileBase)
  83. return true;
  84. if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<GridLayout>() != null)
  85. return true;
  86. return false;
  87. }
  88. private void DuringSceneGUI(SceneView sceneView)
  89. {
  90. if (!showInSceneViewActive || !IsActive())
  91. return;
  92. SceneViewOverlay.Window(Styles.overlayTitleLabel, OnSceneViewDisplayGUI, (int)SceneViewOverlay.Ordering.TilemapRenderer + 1, SceneViewOverlay.WindowDisplayOption.OneWindowPerTitle);
  93. }
  94. private void SelectionChanged()
  95. {
  96. m_IsSelectionValid = IsSelectionValid();
  97. }
  98. private void OnSceneViewDisplayGUI(Object displayTarget, SceneView sceneView)
  99. {
  100. EditorGUILayout.BeginHorizontal();
  101. GUILayout.FlexibleSpace();
  102. if (GUILayout.Button(Styles.openContent, GUILayout.Height(32), GUILayout.Width(32)))
  103. {
  104. OpenTilePalette();
  105. }
  106. GUILayout.FlexibleSpace();
  107. EditorGUILayout.EndHorizontal();
  108. }
  109. internal class SceneViewOpenTilePaletteProperties
  110. {
  111. public static readonly string showInSceneViewEditorPref = "OpenTilePalette.ShowInSceneView";
  112. public static readonly string showInSceneViewLookup = "Show Open Tile Palette in Scene View";
  113. public static readonly GUIContent showInSceneViewLabel = EditorGUIUtility.TrTextContent(showInSceneViewLookup, "Shows an overlay in the SceneView for opening the Tile Palette when selecting an object that interacts with the Tile Palette.");
  114. }
  115. internal static bool showInSceneViewActive
  116. {
  117. get { return EditorPrefs.GetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, true); }
  118. set { EditorPrefs.SetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, value); }
  119. }
  120. internal static void PreferencesGUI()
  121. {
  122. using (new SettingsWindow.GUIScope())
  123. {
  124. EditorGUI.BeginChangeCheck();
  125. var val = EditorGUILayout.Toggle(SceneViewOpenTilePaletteProperties.showInSceneViewLabel, showInSceneViewActive);
  126. if (EditorGUI.EndChangeCheck())
  127. {
  128. showInSceneViewActive = val;
  129. SceneView.RepaintAll();
  130. }
  131. }
  132. }
  133. }
  134. }