123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using UnityEngine;
- using UnityEngine.Tilemaps;
-
- namespace UnityEditor.Tilemaps
- {
- internal class SceneViewOpenTilePaletteHelper : ScriptableSingleton<SceneViewOpenTilePaletteHelper>
- {
- private bool m_RegisteredEventHandlers;
- private bool m_IsSelectionValid;
-
- [InitializeOnLoadMethod]
- private static void Initialize()
- {
- instance.RegisterEventHandlers();
- }
-
- private void OnEnable()
- {
- RegisterEventHandlers();
- }
-
- private void RegisterEventHandlers()
- {
- if (m_RegisteredEventHandlers)
- return;
-
- Selection.selectionChanged += SelectionChanged;
- EditorApplication.hierarchyChanged += SelectionChanged;
-
- m_IsSelectionValid = IsSelectionValid();
-
- m_RegisteredEventHandlers = true;
- }
-
- private void OnDisable()
- {
- Selection.selectionChanged -= SelectionChanged;
- EditorApplication.hierarchyChanged -= SelectionChanged;
- m_RegisteredEventHandlers = false;
- }
-
- internal static void OpenTilePalette()
- {
- GridPaintPaletteWindow.OpenTilemapPalette();
-
- var target = Selection.activeGameObject;
- if (target != null)
- {
- if (PrefabUtility.IsPartOfPrefabAsset(target))
- {
- var path = AssetDatabase.GetAssetPath(target);
- if (AssetDatabase.LoadAssetAtPath<GridPalette>(path))
- {
- GridPaintingState.palette = AssetDatabase.LoadAssetAtPath<GameObject>(path);
- }
- }
- else if (GridPaintingState.validTargets != null)
- {
- var grid = target.GetComponent<GridLayout>();
- if (grid != null)
- {
- foreach (var validTarget in GridPaintingState.validTargets)
- {
- if (validTarget == target)
- {
- GridPaintingState.scenePaintTarget = target;
- break;
- }
- }
- }
- }
- }
- }
-
- internal static bool IsActive()
- {
- if (GridPaintPaletteWindow.isActive)
- return false;
- return instance.m_IsSelectionValid;
- }
-
- internal static bool IsSelectionValid()
- {
- if (Selection.activeObject == null)
- return false;
- if (Selection.activeObject is TileBase)
- return true;
- if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<GridLayout>() != null)
- return true;
- return false;
- }
-
- private void SelectionChanged()
- {
- m_IsSelectionValid = IsSelectionValid();
- }
-
- internal class SceneViewOpenTilePaletteProperties
- {
- public static readonly string showInSceneViewEditorPref = "OpenTilePalette.ShowInSceneView";
- public static readonly string showInSceneViewLookup = "Show Open Tile Palette in Scene View";
-
- 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.");
- }
-
- internal static bool showInSceneViewActive
- {
- get { return EditorPrefs.GetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, true); }
- set { EditorPrefs.SetBool(SceneViewOpenTilePaletteProperties.showInSceneViewEditorPref, value); }
- }
-
- internal static void PreferencesGUI()
- {
- using (new SettingsWindow.GUIScope())
- {
- EditorGUI.BeginChangeCheck();
- var val = EditorGUILayout.Toggle(SceneViewOpenTilePaletteProperties.showInSceneViewLabel, showInSceneViewActive);
- if (EditorGUI.EndChangeCheck())
- {
- showInSceneViewActive = val;
- SceneView.RepaintAll();
- }
- }
- }
- }
- }
|