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

[UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例
相关内容:

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

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

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

示例1:

动态加载Object的工具方法

UTexture2D* MyTextureLoader::LoadTextureFromPath(const FString& Path)  
{  
    if (Path.IsEmpty()) return NULL;  
  
    return Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *(Path)));  
}  

调用:

FString PathToLoad = "/Game/Textures/YourStructureHere";  
UTexture2D* tmpTexture = LoadTextureFromPath(PathToLoad);  

示例2:
加载Material和Texture

复制代码

struct FConstructorStatics  
 {  
     ConstructorHelpers::FObjectFinderOptional<UTexture> TextureFinder;  
     ConstructorHelpers::FObjectFinderOptional<UMaterial> MaterialFinder;  
     FConstructorStatics()  
         : TextureFinder(TEXT("Texture2D'/Game/Textures/2DBackground.2DBackground'"))  
         , MaterialFinder(TEXT("Material'/Game/Materials/DynamicTextureMaterial.DynamicTextureMaterial'"))  
     {  
     }  
 };  
 static FConstructorStatics ConstructorStatics;  
   
 Texture = ConstructorStatics.TextureFinder.Get();  
 UMaterial* Material = ConstructorStatics.MaterialFinder.Get();  
 DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);  

复制代码
设置调用加载好的Material和Texture:

DynamicMaterial->SetTextureParameterValue(FName("DynamicTexture"), Texture);  
Mesh->SetMaterial(0, DynamicMaterial);  

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

Texture2D* mytex; //这里假设mytex合法有效  
  
mytex->ConditionalBeginDestroy();  
mytex = NULL;  
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值