Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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 1 Answer

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

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

Inspector Popup format to look like tag or layer popup 1 Answer

Make popup window in inspector if deleting GameObject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges