Hello Guest

Author Topic: Changing Sprite Alpha Colour using Unity Animation key-frames  (Read 47950 times)

justaddice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Using Unity 4.1's Animation window to set up keyframe animations by hand I ran into a problem where the 'Color' property dosn't update the object as I play or scrub the animation. I can see the colour changing the in the editor property however.

Screenshot of my animation window: http://puu.sh/2Mud4.png

 guess some kind of update or set function needs to be called.

I read on another post that using HotTween or having a dedicated material assigned to the object would work, but I don't see why animation key-frames shouldn't work as in my animation.

Just purchased 2dtk yesterday, so far getting on well with it and enjoying the crisp graphics.
Cheers for your help.



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #1 on: May 04, 2013, 10:39:38 am »
Attach something like this to the sprite and animate that instead.

Code: [Select]
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class tk2dAnimationAdapter : MonoBehaviour {

public Color color = Color.white;
public Vector3 scale = Vector3.one;
tk2dBaseSprite sprite = null;
tk2dTextMesh textMesh = null;

// Use this for initialization
void Start() {
sprite = GetComponent<tk2dBaseSprite>();
textMesh = GetComponent<tk2dTextMesh>();
if (sprite != null)
color = sprite.color;
if (textMesh != null)
color = textMesh.color;
}

// Update is called once per frame
void Update () {
DoUpdate();
}

void DoUpdate() {
if (sprite != null && (sprite.color != color || sprite.scale != scale)) {
sprite.color = color;
sprite.scale = scale;
}
if (textMesh != null && (textMesh.color != color || textMesh.scale != scale)) {
if (textMesh.color != color) textMesh.color = color;
if (textMesh.scale != scale) textMesh.scale = scale;
textMesh.Commit();
}
}
}

justaddice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #2 on: May 04, 2013, 10:43:00 am »
Thank you that looks great, will test it now.

Considering the naming of the class, is this going to be included as part of a later release?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #3 on: May 04, 2013, 10:46:21 am »
No, I don't think so.
This is just working around a fundamental deficiency in the Unity animation system, that it won't animate properties, so hopefully they'll fix it at some point in the future, and this won't be necessary any more.

justaddice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #4 on: May 04, 2013, 12:15:21 pm »
Hmm, just tried testing this, however it still doesn't seem to be visually updating properly, even though the inspector is looking ok:
http://puu.sh/2Mzd8.png

Strangely, when I first tried it, it did kind of work, but not smoothly (for example the color only got half way in the transition, and it looked like it got stuck between 2 keyframes).

Is there anything else we can try, or should I consider an alternative method to transition my alpha?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #5 on: May 04, 2013, 12:56:58 pm »
This should definitely work, though it can be occasionally a bit funny when running in the editor. Do you still have keyframes on the sprite.color? That could cause issues.

justaddice

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #6 on: May 04, 2013, 02:01:54 pm »
Thanks yes deleting all keyframes from my 2dtkSprite got it.

Annoyingly they are re-added automatically if I scrub through the timeline, but I can just disable the tk2dAnimationAdapter until I'm finished animating, fairly easy to delete them anyway.

Cheers again.

undream

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #7 on: August 13, 2013, 02:32:14 pm »
I have the same problem of keyframes being added, looking for a solution. In my case, it's quite annoying since there are a lot of animated propertes.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #8 on: August 13, 2013, 03:16:06 pm »
You can try this approach to see if it works better for you:
Code: [Select]
using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class tk2dAnimationAdapter : MonoBehaviour {

Color color = Color.white;
Vector3 scale = Vector3.one;
tk2dBaseSprite sprite = null;
tk2dTextMesh textMesh = null;

public Color textColor = Color.white;
public Vector3 textScale = Vector3.one;

// Use this for initialization
void Start() {
sprite = GetComponent<tk2dBaseSprite>();
textMesh = GetComponent<tk2dTextMesh>();
if (sprite != null) {
color = sprite.color;
scale = sprite.scale;
}
if (textMesh != null) {
textColor = textMesh.color;
textScale = textMesh.scale;
}
}

// Update is called once per frame
void LateUpdate () {
DoUpdate();
}

void DoUpdate() {
if (sprite != null && (sprite.color != color || sprite.scale != scale)) {
color = sprite.color;
scale = sprite.scale;
sprite.Build();
}
if (textMesh != null && (textMesh.color != textColor || textMesh.scale != textScale)) {
textMesh.color = textColor;
textMesh.scale = textScale;
textMesh.Commit();
}
}
}

If it does, I can add some support functions to make it work a bit more efficiently in the next release.
« Last Edit: August 14, 2013, 12:38:48 pm by unikronsoftware »

undream

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #9 on: August 13, 2013, 09:23:52 pm »
Thank your for the quick reply. it works well for sprites, except it is 1 frame late (therefore the final frame of animation is stuck)
For me, it does not work for text meshes..

undream

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #10 on: August 13, 2013, 09:27:03 pm »
Animator does not show the changes that happens in text mesh color/alpha

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #11 on: August 13, 2013, 10:52:15 pm »
Thank your for the quick reply. it works well for sprites, except it is 1 frame late (therefore the final frame of animation is stuck)
For me, it does not work for text meshes..

Try the updated one? Should be better, I guess? This one uses LateUpdate instead of Update.

undream

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #12 on: August 13, 2013, 11:10:20 pm »
Definitely better!
What about the textmesh?

As far as I can see, with or wthout the adapter script, textmesh changes are not reflected in the animation window, no keyframes are being added in record mode (for editor changes to color of textmesh) or no animation parameter changes are reflected in the editor window (just like the sprite problem). I think, somehow the animation window and the textmesh color are completely unrelated/seperate.

thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #13 on: August 14, 2013, 12:45:06 pm »
Try that script again. You need to animate the textColor field.

Hopefully Unity fix their editor and allow you to edit properties soon, and none of this will be necessary.

AbsurdInteractive

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Changing Sprite Alpha Colour using Unity Animation key-frames
« Reply #14 on: April 12, 2014, 01:00:31 am »
Any idea if Unity fixed this, or if there's a better solution for this problem?