Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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:
    40
    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,562
    You should read a little about menu items.

    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:
    40
    Helped quite a bit, thanks. Specifically th epart that shows hot to adress the right click menus, instea dof the top bar.
     
    fafase likes this.
  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:
    5
    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?
     
  6. lovelamp

    lovelamp

    Joined:
    Aug 27, 2020
    Posts:
    1
    Your blog link has directed me to a mallicious website
     
  7. FiveXGames

    FiveXGames

    Joined:
    Apr 27, 2016
    Posts:
    43
    prvaak and DragonCoder like this.
  8. dval

    dval

    Joined:
    Jul 25, 2012
    Posts:
    24
    This script needs to be titled "CustomMenu.cs" and go into a project folder named 'Editor' .

    It adds a menu item for "Create Empty Parent" and does just that.
    Scale, position, rotation, and place in hierarchy should all be preserved.
    I used the examples in the resources above.
    I'd like to thank everyone above for the years of work that have been put into creating this.

    CustomMenu.cs
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class CustomMenu
    5. {
    6.     // Adding a new context menu item
    7.     [MenuItem("GameObject/Create Empty Parent", true)]
    8.     static bool ValidateLogSelectedTransformName()
    9.     {
    10.         // disable menu item if no transform is selected.
    11.         return Selection.activeTransform != null;
    12.     }
    13.  
    14.     // Put menu item at top near other "Create" options
    15.     [MenuItem("GameObject/Create Empty Parent", false, 0)] //10
    16.     private static void EmptyParent(MenuCommand menuCommand)
    17.     {
    18.         // Use selected item as our context (otherwise does nothing because of above)
    19.         //GameObject selected = menuCommand.context as GameObject;
    20.         GameObject selected = Selection.activeObject as GameObject;
    21.  
    22.         // Create a empty game object with same name
    23.         GameObject go = new GameObject(selected.name);
    24.  
    25.         // adjust hierarchy accordingly
    26.         GameObjectUtility.SetParentAndAlign(go, selected.transform.parent.gameObject);
    27.         go.transform.position = selected.transform.position;
    28.         go.transform.rotation = selected.transform.rotation;
    29.         GameObjectUtility.SetParentAndAlign(selected, go);
    30.  
    31.         // Register the creation in the undo system
    32.         Undo.RegisterCreatedObjectUndo(go, "Parented " + go.name);
    33.  
    34.         // Yea!
    35.         Debug.Log("Created a Parent for "+ selected.name + ".");
    36.     }
    37.  
    38. }
     
    Last edited: Feb 1, 2022