wuxianshengcong/Library/PackageCache/com.unity.2d.tilemap@1.0.0/Editor/UI/TilemapEditorToolButton.cs

66 lines
1.9 KiB
C#
Raw Normal View History

2025-01-02 14:49:00 +08:00
using System.Linq;
using UnityEditor.EditorTools;
using UnityEditor.Toolbars;
using UnityEngine;
using UnityEngine.UIElements;
namespace UnityEditor.Tilemaps
{
internal class TilemapEditorToolButton : EditorToolbarToggle
{
private TilemapEditorTool m_TilemapEditorTool;
public TilemapEditorToolButton(TilemapEditorTool tool)
{
focusable = false;
if (tool != null)
{
name = tool.name;
icon = tool.toolbarIcon?.image as Texture2D;
tooltip = tool.toolbarIcon?.tooltip;
m_TilemapEditorTool = tool;
}
this.RegisterValueChangedCallback((evt) =>
{
SetToolActive();
});
RegisterCallback<AttachToPanelEvent>(OnAttachedToPanel);
RegisterCallback<DetachFromPanelEvent>(OnDetachFromPanel);
UpdateState();
}
private void OnAttachedToPanel(AttachToPanelEvent evt)
{
ToolManager.activeToolChanged += UpdateState;
ToolManager.activeContextChanged += UpdateState;
UpdateState();
}
private void OnDetachFromPanel(DetachFromPanelEvent evt)
{
ToolManager.activeToolChanged -= UpdateState;
ToolManager.activeContextChanged -= UpdateState;
}
protected void SetToolActive()
{
var active = EditorToolManager.activeTool;
if (active == m_TilemapEditorTool)
ToolManager.RestorePreviousPersistentTool();
else
ToolManager.SetActiveTool(m_TilemapEditorTool);
UpdateState();
}
private void UpdateState()
{
bool activeTool = m_TilemapEditorTool == EditorToolManager.activeTool;
SetValueWithoutNotify(activeTool);
}
}
}