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()用来加载蓝图并获取蓝图类;

使用可参考:

LoadClass()和LoadObject()

异步加载

蓝图提供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’,

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值