打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例

相关内容:

C++实现动态加载的问题:LoadClass<T>()和LoadObject<T>()

http://aigo.iteye.com/blog/2281558
C++静态加载问题:ConstructorHelpers::FClassFinder()和FObjectFinder() 

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

 

示例1:

动态加载Object的工具方法

Cpp代码
 
  1. UTexture2D* MyTextureLoader::LoadTextureFromPath(const FString& Path)  
  2. {  
  3.     if (Path.IsEmpty()) return NULL;  
  4.   
  5.     return Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *(Path)));  
  6. }  
调用:
Cpp代码
 
  1. FString PathToLoad = "/Game/Textures/YourStructureHere";  
  2. UTexture2D* tmpTexture = LoadTextureFromPath(PathToLoad);  
 

 

 

示例2:
加载MaterialTexture

Cpp代码
 
  1. struct FConstructorStatics  
  2.  {  
  3.      ConstructorHelpers::FObjectFinderOptional<UTexture> TextureFinder;  
  4.      ConstructorHelpers::FObjectFinderOptional<UMaterial> MaterialFinder;  
  5.      FConstructorStatics()  
  6.          : TextureFinder(TEXT("Texture2D'/Game/Textures/2DBackground.2DBackground'"))  
  7.          , MaterialFinder(TEXT("Material'/Game/Materials/DynamicTextureMaterial.DynamicTextureMaterial'"))  
  8.      {  
  9.      }  
  10.  };  
  11.  static FConstructorStatics ConstructorStatics;  
  12.    
  13.  Texture = ConstructorStatics.TextureFinder.Get();  
  14.  UMaterial* Material = ConstructorStatics.MaterialFinder.Get();  
  15.  DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);  
 

设置调用加载好的Material和Texture:

Cpp代码
 
  1. DynamicMaterial->SetTextureParameterValue(FName("DynamicTexture"), Texture);  
  2. Mesh->SetMaterial(0, DynamicMaterial);  
 

如果资源永不再使用,想销毁资源对象,代码如下:

Cpp代码
 
  1. Texture2D* mytex; //这里假设mytex合法有效  
  2.   
  3. mytex->ConditionalBeginDestroy();  
  4. mytex = NULL;  
  5. GetWorld()->ForceGarbageCollection(true);  

  

 

Dynamic Asset Loading with C++

https://www.youtube.com/watch?v=pJIAmSGxfmQ

 

Dynamic Load Object

https://wiki.unrealengine.com/Dynamic_Load_Object

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Uni2D 入门 -- Atlas
手把手带你实现抖音短视频特效
Мобильный LiveInternet Texture | гилана
摄像机目标渲染纹理targetTexture
map、mesh、texture、material 和 shader 的联系
Quixel SUITE纹理绘制入门之一:简介
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服