Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Validating GameObjects - context menu

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

  1. PawelMarecki

    PawelMarecki

    Joined:
    Aug 7, 2015
    Posts:
    2
    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:
    2,936
    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:
    2
    @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:
    96
    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.