Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

simplest way to add an option to right click menu

Discussion in 'Immediate Mode GUI (IMGUI)' started by SkullKing, Aug 9, 2016.

  1. SkullKing

    SkullKing

    Joined:
    May 31, 2012
    Posts:
    33
    Hello all,

    I never scripted anythig regarding the editor itself. so even though I am familiar with C# and read the documentation, I am not completely sure the best way to do what I intend:

    I use a lot of xmlFiles, and I want to add the option "create xml file" to the same right click menus that allow th euser to create a new C# file.

    What is the best way for me to do that? Is it viable to find the code for that menu, and only copy the code for the C# script and alter the file creating part?

    Thank you for your time.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,179
    You should read a little about menu items.

    I have written a blog post about it here.
    This post was also uploaded to the Unity learn site (https://unity3d.com/learn/tutorials/topics/interface-essentials/unity-editor-extensions-menu-items)

    In a nutshell:
    you create a (static) editor method and decorate it with the [MenuItem] attribute
    Inside this method, implement the logic you need for creating a new XML asset, etc.

    I have also created an open source project in the past for supporting new file templates. It actually provides the foundation for your scenario, you should check it out: https://bitbucket.org/liortal/code-templates

    Let me know if this helped :)
     
  3. SkullKing

    SkullKing

    Joined:
    May 31, 2012
    Posts:
    33
    Helped quite a bit, thanks. Specifically th epart that shows hot to adress the right click menus, instea dof the top bar.
     
  4. Jimbobbedyjobob

    Jimbobbedyjobob

    Joined:
    Nov 12, 2015
    Posts:
    5
    Hey,

    I am in an initial research phase for a pipeline/Asset management tool right now.

    Would this be a valid method for accessing an external database to update a flag item on an asset in my database?
     
  5. kthrose

    kthrose

    Joined:
    Mar 19, 2019
    Posts:
    2
    Why does it need to be static? For example, under Hierarchy there is UI -> Dropdown - if I wanted to add a MenuItem inside of the UI submenu, it wouldn't be static - it would be instantiable, no? So MenuItem decorator would need to be on the constructor?