UE4静态/动态加载资源方式
本文将详细介绍使用UE4静态加载和动态加载的实现方式
静态加载
ConstructorHelpers::FClassFinder()和FObjectFinder()
静态加载指的是在构造函数中完成的加载方式,这种方式的弊端明显,就是需要写死路径,一旦改变路径读取失败很容易造成程序崩溃
API接口:ConstructorHelpers::FClassFinder()和FObjectFinder()
以下为测试代码:创建一个Actor类
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class TWODTEST_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
核心代码为
#include "UObject/ConstructorHelpers.h"
//以下需要写在构造函数中
UTexture2D *texture;
static ConstructorHelpers::FObjectFinder<UTexture2D> Texture(TEXT("/Game/StarterContent/Textures/T_Spark_Core.T_Spark_Core"));
//安全判定
if (Texture.Succeeded())
{
texture = Texture.Object;
}
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyActor.h"
#include "UObject/ConstructorHelpers.h"
// Sets default values
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
UTexture2D *texture;
static ConstructorHelpers::FObjectFinder<UTexture2D> Texture(TEXT("/Game/StarterContent/Textures/T_Spark_Core.T_Spark_Core"));
if (Texture.Succeeded())
{
texture = Texture.Object;
}
}
// Called when the game starts or when spawned
void AMyActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
动态加载
LoadObject() vs StaticLoadObject()
API接口:LoadObject vs StaticLoadObject
这种方式未提供蓝图接口,要想在蓝图中调用需要用C++进行封装
动态加载一般使用LoadClass()和LoadObject() 这两个API,示例如下:
UMaterial *mt = LoadObject<UMaterial>(nullptr, TEXT("/Game/Map/Materials/grass.grass"));
UMaterial *mt = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullptr, TEXT("/Game/Map/Materials/grass.grass")));
LoadObject和StaticLoadObject的区别:
用VS速览定义发现,LoadObject是对StaticLoadObject的一层封装
有人会认为LoadObject要比StaticLoadObject的速度快一点,毕竟又多封装一次,但实际上我们发现,LoadObject使用了inline进行修饰,因此实际和StaticLoadObject没什么区别,只是参数不一样而已。
实例测试:
创建一个FunctionLibrary,写入以下代码:(主要参考LoadTextureFromPath函数实现)
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Engine/Texture2D.h"
#include "LoadFile.generated.h"
/**
*
*/
UCLASS()
class TWODTEST_API ULoadFile : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
UFUNCTION(BlueprintCallable, meta = (DisplayName = "LoadTextureFromPath", keywords = "Load"), Category = "LoadFile")
UTexture2D* LoadTextureFromPath(const FString& Path)
{
if (Path.IsEmpty()) return NULL;
return Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *(Path)));
}
};
点一下VS的生成和UE4 的compile就能在蓝图中调用了:
LoadClass()和LoadObject()
LoadClass()和LoadObject()的区别如下:
- LoadObject()用来加载非蓝图资源,如贴图;
- LoadClass()用来加载蓝图并获取蓝图类;
使用可参考:
异步加载
蓝图提供Async Load Asset异步加载资源的接口,可以直接使用,比如要加载texture:
注意,New Var 0是Soft Object类型变量,Context下的任意资源都能为它赋值。
以下为字符串转Soft Object Refernce方式
编辑器加载
编辑器中提供了Load Asset蓝图接口,对应的代码在EditorAssetLibrary下:
但是该方法只能在Editor Utilities下使用,不能在继承Actor的蓝图中调用
创建一个Editor Utility Widget:
注意事项
StaticLoadObject()函数的对应路径写法( 自己就在路径上捣鼓半天才找到了正确的路径写法,其他博客根本不做介绍 )
实例:
取Content/Texture/jianpan_c时,路径应该写/Game/Texture/jianpan_c,以/Game开头(Content下),不管你的项目名字是什么,然后不要有/Content这个部分,直接接上后面的路径才能成功读取到图片。
鼠标移到对应的资源上,就可以查看路径了:
或者直接右键:
大钊老师整理的路径规则:
同时,如果使用LoadClass()方法,路径名也必须带_C后缀(LoadObject不需要带_C后缀),例如,蓝图路径是:Blueprint’/Game/Blueprints/Test’,
加后缀以后,则是:Blueprint’/Game/Blueprints/Test_C’,