UnrealEngine:加载

发布时间 2023-06-14 17:26:04作者: 言午丶

静态加载

ConstructorHelpers::FClassFinder() FObjectFinder()

在构造函数中完成加载,无法改变路径

 

UTexture2D* texture;

static ConstructorHelpers::FObjectFinder<UTexture2D> Texture(TEXT("/Game/StarterContent/Textures/T_Spark_Core"));

if (Texture.Successed())

{

  texture = Texture.Object;

}

 

 

动态加载

LoadObject()  StaticLoadObject()

LoadObject()用来加载非蓝图资源,如贴图

LoadClass()用来加载蓝图并获取蓝图类

UMaterial* mt = LoadObject<UMaterial>(nullptr, TEXT("/Game/Map/Materials/grass.grass"));

UMaterial* mt = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), nullotrm, TEXT("/Game/Map/Materials/grass.grass")))

 

 

加载纹理

#include "CoreMinimal.h"

#include "Kismet/BlueprintFunctionLibrary.h"

#include "Engine/Texture2D.h"

#incldue "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)));

  }

}