No Description
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.

SceneViewOpenTilePaletteHelper.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEngine;
  2. using UnityEngine.Tilemaps;
  3. namespace UnityEditor.Tilemaps
  4. {
  5. internal class SceneViewOpenTilePaletteHelper : ScriptableSingleton<SceneViewOpenTilePaletteHelper>
  6. {
  7. private bool m_RegisteredEventHandlers;
  8. private bool m_IsSelectionValid;
  9. private bool m_HighlightHelper;
  10. internal static bool highlight
  11. {
  12. get => instance.m_HighlightHelper;
  13. set => instance.m_HighlightHelper = value;
  14. }
  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. Selection.selectionChanged += SelectionChanged;
  29. EditorApplication.hierarchyChanged += SelectionChanged;
  30. m_IsSelectionValid = IsSelectionValid();
  31. m_RegisteredEventHandlers = true;
  32. }
  33. private void OnDisable()
  34. {
  35. Selection.selectionChanged -= SelectionChanged;
  36. EditorApplication.hierarchyChanged -= SelectionChanged;
  37. m_RegisteredEventHandlers = false;
  38. }
  39. internal static void OpenTilePalette()
  40. {
  41. GridPaintPaletteWindow.OpenTilemapPalette();
  42. instance.m_HighlightHelper = false;
  43. var target = Selection.activeGameObject;
  44. if (target != null)
  45. {
  46. if (PrefabUtility.IsPartOfPrefabAsset(target))
  47. {
  48. var path = AssetDatabase.GetAssetPath(target);
  49. if (AssetDatabase.LoadAssetAtPath<GridPalette>(path))
  50. {
  51. GridPaintingState.palette = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  52. }
  53. }
  54. else if (GridPaintingState.validTargets != null)
  55. {
  56. var grid = target.GetComponent<GridLayout>();
  57. if (grid != null)
  58. {
  59. foreach (var validTarget in GridPaintingState.validTargets)
  60. {
  61. if (validTarget == target)
  62. {
  63. GridPaintingState.scenePaintTarget = target;
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. internal static bool IsActive()
  72. {
  73. if (GridPaintingState.isEditing)
  74. return false;
  75. return instance.m_IsSelectionValid;
  76. }
  77. internal static bool IsSelectionValid()
  78. {
  79. if (Selection.activeObject == null)
  80. return false;
  81. if (Selection.activeObject is TileBase)
  82. return true;
  83. if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<GridLayout>() != null)
  84. return true;
  85. return false;
  86. }
  87. private void SelectionChanged()
  88. {
  89. var old = m_IsSelectionValid;
  90. m_IsSelectionValid = IsSelectionValid();
  91. if (m_IsSelectionValid != old)
  92. m_HighlightHelper = m_IsSelectionValid;
  93. }
  94. internal class SceneViewOpenTilePaletteProperties
  95. {
  96. public static readonly string showInSceneViewEditorPref = "OpenTilePalette.ShowInSceneView";
  97. public static readonly string showInSceneViewLookup = "Show Open Tile Palette in Scene View";
  98. 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.");
  99. }
  100. internal static bool showInSceneViewActive
  101. {
  102. get { return EditorPrefs.GetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, true); }
  103. set { EditorPrefs.SetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, value); }
  104. }
  105. internal static void PreferencesGUI()
  106. {
  107. using (new SettingsWindow.GUIScope())
  108. {
  109. EditorGUI.BeginChangeCheck();
  110. var val = EditorGUILayout.Toggle(SceneViewOpenTilePaletteProperties.showInSceneViewLabel, showInSceneViewActive);
  111. if (EditorGUI.EndChangeCheck())
  112. {
  113. showInSceneViewActive = val;
  114. SceneView.RepaintAll();
  115. }
  116. }
  117. }
  118. }
  119. }