We are working with a Texture Atlas and also need the Sprite position (Source UV) inside the Texture Atlas.
#include "PaperSprite.h"
#include "Runtime/Engine/Public/Slate/SlateTextureAtlasInterface.h"
FVector2D UExposedFunctionsBPLibrary::GetSpritePixelSizeInAtlas(UPaperSprite* Sprite)
{
if (Sprite) {
//GetSourceDimensions() is a function inside the FSlateAtlasData struct that gets returned by GetSlateAtlasData()
return Sprite->GetSlateAtlasData().GetSourceDimensions();
/* uncomment and replace for UV coordinates
return Sprite->GetSlateAtlasData().SizeUV;
*/
}
else {
return FVector2D(0, 0);
}
}
FVector2D UExposedFunctionsBPLibrary::GetSpriteUVPositionInAtlas(UPaperSprite* Sprite)
{
if (Sprite) {
return Sprite->GetSlateAtlasData().StartUV;
/* uncomment and replace for pixel data
//for some reason there only is the GetSourceDimensions() function for sprite size, but no for the position, so I copied it here with the StartUV data in place
FSlateAtlasData AtlasData = Sprite->GetSlateAtlasData();
return FVector2D(AtlasData.AtlasTexture->GetSurfaceWidth() * AtlasData.StartUV.X, AtlasData.AtlasTexture->GetSurfaceHeight() * AtlasData.StartUV.Y);
*/
}
else {
return FVector2D(0, 0);
}
}
don’t forget to add Paper2D to the Dependencies
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"Paper2D",
// ... add other public dependencies that you statically link with here ...
}
);
In UPaperSprite is the function FSlateAtlasData UPaperSprite::GetSlateAtlasData() const. It returns a struct that contains the UV data of the Sprite on the Atlas texture. The struct is defined in SlateTextureAtlasInterface.h and contains the function GetSourceDimensions() to get the pixel size, but no function for the position for some reason. I copied the functionality to uncomment in case someone needs it.
Luckily I found these functions by looking at the SetBrushFromAtlasInterface(), because it has a MatchSize option. These values feel very hidden to me. The Asset values are mostly defined as protected and can only be accessed by functions inside of the class. But I’m also still a c++ beginner