- Home /
Changing the alpha channel of a sprite using the new Unity sprite system
I'm trying to change the alpha channel of a sprite, i've already checked around and i couldn't find a solution to my problem because the solutions i've come across don't sound good in my situation. Many of the solutions i've found dealt with the old way of using Unity to create 2D games, I'm using sprites... and i would like to only use sprites.
I'm trying to create a trail effect when my player runs if he has a certain power up. So what i'm trying to do is create a trail game object that has a script that sets it's sprite to the players current sprite index. Then decrease the alpha channel and destroy the object once the alpha channel has reached or is below zero.
My first problem is that the color of sprites are read only so i can not set the alpha channel of the objects sprite.
I then tried to convert the sprite to a texture2d, and some how set the color of those pixels. The only way i know how to do that would be to use SetPixels( ) which takes an array of colors but that sounds crazy and i don't know exactly how to go about doing that.
I also tried converting the texture to a gui texture, but no luck there either.
I don't want to have to use a plane or create separate cropped images of my sprite sheet and set them to another texture type individual. Like i said i'd like to just read the sprite and modify the alpha channel in code.
This is what i have so far:
using UnityEngine;
using System.Collections;
public class PlayerTrail : MonoBehaviour
{
public Sprite sprite;
public float alpha = 255f;
private Component spriteRenderer;
private void Awake ()
{
spriteRenderer = GetComponent<SpriteRenderer> ();
// Get sprite
sprite = GameObject.FindWithTag ("Player").GetComponent<SpriteRenderer> ().sprite;
// Convert sprite
Texture2D croppedTexture = new Texture2D( (int)sprite.rect.width, (int)sprite.rect.height );
var pixels = sprite.texture.GetPixels((int)sprite.textureRect.x,
(int)sprite.textureRect.y,
(int)sprite.textureRect.width,
(int)sprite.textureRect.height);
croppedTexture.SetPixels (pixels);
croppedTexture.Apply();
}
private void Update ()
{
alpha -= 1f;
// Change the sprites alpha channel here
if (alpha <= 0)
GameObject.Destroy (gameObject);
}
}
Is what i'm trying to do possible or is there a better way of doing it? Thanks in advance.
Answer by Q-ro · Mar 01, 2016 at 01:35 AM
A bit lost as to what you are trying to achieve but i think when you talk about "alpha channel" you mean the transparency of the sprite, is thatcorrect ? if that is the case what you would do is the following:
//Get all the sprite renderers for your game object
SpriteRenderer[] normalizeSprites = GetComponentsInChildren<SpriteRenderer>();
//Set up your starting alpha
float alpha = 1.0f ;
//then in your update method do as follows
void update()
{
if (alpha <= 0)
GameObject.Destroy (gameObject);
for (int i = 0; i < sprites.Length; i++)
{
// change the alpha the alpha
sprites[i].color = new Color(sprites[i].color.r, sprites[i].color.g, sprites[i].color.b, alpha);
}
//decrease the alpha value
alpa -= 0.1f;
}
Your answer
Follow this Question
Related Questions
Difficulty loading alpha textures at runtime 0 Answers
Get sprite to have top-left corner always at top-left of screen 0 Answers
Get a portion of a Texture2D to another Texture2D 1 Answer
Changing color of texture with transparency efficiently 0 Answers
Load Texture2D converted into Sprites; How can i add them to a GameObject sucessfully? 1 Answer