2D Toolkit Forum

2D Toolkit => Support => Topic started by: aphexyuri on February 27, 2012, 03:34:14 pm

Title: tk2sSprite - adjust alpha
Post by: aphexyuri on February 27, 2012, 03:34:14 pm
I'm trying to adjust the alpha on a Sprite, but it does not reflect at runtime. Is this possible, and if so, am I just missing something?
Title: Re: tk2sSprite - adjust alpha
Post by: unikronsoftware on February 27, 2012, 03:49:20 pm
How are you changing these values? Also, can you make sure the range is 0..1 and not 0..255.

Cheers,
unikron
Title: Re: tk2sSprite - adjust alpha
Post by: logick on March 02, 2012, 03:38:15 am
How do i adjust alpha in code?

Thanks!
Title: Re: tk2sSprite - adjust alpha
Post by: unikronsoftware on March 02, 2012, 11:44:19 am
tk2dSprite sprite;
sprite.color = new Color(1, 1, 1, 0.5f);

will make the sprite 50% transparent.
Title: Re: tk2sSprite - adjust alpha
Post by: flying_hamster on April 22, 2012, 09:15:41 pm
I am attempting to use alpha (with the code above) to fade out some things, and all is going well except Text. It wont fade the text. It shows in the inspector during run time as .1 alpha, but in the gameplay scene it is still full alpha.

Not sure what Im doing wrong here.
Title: Re: tk2sSprite - adjust alpha
Post by: unikronsoftware on April 22, 2012, 10:11:25 pm
Did you call Commit after changing the .color? Thats when the mesh is actually rebuilt - and it is done that way so you can batch multiple changes into one mesh rebuild.
Title: Re: tk2sSprite - adjust alpha
Post by: flying_hamster on April 22, 2012, 10:41:24 pm
No I didnt and that worked perfectly thanks!  :D
Title: Re: tk2sSprite - adjust alpha
Post by: mcarriere on April 29, 2012, 04:29:57 pm
I have a similar question, and I figured that it was close enough along the lines of this that making a new thread would be unnecessary.

I originally tried using Unity's animation to animate the alpha of the color property of a sprite. This didn't work.

However, when I change the color value in script, doing something along the lines of this, it works:

Note: direction changes from -1 to 1, depending if color is 1 or 0, and this is run in Update()

Code: [Select]
Color deltaColor = textLoading.color;
deltaColor.a += direction * Time.deltaTime;

textLoading.color = deltaColor;

Can someone explains why I cannot animation the alpha in a Unity Animation?
Title: Re: tk2sSprite - adjust alpha
Post by: unikronsoftware on April 29, 2012, 10:35:34 pm
After changing color / scale on a text mesh, you need to call Commit(). That actually Commits the changes. The reason it's like this is that "cost" of changing a textmesh is significantly higher than a sprite due to the number of triangles in the mesh - separating this stage lets you make all the changes you want to in a frame, and then at the end of the frame, update it.

The reason changing these values doesn't work through the animation editor, is that the animation editor only will animate member variables and not properties. You can actually achieve this by using an adapter behavior which contains member variables, and in the update function, sets these variables to the textmesh and calls commit. You will then be able to animate these values through the animation editor.