• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PawelMarecki · Jun 18, 2016 at 05:48 AM · editor-scriptingmenuitemvalidationcontextmenu

Unity Editor - validating GameObjects

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.

alt text

Code:

File MenuItems.cs is located in Assets\Editor

 using UnityEditor;
 using UnityEngine;
 
 public static class MenuItems
 {
 
     [MenuItem("GameObject/Action on prefab", false, 14)]
     private static void MenuActionPrefab()
     {
     }
 
     [MenuItem("GameObject/Action on prefab", true, 14)]
     private static bool ValidateMenuActionPrefab()
     {
         var isPrefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject) != null;
         return isPrefab;
     }
 }

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 from ValidateMenuActionPrefab() 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.

Additional info:

I have tried to run those methods simple way using:

 [MenuItem("GameObject/Action on prefab")]

and

 [MenuItem("GameObject/Action on prefab", true)]

but in this case there is no Action on prefab in context menu. Also this item isn't visible when I try not to set priority index.

I have posted this problem also on StackOverflow Link. There is a shor discusion too.

Question:

So if this is a proper solution? Or is there another way to achieve same goal?

hierarchy.jpg (50.3 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Variables modified on other scripts through a Editor Script reset on Play? 1 Answer

How do you run File->Build Settings... from code? 0 Answers

Editor Scripting Question 1 Answer

How to save field changes to scene file using ContextMenu? 1 Answer

Adding to GameObject context menu WITHOUT [MenuItem(...)] 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges