Right now I just want to find the size of a sprite component in a Blueprint class because I want to use it to restrict the panning of the camera, but after searching the menu I can’t get any further than Get Sprite, which I would assume to be a step in the right direction but doesn’t seem to lead to anything obviously size-related.
I’ve tried looking for size… dimensions… width… length… but I can’t find anything that looks remotely related. You’d think something like this would be quite apparent, but maybe I’m approaching it the wrong way…
Where can you access a sprite’s dimensions? I can even see it in the tooltip for the sprite in the content browser under Source Dimension…
Anyone? Is there a different way of accessing it perhaps? I know I could hardcode the dimensions of course… but I’d rather not leave 2 locations with the same sprite dimensions completely unconnected.
Yeah, that or the size of the sprite object it’s applied to. Either would work in my case I think.
The situation being that I have a sprite that’s bigger than the screen that I want the camera to scroll around without going past the edges. Another solution might involve declaring some variables somewhere and using those as the bounds of the camera but then I’d probably want to use those variables to scale the sprite in some way anyway, speaking of which: can you use variables to set values in the Details pane? e.g. For the position of a camera. Or does that have be set through an Event Graph?
I remember seeing something in the tutorials but it might have just been for UMG…
I’m not sure if it is possible to do that as I don’t use paper2d, but if it isn’t, it’s still possible to expose these functions to blueprints yourself, but it requires using C++ to make the custom node.
For example, here I’ve exposed UPaperSprite::GetSourceSize to blueprints, it’s pretty easy to add this to your project:
This is so you can include Paper2D header files like PaperSprite.h, so you can make a blueprint node that takes one as an argument.
The header file for the node:
SpriteFunctions.h
#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "PaperSprite.h"
#include "SpriteFunctions.generated.h"
/**
*
*/
UCLASS()
class PROJECTNAME_API USpriteFunctions : public UBlueprintFunctionLibrary //Your project name in uppercase followed by "_API"
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, meta = (DisplayName = "Get Source Size", Keywords = "Source Texture Sprite"), Category = Custom) //Here you can change the keywords, name and category
static FVector2D GetSourceSize(UPaperSprite* sprite);
};
SpriteFunctions.cpp
#include "ProjectName.h" //Replace with your project name
#include "PaperSprite.h"
#include "SpriteFunctions.h"
FVector2D USpriteFunctions::GetSourceSize(UPaperSprite* sprite)
{
return sprite->GetSourceSize();
}
As you can see it’s pretty simple to set up, and you can easily add more functions to this function library so you can access them in blueprints.
Thanks for that, should be helpful. Annoying that they seem to have left such a basic function out or in some unintuitive place at least; maybe they had some other type of solution in mind… I wonder if messing around with Paper 2D Flipbooks might help since you’d expect sprite size to be important in the case of fully fledged animations but who knows.
If nothing else, this’ll give me a reason to start mixing C++ and Blueprints, like I’d originally intended before Blueprints looked so inviting…
I’m trying to implement this now but it’s still not showing up in the editor for some reason…
I changed the line in Build.cs, then I selected New C++ Class… created a class called SpriteFunctions, pasted the code in, changed the include and _API to the name of my project, saved…
Then I went into the editor and selected Refresh Visual Studio Project, which I assume is what I’m meant to do after every time I change any C++? Seems to make sense.
After that, Visual Studio gives a message saying that the solution has been modified outside the environment, though it seems to have given me at least 2 different messages in this situation… one with the option to “Reload” the updated solution from disk (which sounds like what I want) and another with a “Save As…” option that sounds similar. I assume I’m not supposed to build in Visual Studio, and when I try it it predictably doesn’t compile right.
So I’m not sure what’s wrong at this point… I can’t find the function by right-clicking, turning Context Sensitive off and typing the name. I’ve checked multiple times and everything seems fine. I did miss the _API bit before but changing that didn’t fix it.
You should rebuild the solution in Visual Studio, in the solution explorer under Games, right click your project and click Rebuild. Try closing the editor first and then building it
It’s there! Excellent. That should help a lot with some of the things they chose not to expose to Blueprints…
Thanks.
Edit: Actually, one more thing… how do you find what you need to add to the PublicDependencyModuleNames.AddRange list?
For instance, I want to expose a C++ function of the DateTime class; should adding “DateTime” work or might it be a part of a larger module…?
Edit2: I see according to FDateTime | Unreal Engine 5.2 Documentation the module is just Core. Only problem is that it says GetDayOfWeek() returns CORE_APIEDayOfWeek, but if I just put that into the C++ it doesn’t recognize the type when I build:
I also tried EDayOfWeek (I assume E refers to it being an enum) but that didn’t work either.
I’ve included DateTime.h and it appears to recognize FDateTime because it doesn’t give an error if I declare a variable of that type before the day of week function, so I assume that’s working ok.
Doesn’t seem that CORE_API is meant to be used like a namespace either, so I can’t figure it out…
Edit3: Visual Studio definitely recognizes EDayOfWeek and that’s definitely what GetDayOfWeek returns because the intellisense says as much. Just won’t let me return that type from a function? Must be meant to be wrapped in a UENUM or something…?
Anyone have any ideas as to why there might be a problem returning an EDayOfWeek? You’d think being an enum it would derive from UENUM in some way and be fine, but apparently it doesn’t work that way. The C++ forum might be better for this, but it’s kind of relevant to both so I dunno.
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:
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.
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