简体   繁体   中英

UE4: Set texture to material (C++)

I'm new to unreal coding, I want to apply texture to material, but I'm stuck with creating:

    UMaterialInterface* Material = CreateDefaultSubobject<UMaterial>(TEXT("MyMaterial"));
    UMaterialInstanceDynamic* DynamicMatierial = UMaterialInstanceDynamic::Create(Material, this);

I've got this error message:

NewObject with empty name can't be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSuobject<> instead.

This error comes from second line. Any idea why this error occurs? Or is there any other easy way to apply texture to material?

Ok, I've solved this error, this line of can't be used in constructor:

UMaterialInstanceDynamic* DynamicMatierial = UMaterialInstanceDynamic::Create(Material, this);

Move it to PostLoad or other lifecycle method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM