Last active
October 21, 2019 06:06
-
-
Save hexagit/198627358fa5bfa3afa5e79915dee19a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.UI; | |
| public class CountDown : MonoBehaviour | |
| { | |
| // 開始 | |
| private void Start() | |
| { | |
| _count = _initialCount; | |
| _countText.text = _count.ToString(); | |
| _countTime = 1.0f / _initialSpeed; | |
| } | |
| // 更新 | |
| private void Update() | |
| { | |
| if( _count <= 0 ) return; | |
| _countTime -= Time.deltaTime; | |
| if( _countTime <= 0.0f ) { | |
| --_count; | |
| _countText.text = _count.ToString(); | |
| setNextCountTime(); | |
| } | |
| } | |
| // 次にカウントを更新する時間の設定 | |
| private void setNextCountTime() | |
| { | |
| // 加速の場合の時間の計算 | |
| int counted = _initialCount - _count; | |
| float speedAccel = 0.0f; | |
| float rateAccel = 0.0f; | |
| if( counted <= _accelCount ) { | |
| float rate = counted / (float)_accelCount; | |
| speedAccel = _initialSpeed + (_maxSpeed - _initialSpeed) * _accelCurve.Evaluate(rate); | |
| rateAccel = 1.0f - rate; | |
| } | |
| // 減速の場合の時間の計算 | |
| float speedDecel = 0.0f; | |
| float rateDecel = 0.0f; | |
| if( _count <= _decelCount ) { | |
| rateDecel = 1.0f - _count / (float)_decelCount; | |
| speedDecel = _lastSpeed + (_maxSpeed - _lastSpeed) * _decelCurve.Evaluate(rateDecel); | |
| } | |
| // 速度をブレンド | |
| float speed; | |
| if( rateAccel == 0.0f && | |
| rateDecel == 0.0f ) { | |
| speed = _maxSpeed; | |
| } | |
| else { | |
| speed = Mathf.Lerp(speedAccel, speedDecel, _brendCurve.Evaluate(1.0f - rateAccel / (rateAccel + rateDecel))); | |
| } | |
| // 逆数を取る | |
| _countTime = 1.0f / speed; | |
| } | |
| [SerializeField] Text _countText = null; // 数字を表示するテキスト | |
| [SerializeField] int _initialCount = 300; // 初期値 | |
| [SerializeField] AnimationCurve _accelCurve = null; // 加速のカーブ | |
| [SerializeField] AnimationCurve _decelCurve = null; // 減速のカーブ | |
| [SerializeField] AnimationCurve _brendCurve = null; // 加減速のブレンドのカーブ | |
| [SerializeField] float _initialSpeed = 1.0f; // 初期速度 | |
| [SerializeField] float _lastSpeed = 1.0f; // 最終速度 | |
| [SerializeField] float _maxSpeed = 60.0f; // 最高速度 | |
| [SerializeField] int _accelCount = 90; // 加速する間に数える数 | |
| [SerializeField] int _decelCount = 60; // 減速する間に数える数 | |
| private float _countTime = 0.0f; // 次にカウントが進むまでの時間 | |
| private int _count = 0; // カウントする数字 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment