• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by ThiagoTejo · Nov 15, 2014 at 04:42 AM · spritecolorpixelchange color

Change sprites colours individualy ( flashing like mario with the star )

I'm making a retro 8 bit plataformer, and I want to know how can I change the pallete of my sprites, similar to the flashing mario, without using other sprite sheets.

This gif from shovel knight is exactly what i want: http://yachtclubgames.com/wp-content/uploads/2014/06/cycle.gif

these too: http://img10.imageshack.us/img10/8199/flashingcolors.png

I dont want to just mess with the color of the material, that tints every color, I'm looking for a way to replace colors individually. ( for making other effects too, like a boss coming out of the dark with the colors gradualy appearing )

Comment
robertbu

People who like this

1 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image incorrect · Nov 15, 2014 at 05:35 AM 0
Share

But color of material makes just what you want.

avatar image Mayank Ghanshala · Nov 15, 2014 at 05:46 AM 0
Share

you could make animation . Where you can choose Add Curve and Sprite and then color. You can play with many options there.

avatar image ThiagoTejo · Nov 15, 2014 at 05:51 AM 0
Share

the color of the material changes every color, and also, I can't change black color by just changing the color of the material. I want to replace colors individually ( including black )

avatar image ThiagoTejo · Nov 15, 2014 at 05:52 AM 0
Share

there's a way to change the colors of a sprite to the negative ones? that would help.

avatar image incorrect · Nov 15, 2014 at 10:30 AM 0
Share

@ThiagoTejo, then you should try changing materials texture! Get it by Texture2D.GetPixels(), process as you want and then return them back with Texture2D.SetPixels().

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by RabidCabbage · Nov 15, 2014 at 07:02 PM

I had a go at this, not able to test currently, could fail spectacularly!

Sorry if a bit hard to read, I added some comments. :)

Just feed it a Texture2D image & associated Rect somehow (there is probably a better way of handling this, that I fail to find right now)

 using UnityEngine;
 using System.Collections.Generic;
 
 public class ColorChanger : MonoBehaviour
 {
     public Texture2D SourceTex;
     public Rect SourceRect;
 
     private int x;
     private int y;
     private int width;
     private int height;
 
     private Color[] pixels;
     private List<Color> colorList;
     private List<Color> backupColorList;
 
     void Start()
     {
         // Get Rect dimensions
         x = Mathf.FloorToInt( SourceRect.x );
         y = Mathf.FloorToInt( SourceRect.x );
         width = Mathf.FloorToInt( SourceRect.width );
         height = Mathf.FloorToInt( SourceRect.height );
 
         // Read image into pixel array
         pixels = SourceTex.GetPixels( x, y, width, height );
     }
 
     // Make a list of all used colors in the image.
     List<Color> GetColorList()
     {
         var cList = new List<Color>();
 
         foreach( var pixel in pixels )
         {
             if( cList.Count == 0 )
             {
                 cList.Add( pixel );
                 Debug.Log( "Added first color: " + pixel );
             }
             else
             {
                 var found = false;
                 foreach( Color c in cList )
                 {
                     if( c == pixel )
                     {
                         found = true;
                         break;
                     }
                 }
                 if( !found )
                 {
                     cList.Add( pixel );
                     Debug.Log( "Added color " + cList.Count + ": " + pixel );
                 }
             }
         }
         return cList;
     }
 
     // Change one color into another.
     void ChangeColor( Color oldC, Color newC )
     {
         for( var i = 0; i < pixels.Length; i++ )
         {
             if( pixels[i] == oldC )
             {
                 pixels[i] = newC;
             }
         }
         SourceTex.SetPixels( x, y, width, height, pixels, 0 );
         SourceTex.Apply();
     }
 
     // or with float's
     void ChangeColorF( float oldR, float oldG, float oldB, float oldA, float newR, float newG, float newB, float newA )
     {
         for( var i = 0; i < pixels.Length; i++ )
         {
             if( pixels[i] == new Color( oldR, oldG, oldB, oldA ) )
             {
                 pixels[i] = new Color( newR, newG, newB, newA );
             }
         }
         SourceTex.SetPixels( x, y, width, height, pixels, 0 );
         SourceTex.Apply();
     }
 }

Then you could do stuff like:

 // Generate a list of unique colors in the image.
 colorList = GetColorList();
 
 // Backup original colors and randomize.
 backupColorList = colorList;
 for( var i = 0; i < colorList.Count; i++ )
 {
     colorList[i] = new Color( Random.Range( 0f, 1f ), Random.Range( 0f, 1f ), Random.Range( 0f, 1f ), 1f );
     ChangeColor( backupColorList[i], colorList[i] );
 }
 
 // Change all Black pixels to White using float's
 ChangeColorF( 0f, 0f, 0f, 1f, 1f, 1f, 1f, 1f );
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by SmilingCatEntertainment · Nov 15, 2014 at 07:21 PM

Ironically, this technique is not as easy on modern, truecolor platforms as it was on the older palettized platforms. In the days of limited colors, this was accomplished by remapping a 2-8 bit color palette of the sprite to different colors, because using 32 bits for one pixel was an unaffordable luxury. Now, we don't have these palettes to play with, and we just use the full 32-bit color spectrum directly.

Some ways to emulate this technique in a truecolor world:

  • Write a custom shader that can do color replacement and maybe even fully emulate the old color palette functionality (pretty advanced).

  • Dynamically change the texture as RabidCabbage suggests (may be too slow if too much is being changed each frame).

  • Have separate textures/sprites for each color combination (which you didn't want but it IS the easiest).

Off the top of my head, if I were to do the shader, I'd try having it take 2 texture inputs - the sprite texture, and a palette texture. I would use the color values from sampling the sprite texture to compute a texture coordinate within the palette texture, and then sample that palette texture for the output color.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SmilingCatEntertainment · Nov 16, 2014 at 04:14 AM 0
Share

I came across some spare time this evening, and came up with the attached implementation of what I was thinking with the shader-based solution. It just requires keeping in mind the following: Source textures must be supplied with the palette index encoded in the red and green components of the texture. Blue is ignored. Ideally, you want to pick your colors so that it samples the center of the color swatch in your palette, as follows:

4-color palettes (palette texture divided into 2x2 squares): Color 0: (64, 64, 0) Color 1: (192, 64, 0) Color 2: (64, 192, 0) Color 3: (192, 192, 0)

16-color palettes (palette texture divided into 4x4 squares): Colors 0-3: (32, 32, 0);(96, 32, 0);(160, 32, 0);(224, 32, 0) Colors 4-7: (32, 96, 0);(96, 96, 0);(160, 96, 0);(224, 96, 0) Colors 8-11: (32, 160, 0);(96, 160, 0);(160, 160, 0);(224, 160, 0) Colors 12-15: (32, 224, 0);(96, 224, 0);(160, 224, 0);(224, 224, 0)

For the source textures, be sure to use point filtering, and clamp if necessary. For the palette textures, be sure to use point filtering and turn off MIP mapping.

I cranked it out in about an hour, so I'm sure it can be improved.

palettized.zip (33.2 kB)
avatar image

Answer by Eric5h5 · Nov 15, 2014 at 08:59 PM

You can just change the sprite color. This will only affect the particular sprite, not all of them. (Because sprites use vertex colors.)

 GetComponent(Sprite).color = ....
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SmilingCatEntertainment · Nov 15, 2014 at 09:23 PM 0
Share

This works if all you want to do to the sprite is tint it. If you want to replace colors individually like the OP does (for example, blue becomes white, black becomes red, yellow becomes geeen), a different technique is needed.

avatar image

Answer by blueLED · Nov 15, 2014 at 09:08 PM

This thread talks about shifting hues on a textures, which, I believe, is all that is happening in those examples you posted.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RabidCabbage · Nov 16, 2014 at 12:36 PM 0
Share

No amount of hue shifting will turn black into red, like the first example shows

avatar image blueLED · Nov 17, 2014 at 01:23 AM 0
Share

They talk about GetPixels and SetPixels. Hue shifting is just a simpler operation, than picking individual colors and changing them.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can i change the color of a pixel in my sprite 0 Answers

Erase certain color from sprite 0 Answers

Changing the color of all children in an empty gameobject 1 Answer

Material doesn't have a color property '_Color' 4 Answers

What is causing a color shift in one scene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges