中動態路徑載入 UE4靜態 動態載入資源方式

2021-10-14 18:52:14 字數 3401 閱讀 5121

ue4靜態/動態載入資源方式

本文將詳細介紹使用ue4靜態載入和動態載入的實現方式
靜態載入指的是在建構函式中完成的載入方式,這種方式的弊端明顯,就是需要寫死路徑,一旦改變路徑讀取失敗很容易造成程式崩潰api介面:constructorhelpers::fclassfinder()和fobjectfinder()以下為測試**:建立乙個actor類

#pragma once

#include "coreminimal.h"

#include "gameframework/actor.h"

#include "myactor.generated.h"

uclass()

class twodtest_api amyactor : public aactor;

核心**為

#include "uobject/constructorhelpers.h"

//以下需要寫在建構函式中

utexture2d *texture;

static constructorhelpers::fobjectfindertexture(text("/game/startercontent/textures/t_spark_core.t_spark_core"));

//安全判定

if (texture.succeeded())

#include "myactor.h"

#include "uobject/constructorhelpers.h"

// sets default values

amyactor::amyactor()

}// called when the game starts or when spawned

void amyactor::beginplay()

// called every frame

void amyactor::tick(float deltatime)

api介面:loadobject vs staticloadobject

這種方式未提供藍圖介面,要想在藍圖中呼叫需要用c++進行封裝

動態載入一般使用loadclass()和loadobject() 這兩個api,示例如下:

umaterial *mt = loadobject(nullptr, text("/game/map/materials/grass.grass"));

umaterial *mt = cast(staticloadobject(umaterial::staticclass(), nullptr, text("/game/map/materials/grass.grass")));

loadobject和staticloadobject的區別:

用vs速覽定義發現,loadobject是對staticloadobject的一層封裝

有人會認為loadobject要比staticloadobject的速度快一點,畢竟又多封裝一次,但實際上我們發現,loadobject使用了inline進行修飾,因此實際和staticloadobject沒什麼區別,只是引數不一樣而已。#pragma once

#include "coreminimal.h"

#include "kismet/blueprintfunctionlibrary.h"

#include "engine/texture2d.h"

#include "loadfile.generated.h"

/** *

*/uclass()

class twodtest_api uloadfile : public ublueprintfunctionlibrary};

點一下vs的生成和ue4 的compile就能在藍圖中呼叫了:

loadclass()和loadobject()的區別如下:

使用可參考:

loadclass()和loadobject()

藍圖提供async load asset非同步載入資源的介面,可以直接使用,比如要載入texture:

注意,new var 0是soft object型別變數,context下的任意資源都能為它賦值。

以下為字串轉soft object refernce方式

但是該方法只能在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'

ue4 動態載入資源

動態載入非藍圖資源 如 聲音等使用loadobject 資源型別 nullptr,text 資源路徑引用 載入藍圖資源 獲得藍圖類 loadclass 藍圖的型別 nullptr,text 資源路徑引用 c 這裡需要注意就是資源的路徑需要額外加上 c 例如uclass result1 loadcla...

UE4之Delegate 動態單播

定義 define func concat va args 定義乙個動態單播,uht會根據body macro combine生成乙個file id line delegate巨集,這個函式留到後面說。先看func declare dynamic delegate 巨集定義 define func ...

UE4 動態陣列 TArray容器

為什麼使用ue4提供的容器類?如果你用過c 的stl庫,你就知道stl提供了各種各樣的容器 資料結構,使得你對處理很多資料的時候非常快捷高效。ue4同樣也提供了類似的庫,庫裡面的型別是以t開頭的,使用ue4提供的容器庫可以更好地實現跨平台。所以在ue4進行開發的時候我們很少去使用stl容器,更多時候...