Files
BABA_YAGA/Assets/Editor/AddStickyNoteContextMenu.cs

45 lines
1.6 KiB
C#
Raw Normal View History

2026-03-26 20:27:19 +07:00
using UnityEditor;
using UnityEngine;
namespace Editor
{
public static class AddStickyNoteContextMenu
{
[MenuItem("GameObject/Add Sticky Note", false, 10)] // Menu item at top, with priority 10
private static void AddStickyNote(MenuCommand menuCommand)
{
// Ensure a GameObject is selected
if (Selection.activeGameObject == null)
{
Debug.LogWarning("No GameObject selected to add Sticky Note.");
return;
}
GameObject selectedGameObject = Selection.activeGameObject;
// Check if StickyNote component already exists
if (selectedGameObject.GetComponent<StickyNote>() != null)
{
Debug.LogWarning($"StickyNote component already exists on '{selectedGameObject.name}'.");
return;
}
// Add the StickyNote component
StickyNote stickyNote = selectedGameObject.AddComponent<StickyNote>();
Undo.RegisterCreatedObjectUndo(stickyNote, "Add Sticky Note");
Debug.Log($"StickyNote added to '{selectedGameObject.name}'.");
}
// Validate the menu item.
// It will only be enabled if a GameObject is selected and it doesn't already have a StickyNote component.
[MenuItem("GameObject/Add Sticky Note", true)]
private static bool ValidateAddStickyNote()
{
if (Selection.activeGameObject == null)
{
return false;
}
return Selection.activeGameObject.GetComponent<StickyNote>() == null;
}
}
}