Skip to content

Commit

Permalink
Update SceneContextMenuActions.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbusysky authored May 29, 2024
1 parent 8776dc2 commit b9a38a2
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

namespace Scenario.Editor
{
/// <summary>
/// This class provides custom context menu actions for the Unity editor's Scene view.
/// When a user right-clicks (with Ctrl key pressed) on a selected GameObject in the Scene view,
/// a context menu appears, offering various layer manipulation options such as moving,
/// cloning, deleting, flipping, and setting background images.
/// </summary>
public class SceneContextMenuActions
{
[InitializeOnLoadMethod]
Expand All @@ -13,6 +19,10 @@ private static void Initialize()
SceneView.duringSceneGui += OnSceneGUI;
}

/// <summary>
/// Handles the Scene GUI events. Detects right-click with Ctrl key pressed and shows the context menu if a GameObject is selected.
/// </summary>
/// <param name="sceneView">The current SceneView.</param>
private static void OnSceneGUI(SceneView sceneView)
{
Event current = Event.current;
Expand All @@ -27,6 +37,9 @@ private static void OnSceneGUI(SceneView sceneView)
}
}

/// <summary>
/// Displays the custom context menu with various layer manipulation options.
/// </summary>
private static void ShowContextMenu()
{
GenericMenu menu = new GenericMenu();
Expand All @@ -44,33 +57,52 @@ private static void ShowContextMenu()
menu.ShowAsContext();
}

/// <summary>
/// Moves the selected layer up in the hierarchy.
/// </summary>
private static void MoveLayerUp()
{
GameObject selectedLayer = Selection.activeGameObject;
int siblingIndex = selectedLayer.transform.GetSiblingIndex();
selectedLayer.transform.SetSiblingIndex(siblingIndex + 1);
}

/// <summary>
/// Moves the selected layer down in the hierarchy.
/// Ensures the new sibling index is within valid bounds to avoid errors.
/// </summary>
private static void MoveLayerDown()
{
GameObject selectedLayer = Selection.activeGameObject;
int siblingIndex = selectedLayer.transform.GetSiblingIndex();
selectedLayer.transform.SetSiblingIndex(siblingIndex - 1);
if (siblingIndex > 0)
{
selectedLayer.transform.SetSiblingIndex(siblingIndex - 1);
}
}

/// <summary>
/// Clones the selected layer.
/// </summary>
private static void CloneLayer()
{
GameObject selectedLayer = Selection.activeGameObject;
GameObject clonedLayer = UnityEngine.Object.Instantiate(selectedLayer);
clonedLayer.name = selectedLayer.name + " (Clone)";
}

/// <summary>
/// Deletes the selected layer.
/// </summary>
private static void DeleteLayer()
{
GameObject selectedLayer = Selection.activeGameObject;
UnityEngine.Object.DestroyImmediate(selectedLayer);
}

/// <summary>
/// Flips the selected layer horizontally.
/// </summary>
private static void FlipHorizontal()
{
GameObject selectedLayer = Selection.activeGameObject;
Expand All @@ -81,6 +113,9 @@ private static void FlipHorizontal()
}
}

/// <summary>
/// Flips the selected layer vertically.
/// </summary>
private static void FlipVertical()
{
GameObject selectedLayer = Selection.activeGameObject;
Expand All @@ -91,6 +126,9 @@ private static void FlipVertical()
}
}

/// <summary>
/// Removes the background of the selected layer's sprite by sending it to an external API.
/// </summary>
private static void RemoveBackground()
{
GameObject selectedLayer = Selection.activeGameObject;
Expand Down Expand Up @@ -132,6 +170,9 @@ private static void RemoveBackground()
}
}

/// <summary>
/// Sets the selected layer as the background by moving it to the first position in the hierarchy.
/// </summary>
private static void SetAsBackground()
{
GameObject selectedLayer = Selection.activeGameObject;
Expand Down

0 comments on commit b9a38a2

Please sign in to comment.