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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. [InitializeOnLoadMethod]
  10. private static void Initialize()
  11. {
  12. instance.RegisterEventHandlers();
  13. }
  14. private void OnEnable()
  15. {
  16. RegisterEventHandlers();
  17. }
  18. private void RegisterEventHandlers()
  19. {
  20. if (m_RegisteredEventHandlers)
  21. return;
  22. Selection.selectionChanged += SelectionChanged;
  23. EditorApplication.hierarchyChanged += SelectionChanged;
  24. m_IsSelectionValid = IsSelectionValid();
  25. m_RegisteredEventHandlers = true;
  26. }
  27. private void OnDisable()
  28. {
  29. Selection.selectionChanged -= SelectionChanged;
  30. EditorApplication.hierarchyChanged -= SelectionChanged;
  31. m_RegisteredEventHandlers = false;
  32. }
  33. internal static void OpenTilePalette()
  34. {
  35. GridPaintPaletteWindow.OpenTilemapPalette();
  36. var target = Selection.activeGameObject;
  37. if (target != null)
  38. {
  39. if (PrefabUtility.IsPartOfPrefabAsset(target))
  40. {
  41. var path = AssetDatabase.GetAssetPath(target);
  42. if (AssetDatabase.LoadAssetAtPath<GridPalette>(path))
  43. {
  44. GridPaintingState.palette = AssetDatabase.LoadAssetAtPath<GameObject>(path);
  45. }
  46. }
  47. else if (GridPaintingState.validTargets != null)
  48. {
  49. var grid = target.GetComponent<GridLayout>();
  50. if (grid != null)
  51. {
  52. foreach (var validTarget in GridPaintingState.validTargets)
  53. {
  54. if (validTarget == target)
  55. {
  56. GridPaintingState.scenePaintTarget = target;
  57. break;
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. internal static bool IsActive()
  65. {
  66. if (GridPaintPaletteWindow.isActive)
  67. return false;
  68. return instance.m_IsSelectionValid;
  69. }
  70. internal static bool IsSelectionValid()
  71. {
  72. if (Selection.activeObject == null)
  73. return false;
  74. if (Selection.activeObject is TileBase)
  75. return true;
  76. if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<GridLayout>() != null)
  77. return true;
  78. return false;
  79. }
  80. private void SelectionChanged()
  81. {
  82. m_IsSelectionValid = IsSelectionValid();
  83. }
  84. internal class SceneViewOpenTilePaletteProperties
  85. {
  86. public static readonly string showInSceneViewEditorPref = "OpenTilePalette.ShowInSceneView";
  87. public static readonly string showInSceneViewLookup = "Show Open Tile Palette in Scene View";
  88. 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.");
  89. }
  90. internal static bool showInSceneViewActive
  91. {
  92. get { return EditorPrefs.GetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, true); }
  93. set { EditorPrefs.SetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, value); }
  94. }
  95. internal static void PreferencesGUI()
  96. {
  97. using (new SettingsWindow.GUIScope())
  98. {
  99. EditorGUI.BeginChangeCheck();
  100. var val = EditorGUILayout.Toggle(SceneViewOpenTilePaletteProperties.showInSceneViewLabel, showInSceneViewActive);
  101. if (EditorGUI.EndChangeCheck())
  102. {
  103. showInSceneViewActive = val;
  104. SceneView.RepaintAll();
  105. }
  106. }
  107. }
  108. }
  109. }