When i load a pak file with mesh and texture(runtime),the texture lost mipmap

i baked asstes in editor,after i Released the game i discover when i load pak file the mipmap was lost。it only load one level of mipmap and not the hight res one。so i change my way,before i bake asstes i change the texture seting to nomipmap and bake again。this time it does load right but only the high res level in there。the result with nomipmap is not well too,a lot moire phenomenon on the texture。so i try to fix this and i find a way,use a function to creat mipmap for the texture。

UTexture* Calculate_Mipmap(UTexture* texture)
{
	int mipsToAdd = 3;
	TArray<uint8> _mipRGBAs;
	UTexture2D* texture2d = Cast<UTexture2D>(texture);
	if (texture2d->PlatformData->Mips.Num() > 1)
	{
		return texture;
	}
	GEngine->AddOnScreenDebugMessage(0, 1, FColor::Blue, FString::FromInt(texture2d->PlatformData->Mips[0].BulkData.GetBulkDataSize()));
	const uint8 *priorData = (const uint8 *)texture2d->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
	int priorwidth = texture2d->PlatformData->Mips[0].SizeX;
	int priorheight = texture2d->PlatformData->Mips[0].SizeY;
	while (mipsToAdd > 0)
	{
		TArray<uint8> *mipRGBAs = &_mipRGBAs;
		int mipwidth = priorwidth >> 1;
		int mipheight = priorheight >> 1;
		if ((mipwidth == 0) || (mipheight == 0))
		{
			break;
		}
		mipRGBAs->Reset();
		mipRGBAs->AddUninitialized(mipwidth * mipheight * BYTES_PER_PIXEL);
		int dataPerRow = priorwidth * BYTES_PER_PIXEL;
		uint8 *dataOut = mipRGBAs->GetData();
		for (int y = 0; y < mipheight; y++)
		{
			const uint8 *dataInRow0 = priorData + (dataPerRow * y /** 2*/);
			const uint8 *dataInRow1 = dataInRow0 + dataPerRow;
			for (int x = 0; x < mipwidth; x++)
			{
				int totalB = *dataInRow0++;
				int totalG = *dataInRow0++;
				int totalR = *dataInRow0++;
				//int totalA = *dataInRow0++;

				totalB += *dataInRow0++;
				totalG += *dataInRow0++;
				totalR += *dataInRow0++;
				//totalA += *dataInRow0++;

				totalB += *dataInRow1++;
				totalG += *dataInRow1++;
				totalR += *dataInRow1++;
				//totalA += *dataInRow1++;

				totalB += *dataInRow1++;
				totalG += *dataInRow1++;
				totalR += *dataInRow1++;
				//totalA += *dataInRow1++;

				totalB >>= 2;
				totalG >>= 2;
				totalR >>= 2;
				//totalA >>= 2;

				*dataOut++ = (uint8)totalB;
				*dataOut++ = (uint8)totalG;
				*dataOut++ = (uint8)totalR;
				//*dataOut++ = (uint8)totalA;
			}
			//dataInRow0 += dataPerRow * 2;
			//dataInRow1 += dataPerRow * 2;
		}

		FTexture2DMipMap* Mip = new(texture2d->PlatformData->Mips) FTexture2DMipMap();
		Mip->SizeX = mipwidth;
		Mip->SizeY = mipheight;
		Mip->BulkData.Lock(LOCK_READ_WRITE);
		void* mipData = Mip->BulkData.Realloc(mipRGBAs->Num());
		FMemory::Memcpy(mipData, mipRGBAs->GetData(), mipRGBAs->Num());
		Mip->BulkData.Unlock();

		priorData = mipRGBAs->GetData();
		priorwidth = mipwidth;
		priorheight = mipheight;
		--mipsToAdd;
	}
	texture2d->PlatformData->Mips[0].BulkData.Unlock();
	texture->UpdateResourceW();
	
	return texture;
}

this is the C++ code,it crash everytime when the texture->UpdateResourceW() function excused。sometime it crash when get next RGBA value the pointer out of bounds。
so dose any have a solution how to load mipmap right or help me fix the code issue。
one more thing,my English not so well,so if you find some grammatical mistakes in this describe please don’t mind。if you don’t understand what i am take about please let me know i will describe more carefuly。