Hello Guest

Author Topic: tk2sSprite - adjust alpha  (Read 12031 times)

aphexyuri

  • Newbie
  • *
  • Posts: 14
    • View Profile
tk2sSprite - adjust alpha
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #1 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

logick

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #2 on: March 02, 2012, 03:38:15 am »
How do i adjust alpha in code?

Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #3 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.

flying_hamster

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #4 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #5 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.

flying_hamster

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #6 on: April 22, 2012, 10:41:24 pm »
No I didnt and that worked perfectly thanks!  :D

mcarriere

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #7 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: tk2sSprite - adjust alpha
« Reply #8 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.