diff --git a/Source/Editor/Undo/Actions/PasteActorsAction.cs b/Source/Editor/Undo/Actions/PasteActorsAction.cs index 2fe4e6235..f4dc7b318 100644 --- a/Source/Editor/Undo/Actions/PasteActorsAction.cs +++ b/Source/Editor/Undo/Actions/PasteActorsAction.cs @@ -41,7 +41,7 @@ namespace FlaxEditor.Actions ActionString = name; _pasteParent = pasteParent; - _idsMapping = new Dictionary(objectIds.Length * 4); + _idsMapping = new Dictionary(objectIds.Length); for (int i = 0; i < objectIds.Length; i++) { _idsMapping[objectIds[i]] = Guid.NewGuid(); @@ -72,13 +72,24 @@ namespace FlaxEditor.Actions /// /// Links the broken parent reference (missing parent). By default links the actor to the first scene. /// - /// The actor. - protected virtual void LinkBrokenParentReference(Actor actor) + /// The actor node. + protected virtual void LinkBrokenParentReference(ActorNode actorNode) { // Link to the first scene root if (Level.ScenesCount == 0) throw new Exception("Failed to paste actor with a broken reference. No loaded scenes."); - actor.SetParent(Level.GetScene(0), false); + actorNode.Actor.SetParent(Level.GetScene(0), false); + } + + /// + /// Checks if actor has a broken parent reference. For example, it's linked to the parent that is indie prefab editor while it should be pasted into scene. + /// + /// The actor node. + protected virtual void CheckBrokenParentReference(ActorNode actorNode) + { + // Ensure pasted object ends up on a scene + if (actorNode.Actor.Scene == null) + LinkBrokenParentReference(actorNode); } /// @@ -103,16 +114,13 @@ namespace FlaxEditor.Actions for (int i = 0; i < actors.Length; i++) { var actor = actors[i]; - - // Check if has no parent linked (broken reference eg. old parent not existing) - if (actor.Parent == null) - { - LinkBrokenParentReference(actor); - } - var node = GetNode(actor.ID); if (node is ActorNode actorNode) { + // Check if has no parent linked (broken reference eg. old parent not existing) + if (actor.Parent == null) + LinkBrokenParentReference(actorNode); + nodes.Add(actorNode); } } @@ -136,6 +144,12 @@ namespace FlaxEditor.Actions nodeParents[i].Actor.SetParent(pasteParentNode.Actor, false); } } + else + { + // Sanity check on pasted actor to ensure they end up i na proper context (scene editor or specific prefab editor) + foreach (var node in nodeParents) + CheckBrokenParentReference(node); + } // Store previously looked up names and the results Dictionary foundNamesResults = new(); diff --git a/Source/Editor/Windows/Assets/PrefabWindow.Actions.cs b/Source/Editor/Windows/Assets/PrefabWindow.Actions.cs index e6b71d5e9..32bf18b19 100644 --- a/Source/Editor/Windows/Assets/PrefabWindow.Actions.cs +++ b/Source/Editor/Windows/Assets/PrefabWindow.Actions.cs @@ -264,10 +264,17 @@ namespace FlaxEditor.Windows.Assets } /// - protected override void LinkBrokenParentReference(Actor actor) + protected override void LinkBrokenParentReference(ActorNode actorNode) { // Link to prefab root - actor.SetParent(_window.Graph.MainActor, false); + actorNode.Actor.SetParent(_window.Graph.MainActor, false); + } + + /// + protected override void CheckBrokenParentReference(ActorNode actorNode) + { + if (actorNode.Actor.Scene != null || actorNode.Root != _window.Graph.Root) + LinkBrokenParentReference(actorNode); } ///