问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

UE5.3中使用代码生成蓝图文件并在代码中保存文件的方法详解

创作时间:
作者:
@小白创作中心

UE5.3中使用代码生成蓝图文件并在代码中保存文件的方法详解

引用
CSDN
1.
https://blog.csdn.net/liuyongjie1992/article/details/137224820

本文将介绍如何在UE5.3中使用代码生成蓝图文件并在代码中保存文件。文章详细介绍了创建编辑器蓝图文件、读取Excel数据、生成GameplayEffect蓝图文件等步骤,并提供了完整的C++代码示例。

数据配表

创建编辑器蓝图文件,继承AssetActionUtility.h
创建在编辑器中显示的函数,可以用中文命名方便其他人使用。

右键任意文件
查看打印,发现调用没有问题。

正式生成流程

GetSelectedAssets返回选中资产
CastToDataTable转换配表数据
循环数据配表

把生成单个蓝图文件封装成一个函数
我这里因为是使用配表循环生成GameplayEffect的蓝图文件,因此叫GetBuffFile。


ForceGetBuffFile是创建文件的C++代码,参数有3个
1:蓝图文件CDO
2:生成文件的文件夹,不能存在则自动生成
3:文件名。

UGameplayEffect* UCMAbilityEditorBpLib::ForceGetBuffFile(TSubclassOf<UGameplayEffect> Archetypes, const FString& Directory, FString AssetName, UObject* Outer)
{
    const FSoftClassPath ArchetypesSoftClassPath(Archetypes);
    return UCMEditorBpLib::TryCopyClass<UGameplayEffect>(ArchetypesSoftClassPath, Directory, AssetName, Outer);
}

请注意新生成的文件是未保存状态,在文件夹里也是看不到这个文件的,它还是数据状态。

封装了一个C++方法 ModifyBuffBluePrintData方法,既修改蓝图文件的数据,修改后保存蓝图文件。

全部C++代码:

#include "Ability/CMAbilityEditorBpLib.h"
#include "CMEditorBpLib.h"
#include "GameplayEffectComponents/BlockAbilityTagsGameplayEffectComponent.h"
#include "GameplayEffectComponents/ImmunityGameplayEffectComponent.h"
#include "GameplayEffectComponents/RemoveOtherGameplayEffectComponent.h"

UGameplayEffect* UCMAbilityEditorBpLib::ForceGetBuffFile(TSubclassOf<UGameplayEffect> Archetypes, const FString& Directory, FString AssetName, UObject* Outer)
{
    const FSoftClassPath ArchetypesSoftClassPath(Archetypes);
    return UCMEditorBpLib::TryCopyClass<UGameplayEffect>(ArchetypesSoftClassPath, Directory, AssetName, Outer);
}

void UCMAbilityEditorBpLib::ModifyBuffBluePrintData(UGameplayEffect* buffBlueprint, const FCMAbilityBuffData& buffRowConfig)
{
    if (!buffRowConfig.BlockAbilityTags.IsEmpty())
    {
        UBlockAbilityTagsGameplayEffectComponent& component = buffBlueprint->FindOrAddComponent<UBlockAbilityTagsGameplayEffectComponent>();
        FInheritedTagContainer TagContainer;
        TagContainer.Added = buffRowConfig.BlockAbilityTags;
        component.SetAndApplyBlockedAbilityTagChanges(TagContainer);
    }
    if (!buffRowConfig.RemoveOtherTag.IsEmpty())
    {
        URemoveOtherGameplayEffectComponent& component = buffBlueprint->FindOrAddComponent<URemoveOtherGameplayEffectComponent>();
        FGameplayEffectQuery const Query = FGameplayEffectQuery::MakeQuery_MatchAnyOwningTags(buffRowConfig.RemoveOtherTag);
        component.RemoveGameplayEffectQueries.Add(Query);
    }
    if (!buffRowConfig.ImmunityTag.IsEmpty())
    {
        buffBlueprint->DurationPolicy = EGameplayEffectDurationType::Infinite;
        buffBlueprint->Period = buffRowConfig.Period;
        UImmunityGameplayEffectComponent& component = buffBlueprint->FindOrAddComponent<UImmunityGameplayEffectComponent>();
        FGameplayEffectQuery const Query = FGameplayEffectQuery::MakeQuery_MatchAnyOwningTags(buffRowConfig.ImmunityTag);
        component.ImmunityQueries.Add(Query);
    }
    if (!buffRowConfig.GameplayCueTags.IsEmpty())
    {
        FGameplayEffectCue GC;
        GC.GameplayCueTags = buffRowConfig.GameplayCueTags;
        buffBlueprint->GameplayCues.Add(GC);
    }
    UPackage* Package = buffBlueprint->GetPackage();
    const FString PackageName = Package->GetName();
    const FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
    const bool bSucceeded = UPackage::SavePackage(Package, buffBlueprint, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *PackageFileName, GError, nullptr, true, true, SAVE_NoError);
    if (bSucceeded == false)//失败输出日志
    {
        
    }
}

保存蓝图文件代码如下

UPackage* Package = buffBlueprint->GetPackage();
const FString PackageName = Package->GetName();
const FString PackageFileName = FPackageName::LongPackageNameToFilename(PackageName, FPackageName::GetAssetPackageExtension());
const bool bSucceeded = UPackage::SavePackage(Package, buffBlueprint, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *PackageFileName, GError, nullptr, true, true, SAVE_NoError);
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号