Hello Guest

Author Topic: Can I make my sprite to be semi-transparent? (Set Alpha value)  (Read 14476 times)

sleepysheep

  • Newbie
  • *
  • Posts: 2
    • View Profile
Can I make my sprite to be semi-transparent? (Set Alpha value)
« 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #1 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.

sleepysheep

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #2 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #3 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;

BraveSirRobin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #4 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #5 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?

brussell

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #6 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #7 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;
}
}
}

brussell

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Can I make my sprite to be semi-transparent? (Set Alpha value)
« Reply #8 on: January 10, 2013, 01:18:06 am »
That makes sense, based on your comments above. Thank you. That worked perfectly.