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