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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Best way to do 2D sprites with interchangeable parts?

Discussion in '2D' started by Creslin7, May 2, 2020.

  1. Creslin7

    Creslin7

    Joined:
    Dec 3, 2019
    Posts:
    5
    Hey Everyone,

    I’m working on a 2D game with an orthographic side scroller perspective. I would like to do characters with interchangeable body parts so that you could procedurally generate new characters or even races by just mixing and matching different parts or changing their scale.

    For instance, you could put pointy ears in the ear “slot” and make the body type more narrow to turn a human into an elf. I also want to try to make all these use the same animations. So that I could just do all the animations for a humanoid body type one time and then use them with any combination of parts.

    does anyone know if something like this is possible, and\or the best way of going about it?

    thanks!
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    There is a some of tricks I'm aware of, but I don't know a complete and elegant solution.
    I'll tell you the tricks though.

    The first obvious one is to break your character down into it's primitives and just switch/recolor these as needed.
    For instance, lets say you want to have a simple character with their head, hands showing, and you want skin color. You could break out the head and hands as primitives, and alter the color property of the sprite for these to get your skin color. You can also get some size control this way to if you wanted say a small medium and large from that are just slightly different scale sprites for the chest.
    The issue here is that you need reasonable 'anchors' for your pieces, and animations can be quite resource intensive as every primitive will need to be independently animated then appropriately overlapped for each frame of animation.

    The second possibility is to get into shaders. Now, this won't help you much for changing geometry, but for character wide color alterations it can be quite useful. With this method, you ensure your actual character sprite use very specific colors for various areas of the sprite (like color X is the skin, color Y is the eyes, color Z is the their shirt, etc). This version of the model is never seen by the user so you can use hot pink and such for your colors. Then you make your shader just do a color swap, hunting down each color and swapping it out which the one for your actual user seen sprite. You actually can do this without the shader, but it is much more of a performance hog. Shaders aren't that hard, thanks to shader graph, so don't let that intimidate you.
    As already said, this won't help you with geometry or patterns on your sprite, so if you want to go from a shirt to a suit of armor, this is the wrong approach, at least by itself. That does bring up though that this is a pretty good resource saver in combo with the first approach as now anything with the same geometry, but just different coloring, you can handle in the shader instead of needing a custom asset for each one. Do keep in mind though you do have some color control in the first approach already, so this is more for complex color swaps. So most the savings here is in keeping the total number of primitive pieces down.

    The third thing you can leverage potentially is to build the sprites in code from the primitives, then reference these created sprites during the game. Now I know this is possible, but I've never done it. What's the advantage? Honestly not much. Unless you have an insanely complex character model, all you're really saving here is processing of the various primitive bits as you've merged them into the sprite itself. Unless your character is super complex, that processing savings is pretty trivial. Seems like a lot of extra work for very little impact to me, but something to keep in mind.
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    The highest level API you could use is KeyFrame transform animations with SpriteRenderers. You swap out the sprite in the SpriteRenderer for swapping different parts and can share KeyFrames for different ears, armors, etc. However, if you have many characters with many "slots" then this will mean quite a lot of SpriteRenderers and independent transforms being animated. So if you have tons of slots and many characters on screen at once it may not be the best choice, but it on the other hand it is easy and uses systems built into Unity.
     
  4. Creslin7

    Creslin7

    Joined:
    Dec 3, 2019
    Posts:
    5
    Thanks so much for the advice guys :)!!! Super helpful, I’m going to look into each approach more and see what is the best for me.