wangguan
2020-10-27 bac1e673b54b1e5f773c4bd098e1b4fe8981c62e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
namespace MoreMountains.NiceVibrations
{
    public class PowerBarElement : MonoBehaviour
    {
        public float BumpDuration = 0.15f;
        public Color NormalColor;
        public Color InactiveColor;
        public AnimationCurve Curve;
 
        protected Image _image;
        protected float _bumpDuration = 0f;
        protected bool _active = false;
        protected bool _activeLastFrame = false;
 
        protected virtual void Awake()
        {
            _image = this.gameObject.GetComponent<Image>();
        }
 
        public virtual void SetActive(bool status)
        {
            _active = status;
            _image.color = status ? NormalColor : InactiveColor;
        }
 
        protected virtual void Update()
        {
            if (_active && !_activeLastFrame)
            {
                StartCoroutine(ColorBump());
            }
            _activeLastFrame = _active;
        }
 
        protected virtual IEnumerator ColorBump()
        {
            _bumpDuration = 0f;
            while (_bumpDuration < BumpDuration)
            {
                float curveValue = Curve.Evaluate(_bumpDuration / BumpDuration);
                _image.color = Color.Lerp(NormalColor, Color.white, curveValue);
 
                _bumpDuration += Time.deltaTime;
                yield return null;
            }
 
            _image.color = NormalColor;
        }
    }
}