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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to store and prepare 2D sprites (3 directions) for later use

Discussion in 'Scripting' started by MacStanley, Sep 21, 2017.

  1. MacStanley

    MacStanley

    Joined:
    Jul 18, 2013
    Posts:
    118
    Hello,

    I have a couple of sprites for a 2d character, containing the following categories:

    - body
    - head
    - hair
    - shirt
    - jacket

    Each version of an "item" has one sprite for three different directions (up, side, down).

    What I am currently trying to do is creating a way to store these sprites with a name, so that I can just set the hair to "Curly" or "Long", so the sprites for all three directions need to be stored under this name. Unfortunately I don't have any idea how to start. Anyone has some tips or at least keywords that will help me get started?
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    By up down and side i assume you mean character movement? Or are you talking about a character creator type thing?
     
  3. MacStanley

    MacStanley

    Joined:
    Jul 18, 2013
    Posts:
    118
    I mean the orientation of the sprites. So I have for example hair shown from the side, shown from behind and shown from the front.
     
  4. MacStanley

    MacStanley

    Joined:
    Jul 18, 2013
    Posts:
    118
    Here is an example:

    Character should use this when walking upwards:
    upload_2017-9-21_23-10-29.png

    This when walking sideways:
    upload_2017-9-21_23-11-21.png

    And this when walking downwards:
    upload_2017-9-21_23-11-44.png

    I have a script that takes care of this. The only thing the script needs to know is "Okay the character has curly hair, so I have to use this sprite now because the character walks sideways.". And because I plan to have a ton of different hair styles it should be easy to maintain and expand at any time.
     
  5. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    Hmm, theres many ways you can go about swapping out the hair (im guessing its a seperate sprite on the main character?),


    anyways, if you already have the button promps like if up hairstyle = up
    then you can do something like this to control and add new hairstyles when you please

    Code (CSharp):
    1. public class CharacterFeatureControl : MonoBehaviour
    2. {
    3.  
    4.     [System.Serializable]
    5.     public class HairStyles
    6.     {
    7.         public string styleName;
    8.         public Sprite top;
    9.         public Sprite down;
    10.         public Sprite side;
    11.     }
    12.  
    13.     public HairStyles[] hairStyles; // create an array of hairstyles
    14.  
    15.     public int currentHairStyle = 0
    16.         ; // change this for the hair style you want from the hairStyle array e.g 0 = curly 1 = afro
    17.     // Use this for initialization
    18.  
    19.     void Start()
    20.     {
    21.         // assign the new sprites in here like
    22.         topHairSprite = hairStyles[currentHairStyle].top;
    23.         downHairStyle = hairStyles[currentHairStyle].down;
    24.         sideHairStyle = hairStyles[currentHairStyle].side;
    25.  
    26.     }
    27. }
    https://gyazo.com/a5cd8d96d0ace0760f33e766b3e2759b - this is what it will look like in the inspector, if you get my idea you can just increase the hairstyle up or down by 1 to change it
     
    MacStanley likes this.
  6. MacStanley

    MacStanley

    Joined:
    Jul 18, 2013
    Posts:
    118
    That should work just fine :) I am currently implementing it since a couple of things need to be adjusted in my scripts. Thank you very much for the idea!