2D Toolkit Forum

2D Toolkit => Support => Topic started by: Mr Oliv on September 26, 2012, 01:22:43 am

Title: Animate Alpha
Post by: Mr Oliv on September 26, 2012, 01:22:43 am
Hi!

I'm trying to fade in a sprite using the .color property in the tk2dSprite component, but I just can't get it to work... Making a static change works just fine, but when I use the Unity animation component nothing happens. I have the latest 2DTk version installed.

Anyone?

Thanks
Mr O 
Title: Re: Animate Alpha
Post by: Tongie on September 26, 2012, 04:18:07 am
private tk2dAnimatedSprite _sprite;
private float _fadingSpeed = 0.7f;

// Update is called once per frame
void Update ()
{
   StartCoroutine(fadeIn(_sprite, _fadingSpeed));
}

//Door fade in
IEnumerator fadeIn(tk2dAnimatedSprite sprite, float time)
{
   float alpha = 0.0f;
      
   while (alpha < 1.0f)
   {
      alpha += Time.deltaTime * time;
      sprite.color = new Color(255, 255, 255, alpha);
      yield return null;
   }
}

I done my fading my fading like this using co routine. I hope this help.
Title: Re: Animate Alpha
Post by: Mr Oliv on September 26, 2012, 05:23:59 am

Thanks Tongie! This is definitely an option. But still... Shouldn't it be possible to animate the .color property??? Is it a bug or is it designed to be static?

O.

Title: Re: Animate Alpha
Post by: unikronsoftware on September 26, 2012, 04:57:10 pm
This is an issue with the Unity animation system, not a bug but known behaviour. The unity animation system can't animate c# properties, just variables. In order for this to work properly with it, the value will need to be polled every frame and updated.

You can get this to work by attaching a simple script to do it -

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;
}
}
}


Animate this color instead of the one in the tk2dSprite and it should animate fine.
Title: Re: Animate Alpha
Post by: Mr Oliv on September 26, 2012, 07:32:52 pm

Awesome! Thanks!

O