using UnityEngine;
public class AnimatedAlphaAutoReset : MonoBehaviour {
public float startAlpha = 1f;
public float finishAlpha = 1f;
public float duration = 0.2f;
private UIWidget _widget;
private UIPanel _panel;
private bool _hasInit;
private float _elapsed;
private void OnEnable() {
if (!_hasInit) {
_hasInit = true;
_widget = GetComponent<UIWidget>();
_panel = GetComponent<UIPanel>();
}
_elapsed = 0f;
SetAlpha(startAlpha);
}
private void Update() {
_elapsed = Mathf.Min(duration, _elapsed + Time.deltaTime);
float progress = _elapsed / duration;
float alphaRange = finishAlpha - startAlpha;
SetAlpha(startAlpha + alphaRange * progress);
}
private void SetAlpha(float value) {
if (_widget != null)
_widget.alpha = value;
if (_panel != null)
_panel.alpha = value;
}
}