| 352 | #if USE_EDITOR |
| 353 | |
| 354 | bool 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++) |
nothing calls this directly
no test coverage detected