Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SceneView Context Menu script. (Free!)

Discussion in 'Assets and Asset Store' started by neon_teebar, Apr 4, 2018.

  1. neon_teebar


    Jun 1, 2017
    Gist file.
    No setup required.
    Shift + Right click on an object in the scene to bring up a list of it's methods marked with [ContextMenu].

    - No set up. Automatically grabs [ContextMenu] of hoverd items.
    - No colliders needed. Uses the built in HandleUtility.PickGameObject.
    - Shows custom context menus for internal classes like Transform.
    - Implements GameObject/Create menu, so you can create objects at mouse click position.
    - Menus for selecting/deselecting GameObjects based on component type or name.
    - Menus for snapping selected objects to the floor/terrain.

    - Undo isn't really supported.
    - Context menus marked with [MenuItem("CONTEXT/")] might not work, as I don't know how to pass a target reference to them through EditorApplication.ExecuteMenuItem(command).

    Add a GetContextMenus() method to any component to have it return context menu items. It can return either: ContextItem[], List<ContextItem>, ContextMenuBuilder, or a MonoBehaviour. If it returns a MonoBehaviour, that object will have GetContextMenus called on it instead, useful as a global or parent context object.
    It can also accept a Ray as a parameter. This will be the ray cast by the scene camera on click, useful for returning context specific menus.

    Add the [SceneViewContextMenu] attribute above any component method and it will be treated as if it were marked [ContextMenu], only Unity won't display it in the component context menu, and it better supports submenus, as the Unity ContextMenu doesn't seem to let you use /.

    A GetContextMenus method can return one of these. It's just a neater, simpler way of creating menus.
    ContextMenuBuilder GetContextMenus(Ray ray)
    return new ContextMenuBuilder()
    .Item("Reset State", Reset)
    .Item("Set State to On", SetState, State.On)
    .Item("Set State to Off", SetState, State.Off)
    .Item("Cast Ray", () => CastRay(ray));

    Here's an example of how I've used it.

    MIT. Do whatever, I don't care. But post useful changes please, so I can update this and make it better.
    Last edited: Apr 4, 2018
    thelebaron, Ruchir, BAIZOR and 6 others like this.
  2. MostHated


    Nov 29, 2015
    Nice, thanks. : D
  3. BubbaRichard


    Jul 1, 2012