Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to Stretch/Scale a Sprite to fill a square PERFECTLY?

Discussion in '2D' started by redapplesonly, May 3, 2023.

  1. redapplesonly

    redapplesonly

    Joined:
    Nov 8, 2021
    Posts:
    51
    Hi everyone, I'm working on a 2D Strategy Game that will dynamically build a game board of squares with X rows, Y columns. I found this great online tutorial here (YouTube link) and built my game off that. Highly recommended.

    The tutorial creates a chessboard-like gameboard using Create: 2D Objects --> Sprites --> Square, not using Tilemaps. I like this method as a way to build out my gameboard because its very easy to understand for a Unity newbie like me. Here's the gameboard, using the Square object:

    So far, so good. But because my game takes place in outer space, I'd like to insert a custom Sprite into (or within? or on top of?) each square. That Sprite might be a planet or a rocketship or a UFO or a comet or empty space or whatever. And I'd like that Sprite to be stretched and scaled so that it completely fills its respective square. For proof-of-concept purposes, I whipped up a simple GIF sprite:

    Then I put that Sprite into the Square Object's Sprite Renderer (red arrow):

    The sprite now appears, but is underscaled within the square object:

    That's no good. The Sprite is smaller than the Square, which gives me those awful gaps between the squares. I've tried manually rescaling the Sprite, but its either too big or too small. I need it to be automatically stretched/scaled so that it matches the Square's dimensions perfectly. Does anyone know how to do this?
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    the first step is to find out the size of the square
     
    redapplesonly likes this.
  3. redapplesonly

    redapplesonly

    Joined:
    Nov 8, 2021
    Posts:
    51
    Yes, point well taken. The size of the square when it is created is 256 x 256 pixels. Are you suggesting that all I must do is set the "inner" Sprite to those dimensions? Thank you
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    yes, if the square is 256x256 all you have to do is to make a 256x256 sprite

    take note of the other import settings of the square in unity, like pixels per unit. You should match all the settings the square sprite currently has

    you might not need to make a 256x256 sprite tho(its unecessarily large), as you could simply make a 16x16 sprite and then adjust the scale. The best course of action would be just to make a square sprite and line it up manually to see which values work best for you.
     
    redapplesonly likes this.
  5. redapplesonly

    redapplesonly

    Joined:
    Nov 8, 2021
    Posts:
    51
    Thank you for your insight. I guess I was hoping that there was a way to do that automatically, a "Scale Sprite by Pixel" feature or a "Snap Inner Sprite to fit Dimensions X-Y" kind of thing. I'm finding the manual adjust thing to be difficult. But thank you.
     
  6. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    well you can do it "automatically" but you have to code it out, you need to factor in the size of the you want to scale to and your current scale

    for example given the same import settings you need to apply a scale of 1.066666666666667 to your sprite
     
    redapplesonly likes this.