Last active
November 7, 2016 09:26
-
-
Save hacha/942de720178372cac8a8e2252c13c501 to your computer and use it in GitHub Desktop.
数値をカウントアップさせる時によくやる処理
This file contains 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 System; | |
using UniRx; | |
using UniRx.Triggers; | |
public class CountUpStream | |
{ | |
/// <summary> | |
/// カウントアップストリームの作成 | |
/// </summary> | |
/// <param name="start">開始値</param> | |
/// <param name="end">終了値</param> | |
/// <param name="addMin">最小足し込み量</param> | |
/// <param name="minSecPerAdd">足し込み間隔</param> | |
/// <param name="maxSec">Max sec.</param> | |
public static IObservable<long> Create(long start, long end, long addMin, float intervalSec, float maxSec) | |
{ | |
// 総足し込み量 | |
var revise = Mathf.Max(addMin, end - start); | |
// 所要時間 | |
var time = Mathf.Min(maxSec, (float)revise / addMin * intervalSec); | |
// 1回あたりの足し込み量 | |
var add = (long)Math.Ceiling(revise / (time / intervalSec)); | |
// 総回数(startの値と、endの値を流すため+2) | |
var totalCount = 2 + (int)(revise / add); | |
Debug.Log("### カウントアップ処理 " + start + " から " + end + " まで, " + intervalSec + " 秒間隔で " + time + " 秒かけて " + add + " ずつ増やしていく。回数:" + totalCount); | |
return Observable.Interval(TimeSpan.FromSeconds(intervalSec)) | |
.Select(_ => 1) | |
.Scan((a, b) => a + b) | |
.Take(totalCount) | |
.Select(cnt => Math.Min(end, start + add * (cnt - 1))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment