Search Unity

Detecting Copy/Paste/Duplicate Events on Editor

Discussion in 'Editor & General Support' started by xLeo, Jun 22, 2011.

  1. xLeo

    xLeo

    Joined:
    Sep 21, 2010
    Posts:
    194
    Hello, I'd like to know how to detect Copy/Paste/Duplicate Events on Unity Editor and handle these events.
    Thanks a lot!
     
  2. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    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.
     
  3. NTDC-DEV

    NTDC-DEV

    Joined:
    Jul 22, 2010
    Posts:
    593
    Try the Event class.

    ex.
    Code (csharp):
    1.  
    2. // Prevents duplication of xxx wihout leaving edit-mode
    3. if(myEditor.editMode){
    4. Event e = Event.current;
    5. if(e.type == EventType.ValidateCommand  (e.commandName == "Duplicate" || e.commandName == "Copy")){
    6. myEditor.editMode = false;
    7. e.Use();
    8. return;
    9. }
    10. }
    11.  
    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.
     
    kilik128 and IgorAherne like this.
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    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.
     
  5. Deleted User

    Deleted User

    Guest

    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.
     
    Propagant, IgorAherne and karl_ like this.
  6. petersvp

    petersvp

    Joined:
    Dec 20, 2013
    Posts:
    63
    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
     
  7. snlehton

    snlehton

    Joined:
    Jun 11, 2010
    Posts:
    99
    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.
     
  8. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
  9. TelloSoft

    TelloSoft

    Joined:
    Oct 29, 2019
    Posts:
    17
    There is an asset on the unity asset store named Copy Paste UnityEvents, after installing, you will see the nice copy-paste buttons below all of your serialized unity events in the inspector.

    Link To Copy Paste UnityEvents https://assetstore.unity.com/packages/slug/214485