using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LogosAnimation : MonoBehaviour {
public float FadeInDuration = 1.0f;
public float PauseDuration = 3.0f;
public float FadeOutDuration = 1.0f;
private enum FadeType
{
FadeOut = -1,
Pause = 0,
FadeIn = 1
}
private float _duration = 0.0f;
private float _time = 0.0f;
private int _currentWidget = 0;
private FadeType _currentAnimation;
UIWidget[] _widgets;
// Use this for initialization
void Start ()
{
_widgets = GetComponentsInChildren<UIWidget>();
foreach (UIWidget widget in _widgets)
{
SetWidgetAlpha(widget, 0.0f);
}
SetupAnimation(FadeType.FadeIn);
}
// Update is called once per frame
void Update()
{
_time += Time.deltaTime;
// If there is a widget to animate
if( _currentWidget < _widgets.Length)
{
// Check if the current animation is done.
if(_time > _duration)
{
if (_currentAnimation == FadeType.FadeOut)
{
// Animate the next widget
_currentWidget++;
// Check if all widgets have been animated.
if (_currentWidget >= _widgets.Length)
{
OnAnimationEnd();
}
else
{
SetupAnimation(FadeType.FadeIn);
}
}
else
{
// Next widget animation
SetupAnimation(_currentAnimation - 1);
}
}
else
{
// Apply the alpha modification;
UpdateWidgetAlpha(_widgets[_currentWidget], Time.deltaTime * ((float)_currentAnimation));
}
}
}
private void OnAnimationEnd()
{
// Do some stuff when the animation is done.
// Call the next level for example.
}
private void SetWidgetAlpha(UIWidget widget, float alpha)
{
Color color = widget.color;
color.a = alpha;
widget.color = color;
}
private void UpdateWidgetAlpha(UIWidget widget, float toAdd)
{
SetWidgetAlpha(widget, widget.alpha + toAdd);
}
private void SetupAnimation(FadeType type)
{
_time = 0;
_currentAnimation = type;
switch (type)
{
case FadeType.FadeIn:
_duration = FadeInDuration;
break;
case FadeType.Pause:
_duration = PauseDuration;
break;
case FadeType.FadeOut:
_duration = FadeOutDuration;
break;
}
}
}