UE5 PlaceActor

  • PlaceActors 需在引擎初始化之后
    UE5 PlaceActor_第1张图片

  • 但,单为这一个功能,更改整个模块的启动顺序,也不太划算

  • 更好的办法是,启动顺序保持正常(如"LoadingPhase": "Default" ),然后通过监听引擎初始化完成的回调事件来实现

    FCoreDelegates::OnPostEngineInit.AddRaw(this, &FAdvModModule::RegisterPlaceActors);
    
  • 1、模块启动时,绑定回调事件

    void FAdvModModule::StartupModule()
    {
    	FCoreDelegates::OnPostEngineInit.AddRaw(this, &FAdvModModule::RegisterPlaceActors);
    }
    
  • 2、模块关闭时,卸载

    void FAdvModModule::ShutdownModule()
    {
    	UnregisterPlaceActors();
    }
    
    void FAdvModModule::UnregisterPlaceActors()
    {
    	// 注意,直接判断模块是否有效
    	if (!IPlacementModeModule::IsAvailable())
    	{
    		return;
    	}
    
    	IPlacementModeModule& PlacementModeModule = IPlacementModeModule::Get();
    	PlacementModeModule.UnregisterPlacementCategory(FName("ExtendPlaceActors"));
    }
    
  • 3、注册

    
    void FAdvModModule::RegisterPlaceActors()
    {
    	// 判断模块是否已加载
    	IPlacementModeModule& PlacementModeModule = IPlacementModeModule::Get();
    	if (!PlacementModeModule.IsAvailable())
    	{
    		return;
    	}
    
    	// 注册分类
    	int32 SortOrder = 0;
    	FPlacementCategoryInfo AdvCategoryInfo(FText::FromString("ExtendActors"), FSlateIcon(), FName("ExtendPlaceActors"), "ExtendActors", SortOrder += 10);
    	PlacementModeModule.RegisterPlacementCategory(AdvCategoryInfo);
    	
    	// 创建子项
    	FPlaceableItem* Item01 = new FPlaceableItem(nullptr, FAssetData(AStaticMeshActor::StaticClass()), SortOrder += 10);
    	FPlaceableItem* Item02 = new FPlaceableItem(nullptr, FAssetData(ASkeletalMeshActor::StaticClass()), SortOrder += 10);
    	FPlaceableItem* SKM_Manny = new FPlaceableItem(nullptr, FAssetData(LoadObject<USkeletalMesh>(nullptr, TEXT("/Game/Characters/Mannequins/Meshes/SKM_Manny.SKM_Manny"))), SortOrder += 10);
    	FPlaceableItem* SKM_Quinn = new FPlaceableItem(nullptr, FAssetData(LoadObject<USkeletalMesh>(nullptr, TEXT("/Game/Characters/Mannequins/Meshes/SKM_Quinn.SKM_Quinn"))), SortOrder += 10);
    	FPlaceableItem* ABP_Manny = new FPlaceableItem(nullptr, FAssetData(LoadObject<UAnimBlueprint>(nullptr, TEXT("/Game/Characters/Mannequins/Animations/ABP_Manny.ABP_Manny"))), SortOrder += 10);
    	FPlaceableItem* ABP_Quinn = new FPlaceableItem(nullptr, FAssetData(LoadObject<UAnimBlueprint>(nullptr, TEXT("/Game/Characters/Mannequins/Animations/ABP_Quinn.ABP_Quinn"))), SortOrder += 10);
    	FPlaceableItem* Item_Material = new FPlaceableItem(nullptr, FAssetData(LoadObject<UMaterial>(nullptr, TEXT("/Game/Characters/Mannequins/Materials/M_Mannequin.M_Mannequin"))), SortOrder += 10);
    
    	// 将子项添加到分类中
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(Item01));
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(Item02));
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(SKM_Manny));
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(SKM_Quinn));
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(ABP_Manny));
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(ABP_Quinn));
    	PlacementModeModule.RegisterPlaceableItem(AdvCategoryInfo.UniqueHandle, MakeShareable(Item_Material));
    	
    }
    

你可能感兴趣的:(UE5,C++,ue5,PlaceActor)