本文主要是介绍关于UE4动态资源加载(蓝图类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本帖纯属个人原创,如有转载请注明出处
需要注意的几点:
1.调试环境下进行的资源加载方式到打包出来后不一定能够使用。
2.假如遇到调试模式下程序运行正常,但是打包出来后程序crash,可以查看log: Saved/Logs/filename/log
3.资源路径的代码书写格式
map : "Game/Maps/Main.map"
蓝图类 : "Game/Blueprint/Skill/skill_1.skill_1_C"
正常的uasset: "Game/Bluerpint/Sound/sound_1.sound_1"
关于资源路径的技巧
1>关于蓝图类的加载记得在结尾要加 _C 就比如 FString sPath = "/Game/Blueprints/Actor/RuntimeActor/RuntimeCameraBP.RuntimeCameraBP_C";
2>使用UE4编辑器的Copy Reference即可,除了蓝图类要加 _C 其他的资源应该都可以加载,就比如 FString sPath = "Texture2D'/Game/Blueprints/UITextures/风险挂接-选中.风险挂接-选中'";
CopyReference操作如下图, 粘贴出来就是 StaticMesh'/Game/Geometry/Meshes/TemplateFloor.TemplateFloor'。然后我们就可以拿到这个路径去进行Load。
关于动态资源生成的几种方式
资源加载远不止我列出这几种方式,会有更多。
1>代码方式如何Spawn蓝图类?
//SpawnActor用法
FString sPath = "/Game/Blueprints/Actor/RuntimeActor/RuntimeCameraBP.RuntimeCameraBP_C";FVector vDir = GGameInstance->Player->GetActorForwardVector();
//vDir.Z = 0;
FVector vLocation = GGameInstance->Player->GetActorLocation() + vDir * 1000;
FActorSpawnParameters params;
params.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
UObject* pObject = UUtilsLibrary::CreateAsset(sPath);
UBlueprintGeneratedClass* BP = Cast<UBlueprintGeneratedClass>(pObject);auto pActor = GWorld->SpawnActor<ASPRuntimeCamera>(BP, FTransform(vLocation), params);
2>另外一种代码Spawn蓝图类的方式
不再写资源路径,让资源去蓝图中选择。TSubclassOf这种写法,可以在蓝图中选择你的这个指定<>类型的蓝图类,比如下面我的例子就是继承自ASPPivotMeshActor的蓝图类可以被选中
/** Pivot Actor bp calss, in the blueprint set value. */
UPROPERTY(Category = "Dynamic Data (General Settings)", EditAnywhere, BlueprintReadWrite)
TSubclassOf<ASPPivotMeshActor> PivotActorBP;//How to Spawn?
SPPivotMeshActor = GWorld->SpawnActor<ASPPivotMeshActor>(PivotActorBP, FTransform(CenterPosition));
3>通过构造加载方式1
如何非构造函数方式加载一个uasset(直接代码写中文以及中文图片的命名方式的习惯不好,不要学我)
//静态方法, 加载uasset的资源,比如UI贴图等。建议写到继承自UBlueprintFunctionLibrary的类中
UFUNCTION(BlueprintCallable, Category = "Utils")
static UObject* CreateAsset(const FString& AssetPath);//实现
UObject* UUtilsLibrary::CreateAsset(const FString& AssetPath)
{FStringAssetReference ref = AssetPath;UObject* uoTmp = ref.ResolveObject();if (uoTmp == nullptr){UE_LOG(LogTemp, Log, TEXT("CreateAsset path = %s"), *AssetPath);FStreamableManager& EKAssetLoader = GGameInstance->GetStreamableManager();uoTmp = EKAssetLoader.LoadSynchronous(ref, true);}return uoTmp;
}//使用案例1, 为UMG上面的UButton和UImage设置图片(直接代码写中文以及中文图片的命名方式的习惯不好,不要学我)
//UButton dynamic change normal image
UObject* pHovered_ContentAttach = UUtilsLibrary::CreateAsset(TEXT("Texture2D'/Game/Blueprints/UITextures/风险挂接-选中.风险挂接-选中'"));
Button_BottomType_Mode->WidgetStyle.Normal.SetResourceObject(pHovered_ContentAttach);//UImage dynamic change normal image
FString sImagePath = TEXT("Texture2D'/Game/Blueprints/UITextures/添加摄像头.添加摄像头'");
UObject* pImage = UUtilsLibrary::CreateAsset(sImagePath);
RETURN_IF_NULL(pImage);
Image_CameraAdd->Brush.SetResourceObject(pImage);//使用案例2, 结合CreateAsset在代码中创建一个UMG蓝图// 对于ScrollBox……等类似列表容器可先使用该种方式, 创建然后再做添加
UFUNCTION()
UUserWidget* CreateFromAsset(const FStringAssetReference& StringRef, ESPPreviewType emUIType, bool bAddToViewPort, int32 nZorder = 0);//实现
UUserWidget* USPUIManager::CreateFromAsset(const FStringAssetReference& StringRef, ESPPreviewType emUIType, bool bAddToViewPort, int32 nZorder)
{UUserWidget* pUI = nullptr;UObject* inObject = UUtilsLibrary::CreateAsset(StringRef.ToString());if (inObject){pUI = CreateWidget<UUserWidget>(GGameInstance, Cast<UClass>(inObject));if (pUI){if (bAddToViewPort){pUI->AddToViewport(nZorder);m_WidgetCache.Add(pUI);}if (Cast<USPUserWidget>(pUI)){Cast<USPUserWidget>(pUI)->PreviewTypeArray.Add(emUIType);}}}return pUI;
}//HowTo Use Sample
UUserWidget* pUW = GGameInstance->UIManager()->CreateFromAsset(StringRef, emUIType, false);
if(pUW) UGridSlot* pGSlot = SPGridPanel_Left->AddChildToGrid(pUW, row, column);
4>通过构造加载方式2
// set default pawn class to our Blueprinted character 构造函数中加载
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{DefaultPawnClass = PlayerPawnBPClass.Class;
}
5>通过构造加载方式3 LoadClass以及ConstructorHelpers::FClassFinder
//.h中声明一下 加载一个蓝图类
UPROPERTY()
TSubclassOf<class AActor> BP_1;//构造函数中实现, 加载一个蓝图类
BP_1 = LoadClass<AActor>(NULL, TEXT("Blueprint'/Game/BP/MeshBP_1.MeshBP_C'"));//.h中声明一下 加载一个UMG
UPROPERTY()
TSubclassOf<UUserWidget> UIMain;//构造函数中实现, 加载一个UMG, 这里可以注意并没有写_C
static ConstructorHelpers::FClassFinder<UUserWidget> MYWidget(TEXT("/Game/UMG/UI_Main"));
UIMain_Instance = MYWidget.Class;
6>通过构造函数内Load资源 进行资源加载 LoadObject
// 红.h中声明一下
UPROPERTY()
UMaterialInstance* MaterialInstance_Level1;// Sets default values 构造函数中通过LoadObject加载
UWorldActorsManager::UWorldActorsManager(): m_pSelectedActor(nullptr)
{mapMaterials.Reset();/*MaterialInstance_Level1 = LoadObject<UMaterialInstance>(NULL, TEXT("/Game/Material/ColorMatreial_Inst_1.ColorMatreial_Inst_1"), NULL, LOAD_None, NULL);MaterialInstance_Level2 = LoadObject<UMaterialInstance>(NULL, TEXT("/Game/Material/ColorMatreial_Inst_2.ColorMatreial_Inst_2"), NULL, LOAD_None, NULL);MaterialInstance_Level3 = LoadObject<UMaterialInstance>(NULL, TEXT("/Game/Material/ColorMatreial_Inst_3.ColorMatreial_Inst_3"), NULL, LOAD_None, NULL);MaterialInstance_Level4 = LoadObject<UMaterialInstance>(NULL, TEXT("/Game/Material/ColorMatreial_Inst_4.ColorMatreial_Inst_4"), NULL, LOAD_None, NULL);*/
}
7>如何加载一张磁盘上的Png、jpg、jpeg、bmp到UMG上
//如何加载一张磁盘上的Png、jpg、jpeg、bmp到UMG上 头文件
UFUNCTION(BlueprintCallable, Category = "Utils")
static UTexture2D* LoadTexture(FString fullPath);//具体实现
UTexture2D* UUtilsLibrary::LoadTexture(FString fullPath)
{RETURN_NULL_IF_FALSE(FPaths::FileExists(fullPath));TArray<uint8> RawFileData;UTexture2D* MyTexture = NULL;RETURN_NULL_IF_FALSE(FFileHelper::LoadFileToArray(RawFileData, *fullPath));IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));// Note: PNG format.EImageFormat format = EImageFormat::Invalid;if (fullPath.EndsWith(TEXT(".png"))){format = EImageFormat::PNG;}else if (fullPath.EndsWith(TEXT(".jpg")) || fullPath.EndsWith(TEXT(".jpeg"))){format = EImageFormat::JPEG;}else if (fullPath.EndsWith(TEXT(".bmp"))){format = EImageFormat::BMP;}TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(format);RETURN_NULL_IF_FALSE(ImageWrapper.IsValid());RETURN_NULL_IF_FALSE(ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()));TArray<uint8> UncompressedBGRA;RETURN_NULL_IF_FALSE(ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA));// Create the UTexture for renderingUTexture2D* result = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);// Fill in the source data from the filevoid* TextureData = result->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());result->PlatformData->Mips[0].BulkData.Unlock();// Update the rendering resource from data.result->UpdateResource();return result;}//使用案例 注意是Full路径,绝对路径
UTexture2D* pTexture2D = UUtilsLibrary::LoadTexture(m_pRuntimeHotPoint->GetObjInfo()->GetBindMoviePath() + TEXT(".jpg"));
如有错误还请纠正。
谢谢,创作不易,大侠请留步… 动起可爱的双手,来个赞再走呗 <( ̄︶ ̄)>
这篇关于关于UE4动态资源加载(蓝图类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!