How to find the size/dimensions of a sprite?

This must be really basic because when I Google for it or search the Unity3D forums, nothing comes up.

How do you find the size/dimensions of a sprite in Unity?

Thanks muchly.

You put a texture of a certain number of pixels in and set it for Sprite import.

Part of that is the property “Pixels Per Unit” which maps those pixels to Unity world units.

Beyond that there are properties such as .rect and .textureRect in the Sprite object itself.

Thank you. I’ll try it out.

I have a sprite renderer property attached to my canal object but when I write “this.size” (see: Unity - Scripting API: SpriteRenderer) it says it doesn’t have that attribute. The script is also attached to the canal object.

this is the instance of the class you’re in.

Unless you ARE the SpriteRenderer, this has nothing to do with SpriteRenderers.

Are you using this in UI? I’m not sure .size is generally valid. Docs claim it’s only good for nine-sliced sprites.

“Property to set/get the size to render when the SpriteRenderer.drawMode is set to SpriteDrawMode.Sliced.” You’re right. I don’t know how to access a .rect properties of my canal sprite if I don’t have a .rect transform but a normal transform, and it gives me an error saying you can’t replace transforms for some reason.

Are there any good tutorials on 2d programming and sprites you recommend, actually? I just need to learn a lot of basics,

Traditionally one does not rely on art to get critical game logic such as size.

The thinking is you don’t want to update your art and make it one pixel larger and suddenly your player can’t get through doorways, or whatever.

For physics you put an appropriate-sized collider on.

For other uses like a tilemap the size come from that grid object or tilemap object.

For other uses you might put a radius or a bounding box value in your script, or else in some kind of dimension indicator script attached to the sprite.

I’m not changing the size of the object after the game starts. I just basically even want the bare minimum: stats on my game objects while programming. That seems like a really reasonable thing to. me. I’d like to resize the object and know how many pixels it’s taking up, things like that. And OCD, but, I want the size of the game object to be a round number, not something arbitrary.

Sorry if I sound angry, I just find it strange that this seemingly simple thing would be so hard.

I hear you about not relying on polish to fix the engine, or screwing over the design for the player.

kurt is giving you the loop around to show you that checking size of your sprite doesnt make sense

the reason is that unity doesnt measure size by pixels, to check the size in pixels use your image editor not unity

unity uses world units which are arbitrary and not pixels

do you know how to measure how much world units a sprite takes up? Is raycast also an option? Because in my game I want the selection arrow to float a certain distance above a characters head but character have different hieghts

height of your sprite divided by pixels per unit

Use this if you want to get the actual size of the sprite image.

Vector2 spriteOriginSize = spriteImage.rect.size;