Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the width of a sprite

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?

like image 965
Vaibhav Avatar asked May 08 '14 07:05

Vaibhav


People also ask

How do you determine the size of a sprite?

If image has width of 100px, and you want to scale it to 150px, then you need to divide 150 by 100 and it will give you scale you need to set sprite. scale. x = 150.0 / 100.0 (note that you need at least one float in division to get float result (int/int=int, i.e. 150/100 = 1)).

How do you find the width of a sprite in Scratch?

Scratch has no way of assigning a sprite's width and height unless the user does so. If you go to the sprite editor the sprite width and height are displayed. If they are all the same, much like using tiles, then you can certainly create variables for width and height and set them to their values and position them.

What is sprite width?

The default width is 100. All sprite properties can be both accessed and updated.


4 Answers

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);
        }
like image 150
Konstantinos Vasileiadis Avatar answered Oct 27 '22 10:10

Konstantinos Vasileiadis


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.

like image 45
MBehtemam Avatar answered Oct 27 '22 10:10

MBehtemam


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;
like image 35
Verv Avatar answered Oct 27 '22 10:10

Verv


 SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();

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

        float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;


        itemSize.y *= pixelsPerUnit;
        itemSize.x *= pixelsPerUnit;
like image 38
user999913 Avatar answered Oct 27 '22 10:10

user999913