123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
-
- using UnityEngine;
- using UnityEngine.UI;
- /// <summary>
- /// Tween the object's alpha.
- /// </summary>
- [RequireComponent(typeof(SpriteRenderer))]
- public class TweenAlphaSpriteRenderer : Tweener
- {
- [Range(0f, 1f)]
- public float from = 1f;
- [Range(0f, 1f)]
- public float to = 1f;
- public bool resetOnEnable = false;
- SpriteRenderer mGraphic;
- /// <summary>
- /// Tween's current value.
- /// </summary>
- public float value {
- get {
- if (mGraphic == null)
- mGraphic = GetComponent<SpriteRenderer>();
- return mGraphic.color.a;
- }
- set {
- if (mGraphic == null)
- mGraphic = GetComponent<SpriteRenderer>();
- Color color = mGraphic.color;
- color.a = value;
- mGraphic.color = color;
- }
- }
- protected override void Awake()
- {
- base.Awake();
- mGraphic = GetComponent<SpriteRenderer>();
- }
- 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 TweenAlphaSpriteRenderer Begin(GameObject go, float duration, float alpha)
- {
- TweenAlphaSpriteRenderer comp = Tweener.Begin<TweenAlphaSpriteRenderer>(go, duration);
- comp.from = comp.value;
- comp.to = alpha;
- if (duration <= 0f)
- {
- comp.Sample(1f, true);
- comp.enabled = false;
- }
- return comp;
- }
- public override void SetStartToCurrentValue()
- {
- from = value;
- }
- public override void SetEndToCurrentValue()
- {
- to = value;
- }
- }
|