Search Unity

2d character customization choices etc.

Discussion in '2D' started by xeonheart, Apr 9, 2021.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello,

    So i watched this tutorial on changing outfits/skins:


    and he mentioned that, the way he did it in the video tutorial is not recommended, but did suggest to serialize, json or xml, wondering if anyone knows a good document/tutorial or video tutorial on how to do that, or code examples for best practice? i am doing a mobile game, so not sure if that makes any difference?

    thanks in advance.
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    what he is saying is that he doesnt recommend overwriting files ( throwing away the original )

    thats the only part he said he doesnt recommend. If you dont care about it you can still do it, otherwise you can use playerprefs to store the value of each body part and use it for save and load.

    Be careful with this tutorial though if you are going to have a lot of these characters on screen it will destroy your performance since each character has so many parts.
     
    MarekUnity likes this.
  3. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    gotya, thank you rarac for your info, makes sense, but since this will be on the mobile, i wanted to know if there is a better way for performance since I would definitely need to make sure i cant get as much since mobile devices is nothing near PC... yet ;)
     
  4. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    yes, there is a better way that is to combine all the images into one instead of having many parts, like merging all layers into 1 in photoshop.

    You use texture2d.getpixels loop to get all the pixels in a layer and then texture2d.setpixels to draw it on top of the previous, and you do this for all parts until you have only 1 image for each frame, so you will need only 1 sprite renderer.

    Ideally you will do this during a loading screen so your players dont get fps drop while the code is running, or you can do it for all combinations and save it to your assets folder ( option 1 wastes more time ingame but saves memory on the hard drive, and option 2 doesnt waste time but the game will occupy more space on disk )

    The bonus of this method is that you only use 1 sprite renderer so you can have many characters on screen at the same time and keep good fps, of course it also takes more work to code this method so you have to decide for yourself if its worth it.
     
  5. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hey Rarac, big thanks again for the input, that would make a lot of sense, sorry to say though, you mentioned to use texture2d.getpixels loop to get all the pixels in a layer and then texture2d.setpixels to draw it on top of the previous.

    No sure how to do this, is there a nice video or document tutorial on explaining steps on how to do that?
    I am currently using this:
    http://gaurav.munjal.us/Universal-L...rs=none&shoes=slippers_white&hair=messy2_blue

    as a test, to go skins until i can find free ones or purchase my own. but as you can see, i wanted to first use:
    1. body choice (male or female)
    2. color of body
    3. shirt
    4. pants
    5. shoes or no shoes

    and thats it, so 5 choices for the player... would your recommendation still work for this? maybe explain more in detail or again if there is a nice tutorial that you ran into that would show me in steps/details? big thanks again for your help
     
  6. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    for 5 parts dont bother about it

    if you start to think maybe 1more maybe another more etc, and suddenly you find yourself with 30 parts then you can start to think about it
     
  7. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    gotya, thank you again Rarac, so in short, are you saying or recommending to just do the the many many combos? or is it still a good route to do the texture2d.setpixels? sorry but have to ask again, is there any reference, documentation or even a youtube video that shows how to do it?
     
  8. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    heres an example to merge 2 layers

    Code (CSharp):
    1.  
    2. public Texture2D texA;
    3. public Texture2D texB;
    4.  
    5. public Sprite spr;
    6.  
    7. for ( int x = 0; x<texA.width;x++){
    8. for ( int y = 0; y<texA.height;y++){
    9.  
    10. if( texA.GetPixel(x,y).a != 0 ){
    11. texB.SetPixel(x,y,texA.GetPixel(x,y));
    12. }
    13. }
    14.  
    15. }
    16.  
    17. texB.Apply();
    18. spr = Sprite.Create(texB,new rect(0,0,texB.width,texB.height),new Vector2(0.5f, 0.5f), 1);
    19.  
     
  9. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Sorry and big thank you again Rarac, so if i have a sprite sheet, do i create a sprite on the unity UI, then with that object, will I then assign it on the UI as in the code you posted:

    Code (CSharp):
    1.  
    2. public Sprite spr;
    and do i do that for all the layers, have multiple sprites, or will they be Textures2d?
     
  10. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    once you understand the concept you can mix it up with sprite sheets

    but you should start without sprite sheets

    just 2 images, use the code to create a new sprite that is the combination of the 2 and after you understand the concept go from there