Search Unity

Apply "MenuItem" to all Fields, not just Component?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Invertex, Apr 13, 2017.

  1. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Using
    Code (CSharp):
    1. [MenuItem ("CONTEXT/Object/Do Something")]
    I can create a right-click action on all Components automatically in Inspector. I was wondering if there is a "CONTEXT" to do this to Fields as well? (My main target being Arrays/Lists). I tried doing something like
    Code (CSharp):
    1. [MenuItem ("CONTEXT/Array/Do Something")]
    and
    Code (CSharp):
    1. [MenuItem ("CONTEXT/Field/Do Something")]
    , but those do not appear to be valid contexts.

    Is there no way to create a custom right-click option for all inspector fields (not just the messy per-field CustomAttribute method, I want it to just be an option for every field, just like my Object context option).

    I assume there must be a way, given that some right-click options already exist for Arrays specifically and fields.
     
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
  3. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    This works for me (Place inside an Editor folder):

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public static class Example {
    5.  
    6.     [InitializeOnLoadMethod]
    7.     static void Start() {
    8.  
    9.         EditorApplication.contextualPropertyMenu += OnPropertyContextMenu;
    10.     }
    11.  
    12.     static void OnPropertyContextMenu(GenericMenu menu, SerializedProperty property) {
    13.  
    14.         var propertyCopy = property.Copy();
    15.  
    16.         menu.AddItem(new GUIContent("Print Property"), false, () =>    {
    17.  
    18.             Debug.Log(propertyCopy.displayName);
    19.         });
    20.     }
    21. }
    Keep in mind that this will only work for fields drawn with EditorGUI.PropertyField
     
    TommySKD likes this.
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Oh, yeah that's the kind of thing I was trying to avoid. I just wanted to add a global right-click option to all properties of a certain type, like Unity already does considering all properties have some sort of right-click option.
    I am already able to globally do it for component options, but I'd really like to be able to go a level deeper than that.
     
  6. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    Well... Unity is actually only doing that through EditorGUI.PropertyField. If for example I was to render a float like:

    myFloat = EditorGUI.FloatField(myFloat);

    rather than

    EditorGUI.PropertyField(myFloatProperty)

    the first one won't have any right click options. So it's not possible to do what you want unless a property is rendered using PropertyField. Even with CustomAttribute, you still need to call PropertyField in order to draw using the PropertyDrawer.
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    I'm not talking about custom inspector writing in that way though, so I wouldn't be using any of those. I'm talking about how with any random script you have in Unity, there will be generic right-click options in the default Inspector.
    I can place a single file in an Editor folder that adds more right-click options to EVERY component/script in any default inspector, no references to custom editors or editor properties in them needed. So clearly Unity allows you to extend its default menu options behavior for various things. I'm just wondering how to go a level deeper to its default properties options.

    edit: To explain further, I can stick this in a static class, that doesn't derive from anything, as long as I'm "using UnityEditor", put it in an Editor folder, and now EVERY component in all default Inspectors will have this extra "Move To Top" option that will do whatever I want to the right-clicked component.

    Code (csharp):
    1.     [MenuItem ("CONTEXT/Object/Move To Top")]
    2.     private static void MoveToTop(MenuCommand command)
    3.     {
    4.         MoveComponents(command, true);
    5.     }
    I do not need to modify any scripts in my project to take advantage of this, and it will work on all native Unity components as well. I wish to take this further to the field level, so I can do things like sort Array lists without having to create custom editors for any classes I want to sort arrays in, without having to mark every array with a define to use a custom editor just to use a right-click option.

    @Adam-Buckner Would you know someone on staff that would know anything about this? I saw that this page talks about doing it, but it's at a per-script level instead of Global. Could the MenuItem functionality not be extended down to fields as well?
     
    Last edited: Apr 20, 2017