Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Spritesheet Creation

Discussion in '2D' started by TomTheLittle, Apr 16, 2020.

  1. TomTheLittle

    TomTheLittle

    Joined:
    Apr 23, 2018
    Posts:
    4
    Hello to anyone who can help!

    I'm trying to create a game graphically similar to Prison Architect and Rimworld. I want to do the same thing these games do and generate characters randomly from a set of head shapes, body shapes, skin tones, etc.

    I have seen on Prison Architect's spritesheet that the sprites for bodies and heads are separate, and in black and white. I can only assume they later have a script of some sort that sets the colour of the skin and uniform.

    What's the best way to go about beginning to design and layout my body parts? Should I draw them in black and white and assign colours later? Or would it be fine to just draw every possible combo for each body part?
    Following this, how can I go about assembling characters from these body parts later?

    Thanks!

    Tom
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    It's probably unwise to pre-draw every possible body-head-hands combo for each body part because for every additional variation you add, you need to create a ton more artwork. All of that would consume a lot of texture space. Instead, draw ea. body part separate from the others so you can combine together in the correct spatial orientation.

    Do you plan on using SpriteRenderer and GameObject for your characters or a lower-level API? If the former, you could arrange a layout for your body in SpriteRenderers positioned in static relative positions to the other body parts.

    Your hierarchy might look like this:
    Code (CSharp):
    1.  
    2. Body
    3.    Head
    4.        Hair
    5.     Hand1
    6.     Hand2    
    7.    
    Moving the body results in the same movement for the head/hair/hands. Moving the head results in similar movement for hair. There are other ways of conceiving of the same, and you'd need more if you want more body parts. Assigning the colors then becomes changing the SpriteRenderer's color. You'd need to create a system to swap sprites as the body reorients.
     
    Last edited: Apr 16, 2020
  3. TomTheLittle

    TomTheLittle

    Joined:
    Apr 23, 2018
    Posts:
    4
    Thanks for the response. The hierarchy example is probably what I'm wanting. I'm now imagining setting up a prefab whereby there are sprite renderers for each body part, and a script assigns a sprite to each part. That should work, so long as the sprites are all designed with the same relative positions, right? I.e. the neck is at the same point on every body sprite.
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Yes, and this should continue to work if you plan on animating by KeyFrames. So long as the hierarchy and names stay the same, it doesn't matter what sprite you swap in or out. Cheers.
     
  5. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    Specially if you are going to have a lot of characters on screen having many sprite renderers per character will get very messy as you go, there is another option which is you combine every part into a single sprite like merging layers in photoshop. Use texture2d.getpixels for one layer and then texture2d.setpixels on a new texture and keep layering until you have every layer in the same texture. Then texture2d.apply and sprite.create. You will need to do this for each frame of animation and then store the animations in an array and link it to your animator. The bonus is that when you use texture2d.setpixels you can choose new colors so whenever you want to create a new character you can have infinite combinations and only generate the ones you need. It also only takes 1 sprite renderer per character so it increases the number of characters you can have onscreen before having performance problems.
     
    Lo-renzo likes this.
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    This is an interesting alternative approach. When you do this, do you consume a lot more texture space f(characters * different animations)? Or are you doing something fancy to minimize the textures aside from only generating for those characters that exist? Or is that cost inconsequential for your requirements?
     
    brigas likes this.
  7. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    I personally have a class which contains the positions on each pixel of my sprites (x,y) and color, ( so I can avoid looping through transparent pixels) and then I use it to layer each frame and I then draw them on a bigger texture with padding to create a spritesheet. It will add up on texture size eventually but if there are sprites that are no longer needed in memory I overwrite them on the texture sheet and destroy the sprite. ( Im still figuring out if I can pool sprites but its iffy because its an unmanaged object )

    This is probably too complicated for this case though =P
     
    Lo-renzo likes this.