2D Toolkit Forum

2D Toolkit => Support => Topic started by: justaddice on May 04, 2013, 07:38:32 am

Title: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: justaddice on May 04, 2013, 07:38:32 am
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.


Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware 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();
}
}
}
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: justaddice 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?
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: justaddice 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?
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: justaddice 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: undream 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: undream 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..
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: undream on August 13, 2013, 09:27:03 pm
Animator does not show the changes that happens in text mesh color/alpha
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: undream 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!
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware 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.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: AbsurdInteractive on April 12, 2014, 01:00:31 am
Any idea if Unity fixed this, or if there's a better solution for this problem?
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware on April 12, 2014, 05:09:07 am
No, there is no update or a better solution to this.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: AbsurdInteractive on June 13, 2014, 03:42:23 am
We have this tk2dAnimationAdapter script on a Sprite in our scene, and when we run it on iOS and  look at the profiler it says this script takes up 25% of the game loop. IS there something we're doing wrong with this script? I thought it was just to modify animation color of sprites and text in Unity animations.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware on June 13, 2014, 08:23:30 am
This script is expected to be rather expensive as it has to continuously poll everything every frame. Thats the reason why it isn't in the actual script itself. Until unity provide a better way to be notified of animation changes (no you cant animate c# properties still) there will be no way to make this appreciably faster.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: cheesus on July 03, 2014, 04:28:17 pm
I've been keeping an eye on this issue & its worrying that there has been no progress in (i think) well over a year now from unity or tk2d. At some point (soon i think) i want to be able to animate the colors of a lot of sprites & the ideal way to do it is with a tool like unitys animation system with scrubbing, editor preview but more importantly all in the same place -  ie a singe animation that controls multiple animating things, its just the way animation on computers is done no?

I dont know if I dreamed it or not but did the 'legacy'  animation system support animating tkd2's sprite colors? I do know its now broken as far as animating material colors goes & its been dropped from development so I probably want to avoid it anyway, i mean check this out its unbelievable that no. 6 on the issue tracker is 'broken by design': http://issuetracker.unity3d.com/issues/cant-animate-color-value-of-materials (http://issuetracker.unity3d.com/issues/cant-animate-color-value-of-materials)

I would like to know if the following statements describe the state of play right now:

 - Unitys animation system only works with Unity sprites if you want to animate colors and gained a competitive advantage over tk2d either by design or accident for its built in 2d system from its initial release to present.

- If I want to stick with tk2d and use use the adapter scripts I will pay a price in performance over using unity sprites.

Now naturally I want to stick with tk2d as much as possible, i mean i'm fully expecting a dev to reply to this -  try posting a similar thread in the unity forums I would expect a wall of silence - but after reading this
Quote
Until unity provide a better way to be notified of animation changes (no you cant animate c# properties still) there will be no way to make this appreciably faster.


I would advise strongly against waiting for the the unity team to deliver the goods.

Could tk2d 'fake' unity sprites somehow? Using the sprite renderer component to get access to the animation system? Or has anyone suggested creating your own animation system? I'm sure whatever unikron came up with would be more steamlined, efficient but mostly better supported than unity's :)

Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: unikronsoftware on July 03, 2014, 05:47:51 pm
The state of things right now
- the animation system doesn't animate c# properties = no way to animate tk2d sprite properties like color
- we cant feed custom mesh data into a sprite renderer, which makes that approach a non-starter
- adapter scripts are one option, you could possibly make them more efficient by having one global watcher dealing with this. This is going to be very specific to a use case, though. I can offer some ideas on what to do to speed this up, but it'll mean writing some code to deal with this.

I dont expect that Unity is going to change this anytime soon. However, I don't have the budget to create an animation system. It would be the ideal solution, but not something that I can pursue.
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: cheesus on July 03, 2014, 06:40:26 pm
thnx for the info...pm sent!
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: moofly on March 02, 2016, 12:27:49 pm
I've been using the above script and it works perfectly with one exception.  It doesn't do anything to tk2dSlicedSprite's - how can I make it work with sliced sprites?
Title: Re: Changing Sprite Alpha Colour using Unity Animation key-frames
Post by: moofly on March 02, 2016, 12:50:48 pm
No, wait, I think I've realised my error.  The script is fine, I just needed to change the animation to animate a tk2dSlicedSprite rather than a tk2dSprite.  Silly me.