Search Unity

Question Weird UVs if using "sprite mode: multiple" for textures from spritesheet?

Discussion in '2D' started by Aurefur, Jun 1, 2023.

  1. Aurefur

    Aurefur

    Joined:
    Apr 19, 2020
    Posts:
    6
    Hello Community,

    I hope this belongs in this subsection of the Unity Forum.

    While I was working on my game I noticed that my healthbars weren't behaving the way they were supposed to. Because the shader was working well with a standard square texture for the "fill" of the bar, I found that the issue was with the texture I was using.
    The texture was the second sprite from a spritesheet containing all my UI-textures (only two for now).
    This however led to weird UVs, as the texture didn't go from 0 to 1 in the x-direction, but instead from 0.5 to 1. Due to this if the "fill amount" of the bar is set to 0.5, the bar appears empty, even though it is supposed to be half full.
    If I replace the bar texture with the first sprite of the spritesheet, the UVs now seem to go from 0 to 0.5 in the x direction.
    Does this mean that the sprite editor spreads the UV across all the sprites of the spritesheet?
    If that is the case, is there any way to fix it, or do I just have to take the old way and import all my sprites seperated?

    Thanks for reading this.
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    > The texture was the second sprite from a spritesheet containing all my UI-textures (only two for now).

    Seems like your misunderstanding starts here. You don't have multiple textures in a spritesheet. The whole spritesheet is single a texture. That's the whole point of spritesheet - combining multiple images in a single texture.

    UV coordinates are positions within texture always in the range of 0-1. So if you have multiple sprites within single texture either due to manually creating such image file or by using Unity sprite sheet functionality to combine multiple images, then in your shaders you can't assume that 0-1 represents whole image.

    I can think of a half a dozen different ways that could be used to avoid the problem depending on exact requirements but all of them are more complex than simply having separate texture for your bar and not worth the effort unless you have very specific needs. If only one of your UI images will be using the special fill shader, then there is probably little value combining it in a spritesheet together with other stuff that will be using different shaders.

    Although before either of the two ways you should consider do you really need a custom shader for implementing bar filling effect yourself. If you need simply changing the fill amount of horizontal bar, then you can probably use the builtin functionality like UGUI Image fill for canvas stuff, or sprite mask if it's for SpriteRenderer. Even some of fancier filling effects can be achieved using masking functionality.
     
    Aurefur likes this.
  3. Aurefur

    Aurefur

    Joined:
    Apr 19, 2020
    Posts:
    6
    Thanks for your reply!

    I think i will stick to my shader though since I intend to add (and already have) other features to it in the future.

    I was always using spritesheets as a method of organizing my sprites and never had any problems, because I never actually used them in combination with a custom shader. I think I should let that bad habit go and move on to other orga-methods like good old folders.

    Again, thanks for taking you time to reply, I really appreciate it.