Search Unity

Validating GameObjects - context menu

Discussion in 'Immediate Mode GUI (IMGUI)' started by PawelMarecki, Jun 23, 2016.

  1. PawelMarecki

    PawelMarecki

    Joined:
    Aug 7, 2015
    Posts:
    3
    Hi, I have some troubles during creating an editor extension. I have already ask on (Unity Answers) http://answers.unity3d.com/questions/1204078/unity-editor-validating-gameobjects.html, and StackOverflow (where you can find short discussion) (http://stackoverflow.com/questions/37774574/unity-editor-validating-gameobjects/37774644), but with no results. I'm not sure if thats what i've done is wrongly coded or this is some kind of bug or strange behaviour. Here is short description of my problem.

    Task:

    I want to create a context menu item that will work on GameObject selected in Hierarchy. This action should be available only when selected GameObject is a Prefab. I'm working on Unity 5.3.2 (tested also on 5.3.5)

    Behaviour should be simillar to `Select Prefab` item (visible on screenshot). When object isn't connected to prefab field should be grey and not-clickable. When object is a prefab field is black and can be clicked.



    I have already make script and put it in Assets/Editor

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public static class MenuItems
    5. {
    6.      [MenuItem("GameObject/Action on prefab", false, 14)]
    7.      private static void MenuActionPrefab()
    8.      {
    9.      }
    10.  
    11.      [MenuItem("GameObject/Action on prefab", true, 14)]
    12.      private static bool ValidateMenuActionPrefab()
    13.      {
    14.          var isPrefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject) != null;
    15.          return isPrefab;
    16.      }
    17. }
    Problem:

    The problem is that the menu item is visible (and can be clicked) no matter if selected GameObject is a Prefab or not. I have tested code in debugger and value returned byValidateMenuActionPrefab() is OK (`true` when I run it on Prefab, false on non-prefab gameobject).

    I've read somewhere that Unity 5 have problem with validation methods but the example from UNITY EDITOR EXTENSIONS – MENU ITEMS about validation in Assets work perfectly OK.

    Looks like the validation method doesn't get called when I've try to use RMB context menu on GameObjects.
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Your code adds a new menu item under the GameObject menu. You can see that the validation method does work, it's just that it works only for the menu item, and not when you right-click something in the hierarchy:

    upload_2016-6-24_8-45-34.png

    Digging a bit deeper (you can search for the decompiled רcode for the SceneHierarchyWindow class, even MonoDevelop allows you to do that), the logic is a bit different:
    1. A GenericMenu instance is created.
    2. Menu items starting with "GameObject" are added to this menu
    3. Other options are being added (such as "Select Prefab")
    Note that none of the validation methods work here since these are only valid for "menu items" and not for this particular menu.

    You can see that the right-click menu has a "Select prefab" option which is greyed out in case something that isn't a prefab is selected. This is done since they internally add this item disabled in case the selection is not a prefab.

    So, it appears that what you want to achieve is not possible at the moment. You can work around by simply not doing anything in case the selection is not a prefab (less elegant, i know).
     
    Last edited: Jun 20, 2018
  3. PawelMarecki

    PawelMarecki

    Joined:
    Aug 7, 2015
    Posts:
    3
    @liortal Thanks for detailed answer. Looks like this is an expected (by Unity Editor) behaviour. I think it'll be ok just to log message that object is not a prefab. As you said it's less elegant but it has to be like this right now.
     
    Last edited: Jun 25, 2016
  4. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    Having the same problem right now.
    It seems to be possible for Unity menuItems. "Rename" and other selected dependent stuff is greyed out even in context menu.
     
  5. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    The bug is still in in 2018.2.1.
    Problem is, there is no way (I see) to notify the user in a proper way because of the way the validation function is called.

    Let's say you decide to show a Dialog to the user in your validation function when things fail. This works perfectly when user right clicks and tries to create your prefab.
    But when using Main Menu this validation function is called before user can see your menu, which leads to confusion. If you have two functions in your menu and validation fails for both, user gets two dialog boxes (again, before even seeing your menu entries).

    Doing validation only in validation function and showing Dialog in your creation function does not work in context menu because it first calls validation function (after user has clicked on 'Create Prefab') and does not branch to creation function anymore, so any Dialog here will not be shown.

    To work around this bug we need at least an information from where validation is called. But better fix the bug and call validation in the same order in both cases.

    Could anyone find a work around?
     
  6. markoal

    markoal

    Joined:
    Aug 31, 2015
    Posts:
    30
    Still a problem in newer unity, at least in 2019.3.14f1.
    To summarize for others:
    Code (CSharp):
    1.  
    2. [UnityEditor.MenuItem("GameObject/MyMenu/DoSomething", false, 0)]
    3. public static void MenuDoSomething(){ }
    4.  
    5. [UnityEditor.MenuItem("GameObject/MyMenu/DoSomething", true, 0)]
    6. public static void MenuCanDoSomething(){ return false; }
    This code above shows the menu item
    1. inside the Hierarchy window when you press the right mouse click (Right-click > MyMenu > DoSomething)
    2. inside upper menu (where are located File/Edit/Assets/...) GameObjects > MyMenu > DoSomething.
    However, the validation method successfully grays out the DoSomething method in the second case in the upper menu but it's not for the hierarchy window.

    Example of how grayed-out option looks, this should work the same when you use validate function.
    .