本文将详细介绍使用 UE4 静态加载 和 动态加载 的实现方式
静态加载
ConstructorHelpers::FClassFinder() 和 ConstructorHelpers::FObjectFinder()
静态加载 指的是在构造函数中完成的加载方式,这种方式的弊端明显,就是需要写死路径,一旦改变路径读取失败很容易造成程序崩溃
API 接口:ConstructorHelpers::FClassFinder()
和 ConstructorHelpers::FObjectFinder()
以下为测试代码:创建一个 Actor 类
.h 文件
// 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;
};
.cpp 核心代码为
#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;
}
.cpp 完整代码为
// 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;
/// @note StaticLoadObject
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