我看网上有人的做法比我更好,我这个更简单
UE5-GAS:读取Excel数据在蓝图创建并更新GE类 - 知乎
数据配表
测试编辑器API
创建编辑器蓝图文件,继承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);