- Home /
Keep sprites at fixed size
Hello.
I have GameObjects that will hold sprites. They will change their sprites at certain event. However, pixel sizes of sprites are different.
What I must write in script to keep all changed sprites in one size? (In pixels size is 263x163)
PS this game is on 2D mode.
Answer by robertbu · Apr 25, 2014 at 03:11 PM
If you want the sprites to be the same world size even if their pixel size is different, then you need to select the texture used for that sprite and set the 'Pixels to Units' value as appropriate to scale the sprite to the target size.
O$$anonymous$$, that works pretty fine. However, I need to set "Pixels to Units" for every image manually? Is there some kind of method to automatically do it in script using formula? Because my application got 1200 images.
You can probably do it with an editor script. But considering the number of images, maybe you just want to scale the images at runtime. Assu$$anonymous$$g uniform scaling and a 'targetHeight' in world units, you can do:
var bounds = GetComponent(SpriteRenderer).sprite.bounds;
var factor = targetHeight / bounds.size.y;
transform.localScale = Vector3(factor, factor, factor);
I don't know your game mechanic, but two other things to consider. If you can use a Quad for your image ins$$anonymous$$d of a sprite, your problem goes away. And alternately (and not without some pitfalls), you would use a batch script in Photoshop to bring your images that need to be the same size to the same resolution.
Answer by richard_Richintellect · Jan 20, 2017 at 04:16 PM
Just change the "pixels per unit" per sprite. To select multiple sprites use the command button (on a mac).
Answer by unityPashok · Sep 06, 2020 at 02:13 PM
Create a canvas
Create a panel in canvas
Create a image in panel
drag first image from your sprite sheet to source image form image
Click on 'Window=>Animation=>Animation (or Ctrl+6)'
Click on your image from panel
Click on Create button from Animation window (now you need to save your clip in a folder)
After creating clip (click on red circle button) and you can drag your sprite sheet on Animation window
Click on red circle button, ready