如果是单例的全局配置,可配置 static 方法来获取数据
可以配置普通数据、结构体、其他 UDataAsset 等
可以配置 资源软引用 用于加载
自定义 UDataAsset 派生类代码
USTRUCT(BlueprintType, Blueprintable)
struct FAttackInfo :public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FString Name ;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float Damage;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UAnimMontage* Montage;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UParticleSystem* VFX;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
USoundBase* Sound;
};
class TIPS_API USympleDataAsset : public UDataAsset
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FAttackInfo AttackInfo;
//UFUNCTION(BlueprintPure, meta = (DisplayName = "SympleDataAsset"))
//static USympleDataAsset* Get();
};
调用数据
void ADataDriveActor::UseDataAsset(USympleDataAsset* SympleDataAsset)
{
if (SympleDataAsset)
{
const FAttackInfo& AttackInfo = SympleDataAsset->AttackInfo;
UE_LOG(LogTemp, Warning, TEXT(" Name:%s\n Damage:%f\n Montage:%s\n Sound:%s\n"),
*AttackInfo.Name, AttackInfo.Damage, *AttackInfo.Montage->GetName(), *AttackInfo.Sound->GetName());
}
}
编辑器创建和使用
将上述代码改为继承 UPrimaryDataAsset 后,可以在蓝图使用
支持动态创建 UPrimaryDataAsset 资源
void USympleBPFunLib::CreateNewDataAsset(const FString PackagePath, const FString AssetName, const FAttackInfo& AttackInfo)
{
//创建package
UPackage* MyPackage = CreatePackage(nullptr, *PackagePath);
//创建Object
USympleDataAsset* SympleDataAsset = NewObject<USympleDataAsset>(MyPackage, USympleDataAsset::StaticClass(), *AssetName, EObjectFlags::RF_Public | RF_Standalone);
if (!SympleDataAsset)
{
return;
}
// 初始赋值,必要时可拷贝赋值
SympleDataAsset->AttackInfo = AttackInfo;//
// 创建资源
FAssetRegistryModule::AssetCreated(SympleDataAsset);
// 标记
SympleDataAsset->GetOutermost()->MarkPackageDirty();
}
【UE4 C++】 UDataAsset、UPrimaryDataAsset 的简单使用
原文:https://www.cnblogs.com/shiroe/p/14770421.html