Author Topic: Weird behaviour (bug?) trying to animate FillAmount property of Filled Sprite  (Read 5149 times)

oxyscythe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
I've been trying to animate the FilledAmount property of some filled sprites and I've been getting an issue where, when I'd scrub the animation timeline or just play the animation, I wouldn't see the changes on the sprite. Until I clicked on some other editor panel, which would (presumably) the sprite to refresh and *then* I'd see the sprite look like what I'd expect to see at that point in the animation.

I thought maybe it's just an editor only problem so I ran the game but alas, it's still broken in the game (basically the animation would appear as if frozen at some random point at the beginning of the animation)

Any thoughts? Maybe the FilledAmount changes don't cause a regeneration of the sprite mesh?

I have a tiny test project that illustrates the issue, which I set up to make sure it's not something in our code that causes the issue so I can share that if needed

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
My guess is you are changing the private variable -- "mFillAmount". You can't do that -- you need to go through the "fillAmount" property instead, and it's not possible with Unity's animations. You need to create a wrapper script instead.
  1. using UnityEngine;
  2.  
  3. public class FillAnimator : MonoBehaviour
  4. {
  5.     public float fillValue; // <-- animate this
  6.  
  7.     void Update() { GetComponent<UISprite>().fillAmount = fillValue;
  8. }

oxyscythe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Thanks ArenMook, you're right, upon further investigation I realised that this is indeed an obvious issue with the fact that properties don't get exposed in the editor (I was seeing MFillAmount, thinking it was the public property, whereas it is indeed the private one which has just been tagged as Serializable and therefore appears in the Inspector)

So yeah, since I am effectively setting the private member directly, the setter doesn't get called when I am playing the animation, so the geometry doesn't get dirtied so then the animation doesn't appear to be updating.

Whilst investigating I actually stumbled on a workaround whilst investigating which is, if I set keyframes for MColor to run alongside the MFillAmount animation (even if it's something as subtle as keying alpha from 0.99 to 1), then the animation updates properly (presumably Color changes force a full update?)

So I am now trying to decide whether I should go for that or for your approach (which I am a bit hesitant about as I am not too keen on peppering everything with this dummy component just to facilitate animation of a property). Both approaches feel a bit hacky anyway (not sure it's any better to have redundant alpha keyframes). Ideally Unity would just expose public properties in the editor! How cool would that be?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Yeah, would be nice if they did. Although I'd settle for them respecting the [HideInInspector] tag. The fact that a private variable shows up is a Unity bug, in my opinion. I suggest using my approach. Changing the color doesn't guarantee an update either.

oxyscythe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
Oh ok, that's very interesting! As it seems that as the animation has been getting more complicated, the MAlpha workaround doesn't seem to be doing the trick anymore! Basically different "layers" of the animation will randomly appear or disappear (usually based on whether they were recently selected in the inspector, which I guess causes things to redraw)

So you're saying color changes don't guarantee an update either then...

Interesting...right, might have to try your approach then...oh well

oxyscythe

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 13
    • View Profile
So I've gone ahead and implemented this (and even tried various ways to make it a bit more data driven by having a single "AnimatedProperty" component and exposing a list of string properties that the user can define, then fetching those properties on the component and animating them on the update)

Which is all well and good.

However!

Initially it looked like this wasn't working. I would scrub the timeline and play the animation and it would have no effect.

When I ran the game though I realised that actually, it is working, however it's not working in the editor.

Any idea why that might be? I would have thought that the property still gets set as normal in the editor from changes in the animation (it is definitely changing in the inspector), so why would it not update the scene view/

Any further suggestions on what I could do? As, it's all well and good that this technique is working when the game is running, but if you can't see what you're doing when you're animating, then it's all a bit moot really.

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
You need to add [ExecuteInEditMode] above your script for it to work in edit mode.