When creating terrain, use ALandscape::Improt API to import, the layer information setting failed

When using the ALandscape::Improt API,I want to set the layer information to the layer I saved before,but my setting fails, Below is my code.


void FImpotLandModule::CreatLandScape(FString heightMapPath)
{

	UWorld* world = GEditor->GetEditorWorldContext().World();
	FVector location(0.0f, 0.0f, 0.0f);
	FRotator rotator(0.0f, 0.0f, 0.0f);
	FActorSpawnParameters SpawnInfo;
	ALandscape* landscape = world->SpawnActor<ALandscape>(location, rotator);
	ALandscapeProxy* proxy = Cast<ALandscapeProxy>(landscape);
	int section_size = 63;
	int section_per_component = 2;
	int component_x = 4;
	int component_y = 4;
	int quads_per_component = section_per_component * section_size;
	int size_x = component_x * quads_per_component + 1;
	int size_y = component_y * quads_per_component + 1;
	TArray<uint16> ImportLandscape_Data;
	ILandscapeEditorModule& editorModule = FModuleManager::GetModuleChecked<ILandscapeEditorModule>("LandscapeEditor");
	const ILandscapeHeightmapFileFormat* heightmapFormat = editorModule.GetHeightmapFormatByExtension(*FPaths::GetExtension(heightMapPath,true));
	if (heightmapFormat)
	{
		FLandscapeHeightmapImportData HeightmapImportData = heightmapFormat->Import(*heightMapPath, { 505,505 });
		ImportLandscape_Data = MoveTemp(HeightmapImportData.Data);
	}
	
	TMap<FGuid,TArray<uint16>> heightmapDataPerLayers;
	heightmapDataPerLayers.Add(FGuid(), ImportLandscape_Data);
	TMap<FGuid,TArray<FLandscapeImportLayerInfo>> landscapeLayersInfo;
	TArray<FLandscapeImportLayerInfo> importLandInfo;
	UMaterialInterface* landscapeMaterial = LoadObject<UMaterialInterface>(nullptr,TEXT("/Game/mapmaterial"));
	proxy->LandscapeMaterial = landscapeMaterial;

	if (landscapeMaterial != nullptr)
	{
		TArray<FName> layerNames = ALandscapeProxy::GetLayersFromMaterial(landscapeMaterial);
		importLandInfo.Reserve(layerNames.Num());
		for (int32 i = 0; i < layerNames.Num() - 1; ++i)
		{
			FLandscapeImportLayerInfo ILayerInfo = FLandscapeImportLayerInfo(layerNames[i]);
			ULandscapeLayerInfoObject* newLayerInfo = NewObject<ULandscapeLayerInfoObject>(proxy);
			FString layerInfoPath = "/Game/0" + FString::FromInt(i + 1) + "_LayerInfo";
			newLayerInfo = LoadObject<ULandscapeLayerInfoObject>(NULL, *layerInfoPath);
			ILayerInfo.LayerInfo = newLayerInfo;
			ILayerInfo.SourceFilePath = "d:/0" + FString::FromInt(i + 1) + ".png";
			importLandInfo.Add(MoveTemp(ILayerInfo));
		}
	}
	landscapeLayersInfo.Add(FGuid(), importLandInfo);
	proxy->Import(FGuid::NewGuid(),0,0,size_x - 1,size_y - 1,section_per_component,section_size,heightmapDataPerLayers,TEXT("MapHeightMap"),landscapeLayersInfo,ELandscapeImportAlphamapType::Additive);
}

The following is the result after running, you can see that my layer information setting failed,please help me.

I get it,If anyone is interested, you can find me.

1 Like

Please help, how did you do it?

man u there?