[UE4]C++实现动态加载的问题:LoadClass<t>()和LoadObject<t>()

发表于2018-02-26
评论0 1.2k浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏程序行业精英群

711501594
动态加载UObject和动态加载UClass分别用LoadObject<T>(),和LoadClass<T>() ,两者均在在UObjectGlobals.h中。

区别:
LoadObject<T>()用来加载非蓝图资源,比如动画、贴图、音效等资源;
LoadClass<T>()用来加载蓝图并获取蓝图Class,比如角色蓝图。如果要用蓝图创建对象,必须先通过LoadClass获取class,然后再通过SpawnActor生成对象。

LoadClass参数注意事项:
另外注意:LoadClass<T>的模版名称,不能直接写UBlueprint,例如:LoadClass<UBlueprint>是错误的,创建蓝图时选择的是什么父类,则写对应的父类名,假如是Actor,那么要写成:LoadClass<AActor>,否则无法加载成功。
路径名也必须带_C后缀(LoadObject不需要带_C后缀),例如,蓝图路径是:Blueprint'/Game/Blueprints/MyBP.MyBP',
加后缀以后,则是:Blueprint'/Game/Blueprints/MyBP.MyBP_C',

例子:
UClass* Test = LoadClass<AActor>(NULL, TEXT("Blueprint'/Game/Blueprints/MapPathBrush_BP.MapPathBrush_BP_C'"));  

官方还没出文档,只能先看代码注释:
// Load an object.  
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 );  
}  
// Load a class object.  
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 );  
}  
/** 
 * Find or load an object by string name with optional outer and filename specifications. 
 * These are optional because the InName can contain all of the necessary information. 
 * 
 * @param ObjectClass   The class (or a superclass) of the object to be loaded. 
 * @param InOuter       An optional object to narrow where to find/load the object from 
 * @param InName        String name of the object. If it's not fully qualified, InOuter and/or Filename will be needed 
 * @param Filename      An optional file to load from (or find in the file's package object) 
 * @param LoadFlags     Flags controlling how to handle loading from disk 
 * @param Sandbox       A list of packages to restrict the search for the object 
 * @param bAllowObjectReconciliation    Whether to allow the object to be found via FindObject in the case of seek free loading 
 * 
 * @return The object that was loaded or found. NULL for a failure. 
 */  
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参数注意事项:
LoadObject加载例子,不需要添加后缀:
UTexture2D* Tex = LoadObject<UTexture2D>(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*然后再强转为具体的类型,例如:
UObject* Obj = LoadObject<UObject>(NULL, TEXT("SkeletalMesh'/Game/MyMesh.MyMesh'"));  
USkeletalMesh* MyMesh = Cast<USkeletalMesh*>(Obj);  

另外有两个全局函数叫:StaticLoadObject()和StaticLoadClass(),应该是LoadObject<T>()和LoadClass<T>()的早期版本,前者需要手动强转,后者使用模版封装过,使用更方便,推荐使用后者
来自:http://aigo.iteye.com/blog/2281558

原文链接

著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

游戏学院公众号二维码
腾讯游戏学院
微信公众号

提供更专业的游戏知识学习平台