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?
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)).
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.
The default width is 100. All sprite properties can be both accessed and updated.
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);
}
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.
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;
SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
//size in Units
Vector3 itemSize = spriteRenderer.bounds.size;
float pixelsPerUnit = spriteRenderer.sprite.pixelsPerUnit;
itemSize.y *= pixelsPerUnit;
itemSize.x *= pixelsPerUnit;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With