Search Unity

Question How can I resize sprites in pixels

Discussion in '2D' started by tomlugin100, May 20, 2023.

  1. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    84
    Hello and thank you for reading my post!
    I'm trying to resize a sprite I have in my scene. As far as I understand it, transform.localScale is not pixels.
    I want to resize the sprite's height to match another sprite. Is there any way to do this?
    Is there a way I can set new bounds on the SpriteRenderer? When I attach new bounds nothing changes.
    Humbly,
    Tom L
     
    Last edited: May 20, 2023
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    use photshop for that
     
  3. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    84
    Thank you for your answer. I need to dynamically change the height to multiple other sprites which have different heights. Using Photoshop would only work for some of them. Is there any way to match the heights programmatically?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    If you want to redo the pixels of a sprite to make the overall sprite a different size, your two choices are:

    - Edit time: redo the sprite (as in what Kard suggested above with Photoshop)

    - Run time: rescale the sprite with localScale, set a RenderTexture of the appropriate resolution with a Camera aligned to photograph the newly-sized sprite, and then finally re-create the sprite at the new size based on the contents of the RenderTexture

    Otherwise just change the .localScale and you can keep the same original pixels and move forward.
     
  5. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    84
    Thank you @Kurt-Dekker for your informative answer. I had just figured out how to use localScale. I'm not sure I understands why it works the way it does, but it works. I set localScale.y = newHeight / oldHeight and it worked like a charm