UE4 创建动态材质实例 Asset
Game
状态下对应的动态材质实例是UMaterialInstance
。
Editor
状态下对应的动态材质实例UMaterialInstanceConstant
。
因此UE4创建可编辑的动态材质实例Asset资源的话,需要创建UMaterialInstanceConstant
。同时需要将"UnrealEd"模块添加到对应的xxxxx.Build.cs中。
Code示例
FString PackageName = "/Game/Mesh/";
PackageName += materialId;
UPackage* Package = CreatePackage(NULL, *PackageName);
UMaterialInstanceConstantFactoryNew* Factory = NewObject<UMaterialInstanceConstantFactoryNew>();
UMaterialInstanceConstant* Instance = CastChecked<UMaterialInstanceConstant>(
Factory->FactoryCreateNew(
UMaterialInstanceConstant::StaticClass(),
Package, *materialId, RF_Standalone | RF_Public, NULL, GWarn));
//Set data
if (IsHasCutoutMap(content))
Instance->SetParentEditorOnly(pCutoutMaterial);
else
Instance->SetParentEditorOnly(pGlassMaterial);
Instance->SetVectorParameterValueEditorOnly(TEXT("DiffuseColor"), baseColor);
Instance->SetScalarParameterValueEditorOnly(TEXT("Transparency"), content.tp);
Instance->SetScalarParameterValueEditorOnly(TEXT("Glossiness"), content.Glossness);
//save asset
Instance->MarkPackageDirty();
Instance->PreEditChange(nullptr);
FAssetRegistryModule::AssetCreated(Instance);
Instance->PostEditChange();