Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

SceneView Context Menu script. (Free!)

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

  1. neon_teebar

    neon_teebar

    Joined:
    Jun 1, 2017
    Posts:
    46
    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].



    Features:
    - 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.

    Warnings:
    - 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).

    GetContextMenus()
    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.

    [SceneViewContextMenu]
    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 /.

    ContextMenuBuilder
    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)
    .Seperator()
    .Item("Cast Ray", () => CastRay(ray));
    }

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

    License:
    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
    BAIZOR, bricevdm, SM_AF and 4 others like this.
  2. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,125
    Nice, thanks. : D
     
  3. BubbaRichard

    BubbaRichard

    Joined:
    Jul 1, 2012
    Posts:
    48
unityunity