read pixel value from a UTexture at runtime

Hi everyone, I want to be able to read the values from a material that uses the distanceToNearestSurfaceNode, to achieve this my logic is the following:

  1. Create mesh
  2. Attach mesh to Actor
  3. Pass material to that mesh
  4. Get texture from material
  5. Save texture data in memory
  6. Read texture data in an x / y position.

Im new to UE4, but reading the API and lots of forum posts I was able to get to step 4, however I haven’t find a way to save the UTexture to memory. I did find a way to save the data of an UTexture2D, but it didn’t work with my UTexture, so I think I’m missing a step somewhere.

My code is the following

UStaticMeshComponent *DistanceField;


DistanceField = NewObject<UStaticMeshComponent>(this, TEXT("Static Mesh"));
const ConstructorHelpers::FObjectFinder<UStaticMesh> MeshObj(TEXT("/Engine/BasicShapes/Cube"));
const ConstructorHelpers::FObjectFinder<UMaterial> MeshMat(TEXT("/Game/Textures/m_DistanceField.m_DistanceField"));

DistanceField->SetRelativeScale3D(FVector(100.0f, 100.0f, 0.0f));
DistanceField->SetMaterial(0, MeshMat.Object);


//get mesh material textures
TArray <UTexture * > MyTextures;
DistanceField->GetUsedTextures(MyTextures, EMaterialQualityLevel::High);

//I think this is the missing step or something like this
UTexture2D *aTexture = MyTextures[0];

//save values in memory
FTexture2DMipMap * MyMipMap = &aTexture->PlatformData->Mips[0];
FByteBulkData * RawImageData = &MyMipMap->BulkData;
FColor* FormatedImageData = static_cast<FColor*>(RawImageData->Lock(LOCK_READ_ONLY));
//read pixel (actual)
uint8 PixelX = 5, PixelY = 10;
uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
FColor PixelColor;

if (PixelX >= 0 && PixelX < TextureWidth && PixelY >= 0 && PixelY < TextureHeight)
PixelColor = FormatedImageData[PixelY * TextureWidth + PixelX];

Thanks in advance