• 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 Remiel · Nov 23, 2013 at 11:46 PM · editormonocopyvalidationpaste

Overriding Component Copy/Paste

I have a problem. I need to override component copy/paste for my custom script. Can I just declare a method with attribute [ContextMenu ("Copy Component")] that will cause my method to "override" the default one. If so how can I call the default one? If not, how can I achieve similar behaviour?

As for why I need it: I have a MonoBehaviour that has a field containing a unique value for every instance. But when user copy-pastes MonoBehaviour the value from the old is copied into the new, when in reality I would like to generate a new value for the new Behaviour.

I can't search for all MonoBehaviours in my Scene(or keep a track of them) and upon adding a new one try to find it in the list and if it doesn't exist generate a new value. I can't do it "per scene" because the value is unique per project. I can't use OnValidate callback because I can't really know if the value is valid (and the only time it isn't valid is if the behaviour was created by copy paste).

Do you have an idea how to solve this or a workaround? Any help is greatly appreciated!

Comment
Add comment · Show 2
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 robertbu · Nov 27, 2013 at 12:31 AM 1
Share

http://forum.unity3d.com/threads/190223-Callback-when-object-is-created-in-editor

avatar image Remiel · Jan 05, 2014 at 05:46 PM 0
Share

That does not solve the problem. 1) if I use unity instance ID I can't change the id and in some cases I need to change it 2) if I use ExecuteInEdit$$anonymous$$ode and run it in Awake it will run every time scene is loaded and change every time

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by thienhaflash · Feb 16, 2014 at 02:49 AM

I'm not really sure I fully understad your question completely, but here is my put :

Unity has a unique instance Id for each Object, so you need to create another serializable variable, somewhat called _cachedID that set to instanceID

And then when the copy occurs the instanceID of the copied Object should be different from _cachedID, check this and you know it's being copied.

Knowing it's being copy, you can regenerate your uniqueID or doing anything the way you want.

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
avatar image
0

Answer by devion-user · Jun 22, 2018 at 08:00 PM

In Unity, there is no adequate way to do this. Manipulations with InstanceID will not give the right effect, because it changes when you exit the scene, which will generate false alarms. You also can not focus on FileID, because it works only in the editor, but it is unique. You can not hope for it either because there is no collback in which you could overwrite this data. Callbacks Awake, OnEnable, will occur earlier than you need. There are scenarios in which the unit is overwritten the data after (for example CopyComponent in the context menu). You can not use OnAfterDeserialize () because you can not get rid of the FileID in this callback, or check Application.IsPlaying - all these calls are blocked there, but they are needed for high-quality processing of situations. There is no way to solve this issue fully, until Unity creates subscriptions to such situations. And she will not create them, because the problem is rare.

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

18 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

Related Questions

How to enable copy/paste/delete for programatically selected objects? 1 Answer

How do I keep a texture different on copied objects? 1 Answer

Clipboard pasting twice into InputField on Mac build 3 Answers

Stopping Copy/paste into Textfield. 3 Answers

Why cant I copy anything I have in my Project Tab to the Hierarchy Tab or Scene? 1 Answer

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