Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Jackal16 · Sep 06, 2015 at 12:38 PM · spritewidth

Getting a sprites size in pixels

Hi guys, I'm trying to get the width / height of a sprite in pixels by doing this

_cam.WorldToScreenPoint( GetComponent().bounds.size );

this gives me a daft value of around 400 pixels high when I can see that it is no where near that size.

Am I doing something wrong here, I've tried a few other variations as well?

Cheers

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wibble82 · Sep 06, 2015 at 12:45 PM 0
Share

Hey

GetComponent() is a bit weird - that shouldn't compile. Are you getting the bounds of a box collider 2d or something?

avatar image Jackal16 · Sep 06, 2015 at 12:50 PM 0
Share

Yeah sorry meant type Renderer in angle brackets - its removes them on here

avatar image wibble82 · Sep 06, 2015 at 12:52 PM 0
Share

It's a bit of a tricky problem, as it depends on your camera too. The world to screen point is definitely not what you want, as that'll treat the size as though it were a world position, which it isn't.

Are you using an orthographic or perspective camera?

avatar image Jackal16 · Sep 06, 2015 at 12:53 PM 0
Share

orthographic - doesn't bounds.size / bounds.extent return a value in world co-ords?

avatar image sharat · Sep 06, 2015 at 12:59 PM 0
Share

Have you tried using sprite.rect.height and sprite.rect.width? Alternatively, maybe sprite.bounds.height/width with sprite.pixelsPerUnit

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by wibble82 · Sep 06, 2015 at 01:10 PM

Orthograph cameras aren't too tricky as they don't scale with depth. You'll need to:

  • Take the sprite bounds which are in world space (I was incorrect in my comment above)

  • Use the camera orthographic size to get the size in screen space (-2 to 2 in x and y)

  • Then scale that by the camera pixel width/height to get size in pixels

EDIT: I've added the 2nd approach that handles rotation.

Something like this:

         //get world space size (this version operates on the bounds of the object, so expands when rotating)
         //Vector3 world_size = GetComponent<SpriteRenderer>().bounds.size;
 
         //get world space size (this version handles rotating correctly)
         Vector2 sprite_size = GetComponent<SpriteRenderer>().sprite.rect.size;
         Vector2 local_sprite_size = sprite_size / GetComponent<SpriteRenderer>().sprite.pixelsPerUnit;
         Vector3 world_size = local_sprite_size;
         world_size.x *= transform.lossyScale.x;
         world_size.y *= transform.lossyScale.y;
 
         //convert to screen space size
         Vector3 screen_size = 0.5f * world_size / Camera.main.orthographicSize;
         screen_size.y *= Camera.main.aspect;
 
         //size in pixels
         Vector3 in_pixels = new Vector3(screen_size.x * Camera.main.pixelWidth, screen_size.y * Camera.main.pixelHeight, 0) * 0.5f;
 
         Debug.Log(string.Format("World size: {0}, Screen size: {1}, Pixel size: {2}",world_size,screen_size,in_pixels));
 





Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jackal16 · Sep 06, 2015 at 01:18 PM 0
Share

No rotation, I gave it a try and it gave me something like (25, 45) for in_pixels. Given that my sprite is square that can't be right can it?

Bit more info - Screen size is 480 wide by 800 high Sprite is currently about 48 by 48 at a guess

avatar image wibble82 · Sep 06, 2015 at 01:30 PM 0
Share

Apologies - I missed a bit. Updated answer above.

avatar image wibble82 · Sep 06, 2015 at 01:34 PM 0
Share

Ok - I'm really not having a good day - give me 5 $$anonymous$$utes :)

avatar image wibble82 · Sep 06, 2015 at 01:36 PM 0
Share

Ok - that should do it

avatar image wibble82 · Sep 06, 2015 at 01:44 PM 0
Share

Added a rotating one for ya too :)

Show more comments
avatar image
4

Answer by Bshow · Jul 15, 2016 at 05:37 AM

You can use the Sprite.Rect parameter, that stores height and width in pixel

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AntonQvarfordt · Feb 04, 2020 at 01:00 PM 0
Share

That only gives the set sprite asset pixel size, which generally defeats the purpose of measuring the size during runtime.

i.e. if you scale the sprite in any way it'll still read you the defined asset pixel size, not the current size after scaling.

avatar image
0

Answer by 25Hour · Nov 15, 2021 at 09:10 PM

I suggest something like:

var originalSpriteSize = image.sprite.rect;

     image.sprite = protoSprite.ToSprite();
     image.color = protoSprite.ToGameSpriteImage().Color;

     var newSpriteSize = image.sprite.rect;

     var scaleFactorWidth = originalSpriteSize.width / newSpriteSize.width;
     var scaleFactorHeight = originalSpriteSize.height / newSpriteSize.height;

     image.transform.localScale = new Vector3(scaleFactorWidth * image.transform.localScale.x, scaleFactorHeight * image.transform.localScale.y, image.transform.localScale.z);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite slicing problem 1 Answer

In Sprite Editor, how can I get theese values by code? (screenshot) 1 Answer

Change GUITexture width 1 Answer

How to make a health bar in Unity 5? 0 Answers

Getting the width of a sprite 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges