123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// Tween the object's alpha.
- /// </summary>
- [RequireComponent(typeof(CanvasGroup))]
- public class TweenAlphaCanvasGroup : Tweener
- {
- [Range(0f, 1f)]
- public float from = 1f;
- [Range(0f, 1f)]
- public float to = 1f;
- public bool resetOnEnable = false;
- CanvasGroup m_CanvasGroup;
- public CanvasGroup canvasGroup {
- get {
- if (m_CanvasGroup == null)
- m_CanvasGroup = GetComponent<CanvasGroup>();
- return m_CanvasGroup;
- }
- }
- /// <summary>
- /// Tween's current value.
- /// </summary>
- public float value {
- get {
- return canvasGroup.alpha;
- }
- set {
- canvasGroup.alpha = value;
- }
- }
- void OnEnable()
- {
- if (resetOnEnable)
- {
- tweenFactor = 0;
- Sample(0, false);
- }
- }
- /// <summary>
- /// Tween the value.
- /// </summary>
- protected override void OnUpdate(float factor, bool isFinished)
- {
- value = Mathf.Lerp(from, to, factor);
- }
- /// <summary>
- /// Start the tweening operation.
- /// </summary>
- static public TweenAlphaCanvasGroup Begin(GameObject go, float duration, float alpha)
- {
- TweenAlphaCanvasGroup comp = Tweener.Begin<TweenAlphaCanvasGroup>(go, duration);
- comp.from = comp.value;
- comp.to = alpha;
- if (duration <= 0f)
- {
- comp.Sample(1f, true);
- comp.enabled = false;
- }
- return comp;
- }
- public override void Play(bool forward)
- {
- base.Play(forward);
- }
- public override void SetStartToCurrentValue()
- {
- from = value;
- }
- public override void SetEndToCurrentValue()
- {
- to = value;
- }
- }
|