Last active
January 18, 2019 14:31
-
-
Save chooaya/df6b2e67a1399d93f93c4cf658d29c05 to your computer and use it in GitHub Desktop.
Unity(C#)のCoroutineで実現した「渡された処理をwaitTime(単位:秒)の間隔でtime_count回実行される」機能
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
/// <summary> | |
/// 渡された処理をwaitTime(単位:秒)の間隔でtime_count回実行される。 | |
/// </summary> | |
/// <param name="firsttime_action_no_wait">初回は遅延時間経過後に実行かのフラグ、trueの場合、初回は即座に実行される。falseの場合、遅延時間経過後、初回が実行される</param> | |
/// <param name="time_count">実行回数 1以上の整数値を設定してください。-1を設定すると、回数制限は無しとなる(つまり無限ループ)。</param> | |
/// <param name="waitTime">遅延時間/実行の間の間隔時間[秒]</param> | |
/// <param name="action">実行したい処理</param> | |
/// <param name="index">今実行しているのは何回目かの値 actionの一つ目のパラメータ</param> | |
/// <param name="total_count">今実行している処理のトータル回数 actionの二つ目のパラメータ</param> | |
/// <param name="prev_action_result">前回実行処理の返却値 actionの三つ目のパラメータ</param> | |
/// <param name="condition">続行可能かの判定処理 返却値がtrueの場合、続行、falseの場合、続行しない。conditionをnullにすることで、判定処理自体を機能させないようにできる(この場合、常に続行可能という判定)</param> | |
/// <returns></returns> | |
private IEnumerator DelayMethodByByGivenTimeCounter(bool firsttime_action_no_wait,int time_count, float waitTime, System.Func<object, object, object, object> action, System.Func<object, object, object, bool> condition = null, object prev_action_result = null, int? total_count = null, int? index = null) | |
{ | |
if (total_count == null) | |
{ | |
total_count = time_count; | |
} | |
if (index == null) | |
{ | |
index = 0; | |
} | |
index++; | |
if (condition != null) | |
{ | |
bool condition_result = condition(index, total_count, prev_action_result); | |
if (condition_result == false) | |
{ | |
yield break; | |
} | |
} | |
object result; | |
if (time_count > 0) | |
{ | |
if (firsttime_action_no_wait) | |
{ | |
result = action(index, total_count, prev_action_result); | |
if (condition != null) | |
{ | |
bool condition_result = condition(index, total_count, result); | |
if (condition_result == false) | |
{ | |
yield break; | |
} | |
} | |
time_count--; | |
yield return new WaitForSeconds(waitTime); | |
} | |
else { | |
yield return new WaitForSeconds(waitTime); | |
result = action(index, total_count, prev_action_result); | |
if (condition != null) | |
{ | |
bool condition_result = condition(index, total_count, result); | |
if (condition_result == false) | |
{ | |
yield break; | |
} | |
} | |
time_count--; | |
} | |
if (time_count == 0) | |
{ | |
} | |
else { | |
StartCoroutine(DelayMethodByByGivenTimeCounter(firsttime_action_no_wait,time_count, waitTime, action, condition, result, total_count, index)); | |
} | |
} | |
else if (time_count == -1) | |
{ | |
if (firsttime_action_no_wait) | |
{ | |
result = action(index, total_count, prev_action_result); | |
if (condition != null) | |
{ | |
bool condition_result = condition(index, total_count, result); | |
if (condition_result == false) | |
{ | |
yield break; | |
} | |
} | |
yield return new WaitForSeconds(waitTime); | |
} | |
else { | |
yield return new WaitForSeconds(waitTime); | |
result = action(index, total_count, prev_action_result); | |
if (condition != null) | |
{ | |
bool condition_result = condition(index, total_count, result); | |
if (condition_result == false) | |
{ | |
yield break; | |
} | |
} | |
} | |
StartCoroutine(DelayMethodByByGivenTimeCounter(firsttime_action_no_wait, time_count, waitTime, action, condition, result, total_count, index)); | |
} | |
} | |
void Start() | |
{ | |
StartCoroutine(DelayMethodByByGivenTimeCounter(false,-1,5f, (_p_index,_p_total,_p_prev_result) => | |
{ | |
var this_result = Convert.ToInt64(_p_prev_result) - 1; | |
Debug.Log("_p_index:" + _p_index + "_p_total:" + _p_total + "_p_prev_result:" + _p_prev_result + " this_result:" + this_result); | |
return this_result; | |
}, (_p_index, _p_total, _p_prev_result) => | |
{ | |
if (Convert.ToInt64(_p_prev_result) == 94) | |
{ | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
, 99)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment