- Home /
Is there a way to set a sprite's color solid white?
I neeed to set a sprite color to solid white, to the point where it is only recognizable by its silhouette.
The problem is: Setting a sprite's color to white(1.0f, 1.0f, 1.0f) will result in the sprite having it's "natural" color.
Funny thing is, if you set it to black, the effect I get is the one I'm aiming for... Except it's black.
(I'm using Unity's Sprite Renderer).
Answer by nyonge · Dec 15, 2013 at 10:05 AM
You could argue that it's a bit hacky, but using a material with a GUI Text Shader is working totally fine for me!
6 years later, still a good solution. Took me a $$anonymous$$ute because I kept looking in the UI list and missed that there was a GUI there as well.
You could argue that it's a bit straightforward, but I love you!
Answer by ericdl · May 08, 2014 at 04:52 PM
To expand on nyonge's suggestion, here's one way to set a SpriteRenderer's shader to a GUI Text shader at runtime:
using UnityEngine;
using System.Collections;
public class myClass : MonoBehaviour {
private SpriteRenderer myRenderer;
private Shader shaderGUItext;
private Shader shaderSpritesDefault;
void Start () {
myRenderer = gameObject.GetComponent<SpriteRenderer>();
shaderGUItext = Shader.Find("GUI/Text Shader");
shaderSpritesDefault = Shader.Find("Sprites/Default"); // or whatever sprite shader is being used
}
}
To set the sprite to white:
void whiteSprite() {
myRenderer.material.shader = shaderGUItext;
myRenderer.color = Color.white;
}
And then to set the sprite back to normal:
void normalSprite() {
myRenderer.material.shader = shaderSpritesDefault;
myRenderer.color = Color.white;
}
You are a legend! Thank you
Also to simplify I would suggest ins$$anonymous$$d to hard code the name of the original shader you can use this line of code ins$$anonymous$$d: shaderSpritesDefault = myRenderer.material.shader;
Answer by Cassos · Apr 29, 2020 at 10:43 AM
You could add a mask to the sprite renderer/Image and then create a child that's empty and white, so you just got the white silhouette.
Greetings,
Max
Answer by Spinnernicholas · Nov 26, 2013 at 11:48 PM
You would have to write a new shader to replace the color of every pixel with white.
Then you could replace the default material on the SpriteRenderer.material with a duplicate that has the new shader.
You would have to work around the SpriteRenderer and be careful not to change the default sprite shader and make all your sprites white.
This is definitely a task, but there is some good information out there on shaders in Unity. If you get lost, let me know and I'll see if I can help.
A technologically simpler solution would just be to create white versions of your sprites.
Yeah, I ended up doing that since I'm not yet capable of writing a shader. Thanks a lot, though!
Your answer
Follow this Question
Related Questions
Translucent sprites appear solid 0 Answers
Sprite not updating color. 1 Answer
Sprite alpha colour not changing via script 1 Answer