2D Toolkit Forum
2D Toolkit => Support => Topic started 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?
-
How are you changing these values? Also, can you make sure the range is 0..1 and not 0..255.
Cheers,
unikron
-
How do i adjust alpha in code?
Thanks!
-
tk2dSprite sprite;
sprite.color = new Color(1, 1, 1, 0.5f);
will make the sprite 50% transparent.
-
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.
-
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.
-
No I didnt and that worked perfectly thanks! :D
-
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()
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?
-
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.