Search Unity

Change an Sprite Instance Color Palette.

Discussion in '2D' started by yukamui, Nov 19, 2013.

  1. yukamui

    yukamui

    Joined:
    Nov 19, 2013
    Posts:
    11
    Hello, I am a complete newbie to Unity (and C#) and I am trying to change an Instance's Sprite Color palette. I was able to change the color palette of an Sprite (I have attached an example) but since it changes the color of the sprite iself, all instances that use the same sprite also change colors(You even have to re-import the asset if you want to recover the original colors!).

    Could somebody please direct me in the right direction? Thank you very much.
     

    Attached Files:

    Last edited: Nov 19, 2013
    blox5000 likes this.
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    345
    Make sure you're targeting the sprite renderer of the actual sprite you want to modify.
    eg: If you throw the following code into your Start function it will change the sprites color to red.

    Code (csharp):
    1.  
    2. void Start () {
    3.         GetComponent<SpriteRenderer>().color = Color.red;
    4. }
    5.  
     
    blox5000 likes this.
  3. yukamui

    yukamui

    Joined:
    Nov 19, 2013
    Posts:
    11
    Thank you for the tip UnityLover, it is handy. But I am not changing the color property of the SpriteRenderer I am changing the pixels colors of the sprites with getPixels32 and setPixels32, and since all instances are supposed to use the same sprite, all of them change to the same color.

    If you create two instances of the character prefab, and you change the array of new colors in one of them you will see what I mean.

    I would like some help to dynamically create and set the recolored sprites... or if anybody has a better idea to achive the same result of the sample I would be very happy to hear it.

    Thanks a lot.
     
    Last edited: Nov 19, 2013
    blox5000 likes this.
  4. maltadirk

    maltadirk

    Joined:
    Mar 12, 2014
    Posts:
    43
    Hey
    Wanted to check if u had any progress with the spritrrenderer with regards changing color. Im trying to have a sequence where the whole scene in black and white, where thr player passes the spritrs change from black and white to their original color. Would be cool to have this work per-pixel for a nice transition effect
     
    blox5000 likes this.
  5. yukamui

    yukamui

    Joined:
    Nov 19, 2013
    Posts:
    11
    Hey Maltadirk!

    I think I have exactly what you need!. I took a total different approach. I found a shader where you can change the HUE, SATURATION and VALUE of any sprite Renderer.

    For you, tweening the Saturation from 0 to 1 will give the desired effect. Just create a Material that uses the shader I am attaching. And then set the Material to the sprite you want to recolor/decolor and you are good to go. (For recoloring what I do is shift the HUE instead of the saturation, and the recoloring looks great).

    The shader was not made by me, it was given away in a forum by a fellow game dev.

    If you have any questions on how to use it, ask me and I show you.
     

    Attached Files:

    jhocking, blox5000 and maltadirk like this.
  6. BlaXun

    BlaXun

    Joined:
    Jan 8, 2015
    Posts:
    52
    I will also give this a try later today. Sounds rly useful :)
    Thanks!
     
  7. maltadirk

    maltadirk

    Joined:
    Mar 12, 2014
    Posts:
    43
    Thanks so much for the share and info yukamui :) I will test it out shortly and let you know how it goes
     
  8. KaaPex

    KaaPex

    Joined:
    Jan 17, 2015
    Posts:
    16
    Hi!

    Try something like this.
     

    Attached Files:

    miguel-janer and maltadirk like this.
  9. DomDom

    DomDom

    Joined:
    Oct 18, 2013
    Posts:
    32
  10. bnatividad

    bnatividad

    Joined:
    Oct 17, 2013
    Posts:
    5
    Hello, I'm trying to achieve recoloring of characters for my game. This looks like it's working but I only need to change colors of specific areas, our artist have assigned them magenta colors. How could I target this as the only ones that need to change value?
     
    franciscochong likes this.
  11. yukamui

    yukamui

    Joined:
    Nov 19, 2013
    Posts:
    11
    Since I don't do frame by frame animation I have splitted the graphics that need different coloring in different sprites. I understund it is probably not the most elegant solution thought:

    Here you can see how it looks:
    https://drive.google.com/file/d/0B_0oND2SmusHWXJ1VzVWWnl4Zlk/view

    On the other hand I found an asset named 2DxFX that let you pick a color range and change it's color (Along other cool features) but have not tryed it myself.

     
  12. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    23
    2dxFX seems like the promised land, but seems a bit expensive.
     
  13. franciscochong

    franciscochong

    Joined:
    Jul 9, 2015
    Posts:
    23
    In case anyone is still looking for this.
    Jesse Freeman has a tutorial on how to achieve this, very easy to follow tutorial:
    http://www.lynda.com/Unity-2D-tutorials/Advanced-Unity-2D-Sprite-Palette-Swapping/383045-2.html

    you will need a lynda.com account tho.
    This is excellent for games where you want to get some variations on reused sprites, characters, enemies, etc.
    You can even create an in game customizer for your character, if you wanted to, following this tutorial
     
    Zealot2018 likes this.