Singularity/Library/PackageCache/com.unity.2d.tilemap@1.0.0/Editor/SceneViewOpenTilePaletteHel...
2024-05-06 11:45:45 -07:00

127 lines
4.3 KiB
C#

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();
}
}
}
}
}