Getting the width of a sprite
Asked Answered
M

4

13

I am trying to create a row out of some square sprites I have. So to get the width of these sprites i am using

tileWidth = (int)tileSet[0].renderer.bounds.size.x;

And then to form the row i am uisng

for(int i = 0; i < tileSet.Length ; i++){
    if((i+1)*tileWidth<screenWidth){
        tileSet[i].transform.position = new Vector3(i*tileWidth,0,0);
    }
}

But the sprites are still overlapping each other and do not form a proper row.
What am i doing wrong here and how can i rectify it?

Mensa answered 8/5, 2014 at 7:23 Comment(0)
M
15

If the sprite's res is 128x128 pixels.
And this sprite's Pixels To Units is 100.

So your tileWidth will be: renderer.bounds.size.x = 128/100 = 1.28
But you use int: (int)renderer.bounds.size.x = (int)1.28 = 1
and this is why your sprites overlapping each other.

float tileWidth = (float)tileSet[0].renderer.bounds.size.x;
Monologue answered 8/5, 2014 at 9:53 Comment(3)
If the sprite is rotated, this may return larger or smaller values than expected.Afterbirth
This is outdated.Lanneret
This is the best answer around sprite sizes.Rowena
A
19

If you are using Unity 5 you should use this code:

float tileWidth = tileSet[0].GetComponent<SpriteRenderer>().bounds.size.x;

Pay attention to your pixels per unit.

Afforest answered 21/8, 2015 at 13:2 Comment(1)
In my case I used: Game object obj = .................................................................................................................. private double getWidth (GameObject o) { ............................................................................ ...... return o.GetComponent<SpriteRenderer>().bounds.size.x; ........................................ } ................................................................................................................................................ Worked perfectly.Di
M
15

If the sprite's res is 128x128 pixels.
And this sprite's Pixels To Units is 100.

So your tileWidth will be: renderer.bounds.size.x = 128/100 = 1.28
But you use int: (int)renderer.bounds.size.x = (int)1.28 = 1
and this is why your sprites overlapping each other.

float tileWidth = (float)tileSet[0].renderer.bounds.size.x;
Monologue answered 8/5, 2014 at 9:53 Comment(3)
If the sprite is rotated, this may return larger or smaller values than expected.Afterbirth
This is outdated.Lanneret
This is the best answer around sprite sizes.Rowena
C
1
 SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();

        //size in Units
        Vector3 itemSize = spriteRenderer.bounds.size;

        float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;


        itemSize.y *= pixelsPerUnit;
        itemSize.x *= pixelsPerUnit;
Corriveau answered 12/9, 2018 at 16:54 Comment(0)
I
1

As jjxtra noted, Verv's answer is not handling rotation properly (and neither is MBehtemam's, as it's the same answer with a slight syntax update).

The following extension method correctly returns the pixel size of a given texture for different orthographic camera sizes, scales, rotations and textures.

        public static Vector2 GetPixelSize(this SpriteRenderer spriteRenderer, Camera camera = null)
        {
            if (spriteRenderer == null) return Vector2.zero;

            if (spriteRenderer.sprite == null) return Vector2.zero;

            float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;

            // Get top left corner
            float offsetRight = spriteRenderer.sprite.rect.size.x / 2f / pixelsPerUnit;
            float offsetUp = spriteRenderer.sprite.rect.size.y / 2f / pixelsPerUnit;

            Vector2 localRight = Vector2.right * offsetRight;
            Vector2 localUp = Vector2.up * offsetUp;

            // Go to world
            Vector2 worldRight = spriteRenderer.transform.TransformPoint(localRight);
            Vector2 worldUp = spriteRenderer.transform.TransformPoint(localUp);
            Vector2 worldCenter = spriteRenderer.transform.position;

            // Go to pixels
            Vector2 coordsRight = GetPixelCoordinates(worldRight, camera);
            Vector2 coordsUp = GetPixelCoordinates(worldUp, camera);
            Vector2 coordsCenter = GetPixelCoordinates(worldCenter, camera);

            // Get sizes
            float pixelsRight = Vector2.Distance(coordsCenter, coordsRight);
            float pixelsUp = Vector2.Distance(coordsCenter, coordsUp);

            Vector2 itemSize = Vector2.right * pixelsRight * 2 + Vector2.up * pixelsUp * 2;

            return itemSize;
        }

        public static Vector2 GetPixelCoordinates(this Transform transform, Camera camera = null)
        {
            if (transform == null) return Vector2.zero;

            return GetPixelCoordinates(transform.position, camera);
        }

        private static Vector2 GetPixelCoordinates(Vector3 position, Camera camera)
        {
            if (camera == null)
                camera = Camera.main;

            if (camera == null) return Vector2.zero;

            return camera.WorldToScreenPoint(position);
        }
Imaimage answered 23/4, 2020 at 15:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.