- Home /
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.
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?
Your answer
Follow this Question
Related Questions
Creating Context Menu Item for FBX files 0 Answers
What's the type of a folder/directory concerning the validation for a MenuItem? 1 Answer
Determine the Scene that is selected when using context menu 0 Answers
Editor scripting - How to show the Assets menu as a context menu 1 Answer
Editor Script does not save changes 1 Answer