- Home /
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!
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
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.
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.
Your answer
Follow this Question
Related Questions
How to enable copy/paste/delete for programatically selected objects? 1 Answer
I having interface problem and it's nasty! Help please? 1 Answer
Parent losing references to Child fields after duplication, copy/paste 0 Answers
Monodevelop hotkeys Issue - Not support others language 0 Answers
Any way to copy and paste GameObjects programatically? 1 Answer