How do I access the size of a sprite?

I am currently using Unreal 5.2 and noticed this information is outdated.

First of all, the function mentioned above (i.e.)

FVector2D UPaperSprite::GetSourceSize() const;

will not work in a Production build, as the data it references is WITH_EDITORONLY_DATA and the function itself is WITH_EDITOR.

So, how can you find the dimensions of a Sprite? Well, you could use:

FBoxSphereBounds UPaperSprite::GetRenderBounds();

// This holds the origin of the bounds (or the pivot point in Unreal units)
FVector FBoxSphereBounds::Origin;

// This holds the extents of the bounds (i.e. the half-dimension) in Unreal units
FVector FBoxSphereBounds::BoxExtent;

From this you can find the sprite dims in Unreal units. This will also work even if you are running from an atlas. If you need to know the actual texture units for some reason, then you should also use:

float UPaperSprite::GetPixelsPerUnrealUnit() const;

Here is the usage:

UPaperSprite *Sprite = <some sprite>
FBoxSphereBounds Bounds = Sprite->GetRenderBounds();

FVector WorldSpriteDims = ( Bounds.BoxExtent * 2.0 );

// WorldSpriteDims.X is the horizontal size for the sprite
// WorldSpriteDims.Z is the vertical size for the sprite

FVector WorldSpritePivot = Bounds.Origin;

// WorldSpritePivot.X and WorldSpritePivot.Z are the pivot points of the sprite in Unreal units.

// If you need the actual texture dims, do this:
FVector TexDims = ( Bounds.BoxExtent * 2.0 ) * ( double ) Sprite->GetPixelsPerUnrealUnit();

// TexDims.X will hold the texture X size and
// TexDims.Z will hold the texture Y size.

I hope this helps.

4 Likes