Page 1 of 1

Binding to Unreal textures in C++

Posted: 04 Sep 2023, 13:17
by David Jefferies
Is there a C++ code snippet to show how to bind an Unreal texture uasset to a Xaml Image?

QuestLog has this Xaml:
<Image Grid.Row="1" Source="{Binding SelectedQuest.Image}" Margin="1,0"/>
In the project SelectedQuest.Image is defined and assigned in a Blueprint.

I'm interested in how to define and assign it in C++

Re: Binding to Unreal textures in C++

Posted: 05 Sep 2023, 12:25
by hcpizzi
Hi David,

This could be the equivalent native code for the QuestLog sample:
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Engine/Texture2D.h"
#include "Quest.generated.h"

UENUM(BlueprintType)
enum class EQuestDifficulty : uint8
{
	Easy,
	Normal,
	Hard
};

UCLASS(BlueprintType)
class SAMPLESMODULE_API UQuest : public UObject
{
	GENERATED_BODY()
	
public:
	UPROPERTY(BlueprintReadWrite)
	FString Title;

	UPROPERTY(BlueprintReadWrite)
	UTexture2D* Image;

	UPROPERTY(BlueprintReadWrite)
	EQuestDifficulty Difficulty;

	UPROPERTY(BlueprintReadWrite)
	FString Description;

	UPROPERTY(BlueprintReadWrite)
	bool Completed;

	UQuest() {}

	UFUNCTION(BlueprintCallable)
	void Initialize(const FString& InTitle, UTexture2D* InImage, EQuestDifficulty InDifficulty, const FString& InDescription, bool InCompleted)
	{
		Title = InTitle;
		Image = InImage;
		Difficulty = InDifficulty;
		Description = InDescription;
		Completed = InCompleted;
	}
};
All you have to do is create an UPROPERTY with type UTexture2D that is either BlueprintReadWrite or BlueprintReadOnly.

Hope this helps.

Re: Binding to Unreal textures in C++

Posted: 06 Sep 2023, 16:40
by David Jefferies
great - thanks :)