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.

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.

Just change the “pixels per unit” per sprite. To select multiple sprites use the command button (on a mac).

  1. Create a canvas
  2. Create a panel in canvas
  3. Create a image in panel
  4. drag first image from your sprite sheet to source image form image
  5. Click on ‘Window=>Animation=>Animation (or Ctrl+6)’
  6. Click on your image from panel
  7. Click on Create button from Animation window (now you need to save your clip in a folder)
  8. After creating clip (click on red circle button) and you can drag your sprite sheet on Animation window
  9. Click on red circle button, ready

var targetHeight = new Vector3(1f, 1f, 1f); // este tamaño corresponde al que necesites en relación al la primera imagen
var oldSize = icon.sprite.bounds.size.y;
icon.sprite = nodeData.icon;
var newSize = icon.sprite.bounds.size.y;
icon.transform.localScale = (oldSize/newSize) * targetHeight;

This code worked for me, you just have to make sure that the initial target is the scale corresponding to that of the initial image.