ue4 Runtime 保存藍(lán)圖資產(chǎn)
有時間項目需求要將一系列的游戲數(shù)據(jù)保存為藍(lán)圖資產(chǎn),然后在其他地方使用。
UMyDataSave?為要繼承自UObject的藍(lán)圖
UCLASS()
class XXXXX_API UMyDataSave?: public UObject
{
? ?GENERATED_BODY()
public:
? ?UPROPERTY(EditAnywhere,BlueprintReadWrite)
? ?int32 IntData;
}
函數(shù)聲明:保存藍(lán)圖資產(chǎn)到AssetPath,并返回該資產(chǎn)的軟對象引用
static bool SaveMyData(const FString& AssetPath, const FString& ObjectName, const TSoftObjectPtr<UMyDataSave?>& MyDataSaveObj,int32 nData);
bool UBPEditorTool::SaveMyData(const FString& AssetPath, const FString& ObjectName,TSoftObjectPtr<UCubeWaveData>& CubeWaveDataObj)
{
? ?FString PackageFileName = FPackageName::LongPackageNameToFilename(AssetPath, FPackageName::GetAssetPackageExtension());
? ?
? ?UPackage* Package = CreatePackage(*AssetPath);
? ?Package->FullyLoad();
? ?// 創(chuàng)建對象時,指定他對應(yīng)的Package就是剛才創(chuàng)建的空資源Package
? ?UMyDataSave?* pMyData = NewObject<UMyDataSave?>(Package, FName(*ObjectName), EObjectFlags::RF_Public | EObjectFlags::RF_Standalone);
? //此處可以填充?pMyData?的數(shù)據(jù)
?pMyData ->IntData =?nData;
? ?UE_LOG(LogTemp, Display, TEXT("SaveMyData:Path:%s,FileName:%s"),*AssetPath,*PackageFileName);
? ?// 保存這個對象到一個指定路徑的uasset文件
? ?bool bSaved = UPackage::SavePackage(Package, pWaveData, EObjectFlags::RF_Public | EObjectFlags::RF_Standalone, *PackageFileName, GError, nullptr, true, true, SAVE_NoError);
? ?FSoftObjectPath SolftObjPath = FSoftObjectPath(pWaveData);
? ?CubeWaveDataObj = TSoftObjectPtr<UCubeWaveData>(SolftObjPath);
? ?return bSaved;
}