Search Unity

changing 2d sprite color at runtime, help needed

Discussion in 'General Graphics' started by enzoravo, Jul 24, 2017.

  1. enzoravo

    enzoravo

    Joined:
    Jun 25, 2013
    Posts:
    284
    Hello, i'm working on a 2d action side-scroller game, when the player finish the game, he will obtain player's suits, these suits will have some power to give to the game replay value, each suit must have it's own color.

    i need to change the original suit color (dark blue with some details in light blue), to 5 or 6 new colors, each color for one suit, i tried already with the setpixels, but i didn't obtain good results, the setpixels make a pause when it is copying and replacing the pixels colors and the colors that i have obtained didn't look so well.

    i want to find a way (shader, particle effect, or whatever) to avoid to make all the animations of the main player character in the 5 or 6 colors.

    i tried on google, but didn't found any way that will help me.

    if someone can point to me to a page, thread or article to help me to figure out this, will be very appreciate.

    thank you in advance for all the help.
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,022
    Hi enzoravo,

    the simplest way to do this would be to make two textures for suit, one with the parts that don't change, and another one containing parts that have to change color, in grayscale. The second one can then be easily colored.
     
  3. enzoravo

    enzoravo

    Joined:
    Jun 25, 2013
    Posts:
    284
    hi, thank you for the answer.

    what function i need to use to change the color of the grayscale areas ?

    i did that, when i used the setpixels, but the colors didn't look so well.
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,022
    You don't have to modify the pixels manually.
    You can just change the color in the material when rendering.

    If you still want to modify them manually, you can read the grayscale value from the texture, multiply the color you want to have by this value, and write the result back.
     
  5. enzoravo

    enzoravo

    Joined:
    Jun 25, 2013
    Posts:
    284
    ohhh, got it, you mean break the suit in two parts, one with the fixed color parts (the ones that will not change never) and the one that need change when the player change suits.

    just changing de color of the material assigned to the second parts (the grayscale parts) will do the trick, that is what you are trying to tell me ?
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,022
    yes, exactly
     
    enzoravo likes this.
  7. enzoravo

    enzoravo

    Joined:
    Jun 25, 2013
    Posts:
    284