David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

Binding to Unreal textures in C++

04 Sep 2023, 13:17

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++
 
User avatar
hcpizzi
Site Admin
Posts: 368
Joined: 09 Feb 2012, 12:40

Re: Binding to Unreal textures in C++

05 Sep 2023, 12:25

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.
 
David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

Re: Binding to Unreal textures in C++

06 Sep 2023, 16:40

great - thanks :)

Who is online

Users browsing this forum: Bing [Bot] and 5 guests