Fix setting up prefab objects ids mapping for nested prefabs to link cross-object references correctly

This commit is contained in:
Wojtek Figat
2023-10-16 16:37:44 +02:00
parent 3e7368b1cb
commit 582cf94198
2 changed files with 18 additions and 3 deletions

View File

@@ -369,14 +369,14 @@ SceneObjectsFactory::PrefabSyncData::PrefabSyncData(Array<SceneObject*>& sceneOb
{ {
} }
void SceneObjectsFactory::SetupPrefabInstances(Context& context, PrefabSyncData& data) void SceneObjectsFactory::SetupPrefabInstances(Context& context, const PrefabSyncData& data)
{ {
PROFILE_CPU_NAMED("SetupPrefabInstances"); PROFILE_CPU_NAMED("SetupPrefabInstances");
const int32 count = data.Data.Size(); const int32 count = data.Data.Size();
ASSERT(count <= data.SceneObjects.Count()); ASSERT(count <= data.SceneObjects.Count());
for (int32 i = 0; i < count; i++) for (int32 i = 0; i < count; i++)
{ {
SceneObject* obj = data.SceneObjects[i]; const SceneObject* obj = data.SceneObjects[i];
if (!obj) if (!obj)
continue; continue;
const auto& stream = data.Data[i]; const auto& stream = data.Data[i];
@@ -417,6 +417,21 @@ void SceneObjectsFactory::SetupPrefabInstances(Context& context, PrefabSyncData&
// Add to the prefab instance IDs mapping // Add to the prefab instance IDs mapping
auto& prefabInstance = context.Instances[index]; auto& prefabInstance = context.Instances[index];
prefabInstance.IdsMapping[prefabObjectId] = id; prefabInstance.IdsMapping[prefabObjectId] = id;
// Walk over nested prefabs to link any subobjects into this object (eg. if nested prefab uses cross-object references to link them correctly)
NESTED_PREFAB_WALK:
const ISerializable::DeserializeStream* prefabData;
if (prefab->ObjectsDataCache.TryGet(prefabObjectId, prefabData) && JsonTools::GetGuidIfValid(prefabObjectId, *prefabData, "PrefabObjectID"))
{
prefabId = JsonTools::GetGuid(stream, "PrefabID");
prefab = Content::LoadAsync<Prefab>(prefabId);
if (prefab && !prefab->WaitForLoaded())
{
// Map prefab object ID to the deserialized instance ID
prefabInstance.IdsMapping[prefabObjectId] = id;
goto NESTED_PREFAB_WALK;
}
}
} }
} }

View File

@@ -100,7 +100,7 @@ public:
/// </remarks> /// </remarks>
/// <param name="context">The serialization context.</param> /// <param name="context">The serialization context.</param>
/// <param name="data">The sync data.</param> /// <param name="data">The sync data.</param>
static void SetupPrefabInstances(Context& context, PrefabSyncData& data); static void SetupPrefabInstances(Context& context, const PrefabSyncData& data);
/// <summary> /// <summary>
/// Synchronizes the new prefab instances by spawning missing objects that were added to prefab but were not saved with scene objects collection. /// Synchronizes the new prefab instances by spawning missing objects that were added to prefab but were not saved with scene objects collection.