Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Unsupported.DuplicateGameObjectsUsingPasteboard can't be removed from the Undo stack

Discussion in '2022.1 Beta' started by Darkgaze, Apr 20, 2022.

  1. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    395
    I have this problem that is only happening in 2022.1.0b16.3019

    I have a tool with which you can create objects silently and not registered in the Undo stack, and when you undo, it doesn't delete this object but runs the Undo of the previous task stored.

    But in 2022 it deletes the object created, so it seems like an Undo task is created for it even though I don't call the register method.

    Normal instantiation works as expected. The problem comes with copying prefabs. In my case, not just a regular duplication, but I create a full copy of a prefab with overrides using the only method available to us which is:
    Unsupported.DuplicateGameObjectsUsingPasteboard();

    I select that object, then call the method, then gather the new selection (which is the duplicated object) and restore the previous selection. This works in 2020 and 2021.

    It would be interesting to either have a better way to duplicate prefabs WITH overrides (not just parameter overrides), or have a way to delete the last entry on the Undo stack. Is there any way?

    Thanks
     
  2. thomasa1972

    thomasa1972

    Unity Technologies

    Joined:
    Oct 11, 2016
    Posts:
    10
    I'm not aware of any change to DuplicateGameObjectsUsingPasteboard with regard to undo.
    The undo system has changes fairly recently though. One change which might be relevant is that, orhphaned objects are now deleted by the undo system. When that happens a warning should be printed to the console.
    Are you seeing any warning in the console?
     
  3. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    395
    No. Otherwise I would have seen it.
    What makes an object an orphan?
    The object I create is created via that method, than changing the hideFlags to Hide in Hierarchy and in Inspector so that the object acts like a template.

    obj.hideFlags = HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor | HideFlags.HideInHierarchy | HideFlags.HideInInspector;
     
  4. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    395
    @thomasa1972
    I'm coming back to this with more testing and more information about this. I needed some feedback for a possible bug...

    What I'm trying to do is to silently copy a whole prefab that I manage. It is an editor tool.

    The following code doesn't work.

    Code (CSharp):
    1. Undo.IncrementCurrentGroup();
    2. Unsupported.DuplicateGameObjectsUsingPasteboard();
    3. var duplicate = Selection.activeObject as GameObject; // Since the method doesn't return anything, I need to get it
    4. Undo.ClearUndo(duplicate);
    If I don't increment the ID (first line), the top Undo item in the stack changes text to "Paste <obj name>".

    If I increment the ID (new Undo item), I get a new "Paste <obj name>" added to the Undo stack.

    In both cases Clear doesn't work afterwards. And when I Undo the object gets removed, which I don't want.
    This happens only in latest v2021 and v2022. Not in v2019.

    ------------

    This is problematic because I need to keep this object alive and be ignored by Undo, which would confuse the user.

    Since undoing deletes the object no matter what, I have to create a new one right after that, and since I can't make it silently, it creates another state in the Undo stack "paste <obj name>". This is bad, because if you execute several Undo operations, it clears all possible REDO operations that they just undid.

    Increasing the ID before the operation makes it worse, because Undoing just deletes this object, being very confusing for the user.

    I wish we had an Undo.Clear or an Undo.Pop() to delete the last in the Undo stack. We don't have control on the stack, but only on certain objects, and in this particular case this doesn't work either!