Helper (HACK) class to allow the dirty flags on UI elements to be updated when animated using Unity's Legacy system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // <author>Glenn Powell</author> | |
| // <summary> | |
| // Helper (HACK) class to allow the dirty flags on UI elements to be updated when animated using Unity's Legacy system. | |
| // It works by finding all children Animation components of this object, and then individually finding all UI Graphic components under those Animations. | |
| // This allows you to put this behaviour up on a parent object, and have all the animations of it's children UI elements still handled uniquely. | |
| // You could theoretically also put one copy of this component on your Canvas object, and have it handle *all* UI objects, but I haven't personally tried that. | |
| // </summary> | |
| using UnityEngine; | |
| using UnityEngine.UI; | |
| using System.Collections.Generic; | |
| public class LegacyAnimationUI : MonoBehaviour { | |
| // relationship between Animation components and their UI Graphics | |
| public class AnimatedGraphic { | |
| public Animation animation; | |
| public Graphic[] graphics; | |
| public AnimatedGraphic(Animation animation, Graphic[] graphics) { | |
| this.animation = animation; | |
| this.graphics = graphics; | |
| } | |
| } | |
| // list of relationships | |
| private List<AnimatedGraphic> m_AnimatedGraphics = new List<AnimatedGraphic>(); | |
| void Awake() { | |
| // find all legacy animated UI elements | |
| Animation[] animations = GetComponentsInChildren<Animation>(true); | |
| foreach (Animation anim in animations) { | |
| Graphic[] graphics = anim.GetComponentsInChildren<Graphic>(true); | |
| if (graphics.Length > 0) { | |
| m_AnimatedGraphics.Add(new AnimatedGraphic(anim, graphics)); | |
| } | |
| } | |
| if (m_AnimatedGraphics.Count == 0) { | |
| Debug.LogWarning("LegacyAnimationUI couldn't find any Legacy Animated UI components in hierarchy object", gameObject); | |
| enabled = false; | |
| return; | |
| } | |
| } | |
| void Update() { | |
| // set all dirty flags on any animating graphics | |
| foreach (AnimatedGraphic animatedGraphic in m_AnimatedGraphics) { | |
| if (animatedGraphic.animation.isPlaying) { | |
| foreach (Graphic graphic in animatedGraphic.graphics) { | |
| graphic.SetAllDirty(); | |
| // NOTE - One of these may be all that's needed to animate color/alpha, but I made this script as simple and general-purpose as possible | |
| //graphic.SetMaterialDirty(); | |
| //graphic.SetVerticesDirty(); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment