動態(tài)加載UObject和動態(tài)加載UClass分別用LoadObject(),和LoadClass() ,兩者均在在UObjectGlobals.h中。
另外注意:LoadClass的模版名稱,不能直接寫UBlueprint,例如:LoadClass是錯誤的,創(chuàng)建藍(lán)圖時選擇的是什么父類,則寫對應(yīng)的父類名,假如是Actor,那么要寫成:LoadClass>,否則無法加載成功。
路徑名也必須帶_C后綴(LoadObject不需要帶_C后綴),例如,藍(lán)圖路徑是:Blueprint'/Game/Blueprints/MyBP.MyBP',
加后綴以后,則是:Blueprint'/Game/Blueprints/MyBP.MyBP_C',
例子:
-
UClass* Test = LoadClass(NULL, TEXT("Blueprint'/Game/Blueprints/MapPathBrush_BP.MapPathBrush_BP_C'"));
官方還沒出文檔,只能先看代碼注釋:
-
-
template< class T >
-
inline T* LoadObject( UObject* Outer, const TCHAR* Name, const TCHAR* Filename=nullptr, uint32 LoadFlags=LOAD_None, UPackageMap* Sandbox=nullptr )
-
{
-
return (T*)StaticLoadObject( T::StaticClass(), Outer, Name, Filename, LoadFlags, Sandbox );
-
}
-
-
template< class T >
-
inline UClass* LoadClass( UObject* Outer, const TCHAR* Name, const TCHAR* Filename=nullptr, uint32 LoadFlags=LOAD_None, UPackageMap* Sandbox=nullptr )
-
{
-
return StaticLoadClass( T::StaticClass(), Outer, Name, Filename, LoadFlags, Sandbox );
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
COREUOBJECT_API UObject* StaticLoadObject( UClass* Class, UObject* InOuter, const TCHAR* Name, const TCHAR* Filename = NULL, uint32 LoadFlags = LOAD_None, UPackageMap* Sandbox = NULL, bool bAllowObjectReconciliation = true );
-
COREUOBJECT_API UClass* StaticLoadClass(UClass* BaseClass, UObject* InOuter, const TCHAR* Name, const TCHAR* Filename = NULL, uint32 LoadFlags = LOAD_None, UPackageMap* Sandbox = NULL);
LoadObject加載例子,不需要添加后綴:
-
UTexture2D* Tex = LoadObject(NULL, TEXT("Texture2D'/Game/Textures/UI/tex_test001.tex_test001'"));
可以用LoadObject加載的文件包括:
Texture、Material、SoundWave、SoundCue、ParticlesSystem、AnimMontage、BlendSpace(1D,2D,3D)、AnimSequence、AnimBlueprint、SkeletalMesh等等。這些文件的父類都是UObject,所以也可以先加載為UObject*然后再強(qiáng)轉(zhuǎn)為具體的類型,例如:
-
UObject* Obj = LoadObject(NULL, TEXT("SkeletalMesh'/Game/MyMesh.MyMesh'"));
-
USkeletalMesh* MyMesh = Cast(Obj);
另外有兩個全局函數(shù)叫:StaticLoadObject()和StaticLoadClass(),應(yīng)該是LoadObject()和LoadClass()的早期版本,前者需要手動強(qiáng)轉(zhuǎn),后者使用模版封裝過,使用更方便,推薦使用后者