• 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
1
Question by numberkruncher · Mar 28, 2014 at 10:18 PM · editorinspectormonobehaviour

Prevent 'Paste Component Values' on MonoBehaviour

I have a custom MonoBehaviour class and would like to remove or disable the menu items for copying and pasting since they do not make sense for this particular behaviour. If used, these menu items will lead to trouble!

  • Copy Component

  • Paste Component as New

  • Paste Component Values

Is there a way to achieve this?

Comment
Add comment · Show 1
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
avatar image frarees · Mar 29, 2014 at 10:57 AM 1
Share

That functionality is implemented in UnityEditorInternal.ComponentUtility, but the actual behavior is defined in unmanaged land, so couldn't track it. On OnInspectorGUI you don't get an event that let's you know what happened (i.e. you receive an used event). Haven't found anything yet

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by vexe · May 09, 2014 at 06:52 AM

Hello there again :)

So I've been fiddling around trying to find a public way to modify this behavior, I couldn't find any. Via reflection, I found out that the method responsible for drawing the Help and Settings context menu, is DrawHeaderHelpAndSettingsGUI in UnityEditor.Editor - If you go to UnityEditor.Editor.DrawHeaderGUI you'll see a call to it there (this is all part of the inspector window OnGUI - It's in UnityEditor.InspectorWindow if you wanna check it out feel free)

If we take a peek inside DrawHeaderHelpAndSettingsGUI we'll see a call to DisplayObjectContextMenu, which in turns calls the internal method Internal_DisplayObjectContextMenu - This is really what we care about.

Using the same injecting techniques in here and here, we can definitely do something about it! - Here's what I did:

1- Created a wrapper for Internal_DisplayObjectContextMenu that won't display context options for certain types:

 public static List<Type> typesToExcludeFromContextMenu = new List<Type>
 {
     typeof(AwesomeColTest)    //, etc
 };

 public static void Internal_DisplayObjectContextMenu(Rect position, Object[] context, int contextUserData)
 {
     if (context.Any(c => typesToExcludeFromContextMenu.Contains(c.GetType())))
         return;

     typeof(EditorUtility)
         .GetMethod(
             "Internal_DisplayObjectContextMenu",
             BindingFlags.Static | BindingFlags.NonPublic,
             null,
             new Type[] { typeof(Rect), typeof(Object[]), typeof(int) },
             null)
         .Invoke(null, new object[] { position, context, contextUserData });
 }

2- redirected the method call to Internal_DisplayObjectContextMenu in DisplayObjectContextMenu to my wrapper:

alt text

3- Copy your patched dll to your Editor/Data/Managed folder

4- And that's pretty much it :)

DISCLAIMER: Do this shit on you own responsibility, I'm not responsible for the laws/stuff/etc that might be broken due to this hack. I do this for pure educational reasons.


inject.png (128.6 kB)
Comment
Add comment · Share
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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Large serializable field in a MonoBehaviour causing poor performance in Inspector 0 Answers

How can I determine if property was set to 'None (Audio Clip)' 1 Answer

Can i extend or customise editor for all MonoBehaviours? 3 Answers

Custom Inspector. Aligning properties? 1 Answer

Cogwheel icon in inspector 2 Answers

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