相关内容:
C++静态加载问题:ConstructorHelpers::FClassFinder()和FObjectFinder()
http://aigo.iteye.com/blog/2281373
C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例
http://aigo.iteye.com/blog/2268056
动态加载UObject和动态加载UClass的两个全局函数分别是:
LoadClass<T>() 、LoadObject<T>(),均在在UObjectGlobals.h中
// 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);
使用示例:
UTexture2D* Tex = LoadObject<UTexture2D>(NULL, TEXT("Texture2D'/Game/Textures/UI/tex_test001'"));
另外有两个全局函数叫:StaticLoadObject()和StaticLoadClass(),应该是LoadObject<T>()和LoadClass<T>()的早期版本,前者需要手动强转,后者使用模版封装过,使用更方便,推荐使用后者