Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Validating GameObjects - context menu

Discussion in 'Extensions & OnGUI' started by PawelMarecki, Jun 23, 2016.

  1. PawelMarecki


    Aug 7, 2015
    Hi, I have some troubles during creating an editor extension. I have already ask on (Unity Answers), and StackOverflow (where you can find short discussion) (, 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.


    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;
    4. public static class MenuItems
    5. {
    6.      [MenuItem("GameObject/Action on prefab", false, 14)]
    7.      private static void MenuActionPrefab()
    8.      {
    9.      }
    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. }

    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


    Oct 17, 2012
    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:


    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


    Aug 7, 2015
    @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


    Jun 30, 2015
    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


    Jun 30, 2015
    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?