Hello Guest

Author Topic: [Solved] animating sprite.color  (Read 5217 times)

chro

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
[Solved] animating sprite.color
« on: January 21, 2013, 10:16:40 am »
hello, I'm trying to animate a sprite's color using unity's animation system.
Unfortunately it's not having any effect. I can see the color changing in the inspector but not in game. After some research it seems calling build() at the end of each frame on the sprite fixes it. I don't really like this solution as it forces me to have an extra script just to do that.
Is there  currently any cleaner way to achieve this ? (like an auto-invalidate flag).

thanks!
« Last Edit: January 22, 2013, 02:51:25 pm by chro »

nikolic

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: animating sprite.color
« Reply #1 on: January 21, 2013, 06:02:47 pm »
Have you tried HOTween? It's free and it's superb.

Here is a single line of code which changes sprite color to another color in 1 second.

Code: [Select]
HOTween.To(spr, 1.0f, new TweenParms().Prop("color", new Color(0.24f, 0.63f, 0.35f, 1.0f));

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: animating sprite.color
« Reply #2 on: January 22, 2013, 11:34:03 am »
hello, I'm trying to animate a sprite's color using unity's animation system.
Unfortunately it's not having any effect. I can see the color changing in the inspector but not in game. After some research it seems calling build() at the end of each frame on the sprite fixes it. I don't really like this solution as it forces me to have an extra script just to do that.
Is there  currently any cleaner way to achieve this ? (like an auto-invalidate flag).

thanks!

Hi,

There is an issue with the built in animation editor in that it wont animate .net properties. The color field is a property, and even though it looks like its animating it, it isn't really animating the correct property.

In order for this to work properly, you should use an adapter class and animate this instead. Example here:

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

You can attach this behaviour to your sprite, and animate this instead of the sprite. It should then animate properly. This can't be integrated into the sprite code, as it would add quite a bit of overhead to sprites that don't animate color.

As nikolic says - if you don't NEED to animate visually, HOTween does some pretty amazing stuff which is probably worth looking into.

chro

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: animating sprite.color
« Reply #3 on: January 22, 2013, 02:51:05 pm »
Thanks a lot for both replies, I'll mark this as solved.

I'm fairly new to the unity animation system (I'm used to doing everything in code). The motivation behind this was simply to prepare the workflow for a collaboration with an 'art guy'.So I wanted to offload as much as possible out of the code. but I can live with this :)

unikron: it seems it's correctly animating the Property since if I call build() in a LateUpdate() it does work correctly.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: [Solved] animating sprite.color
« Reply #4 on: January 22, 2013, 05:49:24 pm »
It isn't actually updating the property. Its updating the internal state variable _color. Unity prettifies the variable and calls it Color :)
Calling Build force builds the sprite, and calling it from LateUpdate will force build it every frame, even if color hasn't changed. Having a second behaviour like the one I posted will be vastly more efficient, unless your sprite changes color every frame.