Change texture of material instance on runtime

Good Day, Friends!

For my memory game I want to swap Textures/Pictures at runtime in my Material instance.

  • I hava a M_Card, with TextureSampleParameter2D
  • I have MI_Card, that is applied to BP_Card with some default values
  • Upon function call I want access available Textures/Images and apply it to MI_Card

The intention is, if I add an image (e.g. Image32.jpeg) to my content folder, I can access that in my BP_Card and replace the texture of MI_Card. BP_Card has a value (e.g. 32) and will access and load Image<BP_Card_Value>.jpeg to insert into MI_Card.

Or in general:
How can I achieve asset generation based on the Content folder? Is this even a practice?

Best Regards and Thank you in advance!

Hello, not sure I understand the problem clearly but do correct me if necessary.

That is a practice yes. Basically you want to change image on runtime which you already have structure but you want to access images in a folder.

you can basically check assets like this and get images on folder and search through them

However if you are adding images to a folder and you want them to be imported to engine in runtime, thats another thing but can be done too. However I don’t think it is a good practice.

However there is some ways to do it. Here is anotheer practice if you want images imported on runtime since you just created them or they are somehow dynamic

Also in editor you can reimport assets like below however its editor dependent.

void USOMEOBJECT::ReImportImages(UTexture* TextureToImport)
{
	if (TextureToImport)
	{
		// Ensure the texture is updated
		TextureToImport->UpdateResource();

		// Get the import data and re-import the asset
		if (UAssetImportData* ImportData = TextureToImport->AssetImportData) // this is the UTexture
		{
			FString AssetPath = TextureToImport->GetPathName(); // Lets look its path
		
			if (ImportData->GetFirstFilename() != "") // if the path is not empty
			{
				if (FReimportManager::Instance()->Reimport(TextureToImport))
				{
					UE_LOG(LogTemp, Display, TEXT("Importer-> Texture Reimported Successfully."));
				}
			}
			else
			{
				UE_LOG(LogTemp, Warning, TEXT("Importer-> Unable to find source file for re-import."));
			}
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("Importer-> AssetImportData is invalid for texture."));
		}
	}
}```

This was helpfull! Thank You!

Important to mention are the paths to the assets

My full path was E:\Unreal Projects\Memory\Content\Cards<all_jpegs_are_here>

And in the Package Path parameter in Get Assets by Path must be /Game/Cards

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.