- Home /
Semi-Transparent Sprite over another Sprite?
In my tile-based game, I'm trying to get the effect of a tile showing it's selected by having it highlighted, but with the original sprite underneath. However, I can't have two sprite renderers attached to the same object to hold both Sprites (The Tile Sprite and the Highlight Sprite).
How can I overlay the sprites or combine them without making a "highlighted tile" for each individual tile?
Note: The Tiles fill a uniform tile size of 32x32 pixels, but some sprites go higher than this, i.e. 32x40. I need the entire 32x40 sprite to be highlighted, not a bunch of 32x32 squares. Here is where my problem really lies.
Answer by Lazdude17 · Jul 29, 2014 at 05:06 PM
You could just change the color in the sprite renderer to a specific color when selected. Or you could go more complicated and create an empty in front of your selected object every time it is selected then make that empty a child of the selected/real object. Assign this empty the same sprite as the selected object and change it's color on the empty with the sprite renderer. Then delete this object on "Not Selected".
void Update()
{
if(SELECTED)
{
Transform highlightSprite = Instantiate(EMPTY WITH BLANK SPRITE RENDERER COMPONENT); //Save as a prefab
highlightSprite.parent = this.transform;
highlightSprite.GetComponent<SpriteRenderer>().sprite = highlightSprite.parent.GetComponent<SpriteRenderer>().sprite;
highlightSprite.GetComponent<SpriteRenderer>().color = SPECIFIC COLOR CODE YOU WOULD LIKE WITH ALPHA
}
else
{
Destroy(highlightSprite.gameObject);
}
}
It's rough code and I would recommend splitting the code up between the two objects involved.
I had a similar idea, and thanks for the great answer, but the problem is that I wanted the "highlight" to be an animated sprite all its own, but the problem with that is the non-uniform sprite sizes...
After thinking about your answer, it turns out, I can make it work. Thanks again!
No problem. Yeah size shouldn't matter at all. If you are wanting the highlight to flash then just be sure to write a 'Flashing.cs or .js' script to animate the flash for you and attach it to the empty prefab and overwrite the prefab so that every instance of it has the flash on it.