Get Texture from name

Let’s say that I have 100 textures and I want to choose one.

What I have to do is to create an enum of 100 items, and then a function that takes that enum as an input and map each enum to a specified texture.

In c++ I could just save the texture name and that’s all.

is it possible to achieve something like that with blueprints?

Using this plugin helps

https://forums.unrealengine.com/attachment.php?attachmentid=23283&d=1422050820

I ended up creating a UBlueprintFunctionLibrary in C++ and use it from blueprint.

UTexture2D* UUtils::GetSpellIconByName(FString AssetName)
{
	//todo: save the library a single time instead of doing for every spell
	auto textureLibrary = UObjectLibrary::CreateLibrary(UTexture2D::StaticClass(), true, GIsEditor);

	FString textPath = "/Game/SpellCasterComponent/Icons/SpellButtonIcons";
	textureLibrary->LoadAssetDataFromPath(textPath);

	TArray<FAssetData> Assets;
	textureLibrary->GetAssetDataList(Assets);
	for (auto& asset : Assets) {
		if (asset.AssetName.ToString() == AssetName)
		{
			UTexture2D* found = Cast<UTexture2D>(asset.GetAsset());
			if (found)
			{
				return found;
			}
		}
	}

	return nullptr;
}
1 Like