zoukankan      html  css  js  c++  java
  • [UE4]C++实现动态加载的问题:LoadClass()和LoadObject()

    http://aigo.iteye.com/blog/2281558

    原文作者:@玄冬Wong 

     

    相关内容:
    C++静态加载问题:ConstructorHelpers::FClassFinder()和FObjectFinder() 

    http://aigo.iteye.com/blog/2281373

    C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例

    http://aigo.iteye.com/blog/2268056

     

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

     

    另外注意:LoadClass<T>的模版名称,不能直接写UBlueprint,例如:LoadClass<UBlueprint>是错误的,创建蓝图时选择的是什么父类,则写对应的父类名,假如是Actor,那么要写成:LoadClass<AActor>,否则无法加载成功。

    路径名也必须带_C后缀(LoadObject不需要带_C后缀),例如,蓝图路径是:Blueprint'/Game/Blueprints/MyBP.MyBP'

    加后缀以后,则是:Blueprint'/Game/Blueprints/MyBP.MyBP_C',

    例子:

     

    Cpp代码  收藏代码
    1. UClass* Test = LoadClass<AActor>(NULL, TEXT("Blueprint'/Game/Blueprints/MapPathBrush_BP.MapPathBrush_BP_C'"));  
     

     

    官方还没出文档,只能先看代码注释:

    Cpp代码  收藏代码
    1. // Load an object.  
    2. templateclass T >   
    3. inline T* LoadObject( UObject* Outer, const TCHAR* Name, const TCHAR* Filename=nullptr, uint32 LoadFlags=LOAD_None, UPackageMap* Sandbox=nullptr )  
    4. {  
    5.     return (T*)StaticLoadObject( T::StaticClass(), Outer, Name, Filename, LoadFlags, Sandbox );  
    6. }  

     

    Cpp代码  收藏代码
    1. // Load a class object.  
    2. templateclass T >   
    3. inline UClass* LoadClass( UObject* Outer, const TCHAR* Name, const TCHAR* Filename=nullptr, uint32 LoadFlags=LOAD_None, UPackageMap* Sandbox=nullptr )  
    4. {  
    5.     return StaticLoadClass( T::StaticClass(), Outer, Name, Filename, LoadFlags, Sandbox );  
    6. }  

     

    Cpp代码  收藏代码
    1. /** 
    2.  * Find or load an object by string name with optional outer and filename specifications. 
    3.  * These are optional because the InName can contain all of the necessary information. 
    4.  * 
    5.  * @param ObjectClass   The class (or a superclass) of the object to be loaded. 
    6.  * @param InOuter       An optional object to narrow where to find/load the object from 
    7.  * @param InName        String name of the object. If it's not fully qualified, InOuter and/or Filename will be needed 
    8.  * @param Filename      An optional file to load from (or find in the file's package object) 
    9.  * @param LoadFlags     Flags controlling how to handle loading from disk 
    10.  * @param Sandbox       A list of packages to restrict the search for the object 
    11.  * @param bAllowObjectReconciliation    Whether to allow the object to be found via FindObject in the case of seek free loading 
    12.  * 
    13.  * @return The object that was loaded or found. NULL for a failure. 
    14.  */  
    15. 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 );  
    16. COREUOBJECT_API UClass* StaticLoadClass(UClass* BaseClass, UObject* InOuter, const TCHAR* Name, const TCHAR* Filename = NULL, uint32 LoadFlags = LOAD_None, UPackageMap* Sandbox = NULL);  

     

     

     LoadObject加载例子,不需要添加后缀:

    Cpp代码  收藏代码
    1. 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*然后再强转为具体的类型,例如:

    Cpp代码  收藏代码
    1. UObject* Obj = LoadObject<UObject>(NULL, TEXT("SkeletalMesh'/Game/MyMesh.MyMesh'"));  
    2. USkeletalMesh* MyMesh = Cast<USkeletalMesh*>(Obj);  

     

      

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

  • 相关阅读:
    Delphi开发动态连接库的方法和规范
    EntityFramework+DomainDataSource+Silverlight完成数据读取分页排序与修改
    啊啊啊啊
    HTML、CSS、JavaScript从零开始系列文章
    从JAVA学思想,在.Net用……关于各种模型,备案以便查
    为ExtJS 4 系列树添加可将节点拖动到叶子节点上的功能,可配置
    在使用WCF RIA Services时所要注意的,不断更新中……
    修复EXTJS 4.0.2a在ie9与FIREFOX下字体过小、表格头部字体在Chrome下模糊的CSS补丁
    修复EXTJS 4.0.2a下面gridFilter的type为list时,不能从服务器读取列表数据的bug
    大学生程序员,"我们将要何去何从?!"
  • 原文地址:https://www.cnblogs.com/nafio/p/9137069.html
Copyright © 2011-2022 走看看