2D Toolkit Forum

2D Toolkit => Support => Topic started by: sleepysheep on September 22, 2012, 01:46:10 am

Title: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: sleepysheep on September 22, 2012, 01:46:10 am
I have started to 2D Toolkit.
So far, I am happy with it.
However, I tried to set my sprite to be semi-transparent but I cannot find about alpha setting stuff.
I looked in document but I still do not get my answer.

Can I make my sprite to be semi-transparent? Can I set sprite's alpha?
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: unikronsoftware on September 22, 2012, 02:52:29 am
You set the alpha on color.a. You can do that on the color swatch in the sprite inspector, or by modifying color.a on the tk2dSprite.
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: sleepysheep on September 22, 2012, 02:53:58 pm
You set the alpha on color.a. You can do that on the color swatch in the sprite inspector, or by modifying color.a on the tk2dSprite.

I tried it but I got an error.
 "Cannot modify a value type return value of `tk2dBaseSprite.color'"

my code is :
myTk2dsSprite.color.a = 1.0f;
I cannot set the value.
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: unikronsoftware on September 22, 2012, 07:48:15 pm
sprite.color is a property.
You will need to do this:

Code: [Select]
Color c = myTk2dsSprite.color;
c.a = 1.0f;
myTk2dsSprite.color = c;
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: BraveSirRobin on January 07, 2013, 09:06:38 pm
sprite.color is a property.
You will need to do this:

Code: [Select]
Color c = myTk2dsSprite.color;
c.a = 1.0f;
myTk2dsSprite.color = c;

I tried changing the alpha of a tk2dsprite in an animation, changing the Color.a form 1.0 to 0.0 in the tk2dsprite but the sprite is still showing.

The shader I am using is tk2d/BlendVertexColor

Any suggestions?
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: unikronsoftware on January 08, 2013, 04:29:42 am
That definitely works. How are you getting myTk2dsSprite, and is it a correct reference? No errors in output log?
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: brussell on January 09, 2013, 08:23:21 pm
I'm trying this and failing, too. I created an Animation in Unity. The animation is modifying the sprite's transform's scale and position correctly. Then it tries to fade out the tk2dsprite. I've put an animation curve on the color.a to go from 1.0 to 0.0 and the sprite doesn't fade out. In the Inspector, I can actually see the black bar move across the bottom of the color box for the sprite, but in the Scene and Game windows (and on device) the sprite stays fully visible.
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: unikronsoftware on January 10, 2013, 12:48:35 am
Ah, the issue with the animation editor is different. The animation editor in the current versions of Unity will only edit the internal private state variables (_color) and not the property (color). This is a limitation of the animation editor, and it doesn't help matters that it will "beautify" the member names (_color becomes Color) so it isn't apparent what is happening. The property is what actually changes the color on the sprite, so you don't see any change.

If you want to use the animation editor, you should attach a behaviour similar to this and animate the values of that instead.

Code: [Select]
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class tk2dAnimationAdapter : MonoBehaviour {

public Color color = Color.white;
tk2dBaseSprite sprite = null;

// Use this for initialization
void Start() {
sprite = GetComponent<tk2dBaseSprite>();

if (sprite != null)
{
sprite.color = color;
}
}

// Update is called once per frame
void Update () {
if (sprite != null && sprite.color != color)
{
sprite.color = color;
}
}
}
Title: Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
Post by: brussell on January 10, 2013, 01:18:06 am
That makes sense, based on your comments above. Thank you. That worked perfectly.