Skip to content

Instantly share code, notes, and snippets.

@glennpow
Created February 6, 2015 17:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Helper (HACK) class to allow the dirty flags on UI elements to be updated when animated using Unity's Legacy system.
// <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