Hello, I’d like to know how to detect Copy/Paste/Duplicate Events on Unity Editor and handle these events.
Thanks a lot!
Same here, SerializeObject didn’t seem to lead anywhere, what’s the story on this? I need to catch paste events to rebuild a guid.
Try the Event class.
ex.
// Prevents duplication of xxx wihout leaving edit-mode
if(myEditor.editMode){
Event e = Event.current;
if(e.type == EventType.ValidateCommand (e.commandName == "Duplicate" || e.commandName == "Copy")){
myEditor.editMode = false;
e.Use();
return;
}
}
All of this depends on where you put it, as OnInspector in Editor will only execute when the editor window is focused. Though, you still have OnSceneGUI.
Hope it helps.
A simple way to detect this might be to use the instanceID
store the gameObject or component instanceID ( GetInstanceID() ) in a script and then in Awake() check that the stored value and GetInstance() match, if they don’t then it’s a new copy or new instance of a prefab rather than an object being loaded in that was already instanced in a scene.
InstanceID has extremely weird behaviour during Edit mode. It will change multiple times during object creation and between saving and loading the scene. It cannot be used.
I would probably use Awake() to find out whether a GUID that was generated at some point in time is present on another instance as well, in which case you’d know you’re a duplicate.
You are not exactly right. It changes, yes. And when it changes, I reinitialize my objects. This happens in play mode as well. It happens after play mode quit. This is not issue for me. The real benefit is that it happens after prefab instantiating and after copy paste
I solved this by having a separate InstanceTool to which all components of certain type register to. Then I use that register to verify that if this instanceId belongs to me… if not, get a new fresh id. Seems to work pretty ok.
Trying to get any kind of id tracking in the component itself using GetInstanceId was really shaky.