Question

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?

Was it helpful?

Solution 2

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;

OTHER TIPS

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.

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

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

        float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;


        itemSize.y *= pixelsPerUnit;
        itemSize.x *= pixelsPerUnit;

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);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top