MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / CreatePrefab

Method CreatePrefab

Source/Engine/Level/Prefabs/PrefabManager.cpp:354–472  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

352#if USE_EDITOR
353
354bool PrefabManager::CreatePrefab(Actor* targetActor, const StringView& outputPath, bool autoLink)
355{
356 PROFILE_CPU_NAMED("Prefab.Create");
357
358 // Validate input
359 if (targetActor == nullptr)
360 {
361 Log::ArgumentNullException();
362 return true;
363 }
364 if (dynamic_cast<Scene*>(targetActor) != nullptr)
365 {
366 LOG(Error, "Cannot create prefab from scene actor.");
367 return true;
368 }
369 if (EnumHasAnyFlags(targetActor->HideFlags, HideFlags::DontSave))
370 {
371 LOG(Error, "Cannot create prefab from actor marked with HideFlags.DontSave.");
372 return true;
373 }
374
375 // Gather all objects
376 CollectionPoolCache<ActorsCache::SceneObjectsListType>::ScopeCache sceneObjects = ActorsCache::SceneObjectsListCache.Get();
377 sceneObjects->EnsureCapacity(256);
378 SceneQuery::GetAllSerializableSceneObjects(targetActor, *sceneObjects.Value);
379
380 // Filter actors for prefab
381 if (FilterPrefabInstancesToSave(Guid::Empty, *sceneObjects.Value))
382 return true;
383
384 LOG(Info, "Creating prefab from actor {0} (total objects count: {2}) to {1}...", targetActor->ToString(), outputPath, sceneObjects->Count());
385
386 // Serialize to json data
387 ASSERT(!IsCreatingPrefab);
388 IsCreatingPrefab = true;
389 rapidjson_flax::StringBuffer actorsDataBuffer;
390 {
391 CompactJsonWriter writerObj(actorsDataBuffer);
392 JsonWriter& writer = writerObj;
393 writer.StartArray();
394 for (int32 i = 0; i < sceneObjects->Count(); i++)
395 {
396 SceneObject* obj = sceneObjects->At(i);
397 writer.SceneObject(obj);
398 }
399 writer.EndArray();
400 }
401 IsCreatingPrefab = false;
402
403 // Randomize the objects ids (prevent overlapping of the prefab instance objects ids and the prefab objects ids)
404 Dictionary<Guid, Guid> objectInstanceIdToPrefabObjectId;
405 objectInstanceIdToPrefabObjectId.EnsureCapacity(sceneObjects->Count());
406 if (targetActor->HasParent())
407 {
408 // Unlink from parent actor
409 objectInstanceIdToPrefabObjectId[targetActor->GetParent()->GetID()] = Guid::Empty;
410 }
411 for (int32 i = 0; i < sceneObjects->Count(); i++)

Callers

nothing calls this directly

Calls 15

EnumHasAnyFlagsFunction · 0.85
ChangeIdsFunction · 0.85
GetIDMethod · 0.80
NewFunction · 0.50
CreateFunction · 0.50
GetMethod · 0.45
EnsureCapacityMethod · 0.45
ToStringMethod · 0.45
CountMethod · 0.45
StartArrayMethod · 0.45
SceneObjectMethod · 0.45

Tested by

no test coverage detected