diff --git a/Source/Engine/Level/Prefabs/Prefab.Apply.cpp b/Source/Engine/Level/Prefabs/Prefab.Apply.cpp index f65c9fbe9..a6a0edd4b 100644 --- a/Source/Engine/Level/Prefabs/Prefab.Apply.cpp +++ b/Source/Engine/Level/Prefabs/Prefab.Apply.cpp @@ -834,7 +834,7 @@ bool Prefab::ApplyAllInternal(Actor* targetActor, bool linkTargetActorObjectToPr sceneObjects->RemoveAtKeepOrder(i); } - // Deserialize new prefab objects (add new objects) + // Deserialize new prefab objects int32 newPrefabInstanceIdToDataIndexCounter = 0; int32 newPrefabInstanceIdToDataIndexStart = sceneObjects->Count(); sceneObjects->Resize(sceneObjects->Count() + newPrefabInstanceIdToDataIndex.Count()); @@ -856,9 +856,27 @@ bool Prefab::ApplyAllInternal(Actor* targetActor, bool linkTargetActorObjectToPr { const int32 dataIndex = i->Value; SceneObject* obj = sceneObjects->At(newPrefabInstanceIdToDataIndexStart + newPrefabInstanceIdToDataIndexCounter++); - if (obj) + if (!obj) + continue; + SceneObjectsFactory::Deserialize(obj, diffDataDocument[dataIndex], modifier.Value); + } + for (int32 j = 0; j < targetObjects->Count(); j++) + { + auto obj = targetObjects->At(j); + Guid prefabObjectId; + if (newPrefabInstanceIdToPrefabObjectId.TryGet(obj->GetSceneObjectId(), prefabObjectId)) { - SceneObjectsFactory::Deserialize(obj, diffDataDocument[dataIndex], modifier.Value); + newPrefabInstanceIdToDataIndexCounter = 0; + for (auto i = newPrefabInstanceIdToDataIndex.Begin(); i.IsNotEnd(); ++i) + { + SceneObject* e = sceneObjects->At(newPrefabInstanceIdToDataIndexStart + newPrefabInstanceIdToDataIndexCounter++); + if (e->GetID() == prefabObjectId) + { + // Synchronize order of new objects with the order in target instance + e->SetOrderInParent(obj->GetOrderInParent()); + break; + } + } } } Scripting::ObjectsLookupIdMapping.Set(nullptr); diff --git a/Source/Engine/Level/SceneObjectsFactory.cpp b/Source/Engine/Level/SceneObjectsFactory.cpp index e366a2f37..5eca3230e 100644 --- a/Source/Engine/Level/SceneObjectsFactory.cpp +++ b/Source/Engine/Level/SceneObjectsFactory.cpp @@ -326,6 +326,15 @@ void SceneObjectsFactory::SynchronizePrefabInstances(Array& sceneO for (int32 i = objectsToCheckCount; i < sceneObjects.Count(); i++) { SceneObject* obj = sceneObjects[i]; + + // Preserve order in parent (values from prefab are used) + auto prefab = Content::LoadAsync(obj->GetPrefabID()); + const auto defaultInstance = prefab && prefab->IsLoaded() ? prefab->GetDefaultInstance(obj->GetPrefabObjectID()) : nullptr; + if (defaultInstance) + { + obj->SetOrderInParent(defaultInstance->GetOrderInParent()); + } + obj->PostLoad(); }