From 1b85a4983661ea5df1414cc70065374e91eb7f5e Mon Sep 17 00:00:00 2001 From: chorome Date: Wed, 1 Jul 2020 12:36:44 +0900 Subject: [PATCH] ContinueWithCoroutine method supports a return value and a argument --- Assets/VRM/DepthFirstScheduler/Functor.cs | 30 ++++++++++++------- Assets/VRM/DepthFirstScheduler/Schedulable.cs | 23 ++++++++++---- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Assets/VRM/DepthFirstScheduler/Functor.cs b/Assets/VRM/DepthFirstScheduler/Functor.cs index 3f7f6a595..735dbbdf2 100644 --- a/Assets/VRM/DepthFirstScheduler/Functor.cs +++ b/Assets/VRM/DepthFirstScheduler/Functor.cs @@ -75,10 +75,18 @@ namespace DepthFirstScheduler #region CoroutineFunctor public class CoroutineFunctor : IFunctor { - T m_result; public T GetResult() { - return m_result; + if (m_last?.Current == null) return default; + + try + { + return (T)m_last.Current; + } + catch + { + return default; + } } Exception m_error; @@ -87,12 +95,11 @@ namespace DepthFirstScheduler return m_error; } - Func m_arg; - Func m_starter; + Func m_starter; Stack m_it; - public CoroutineFunctor(Func arg, Func starter) + private IEnumerator m_last; + public CoroutineFunctor(Func starter) { - m_arg = arg; m_starter = starter; } @@ -100,9 +107,8 @@ namespace DepthFirstScheduler { if (m_it == null) { - m_result = m_arg(); m_it = new Stack(); - m_it.Push(m_starter(m_result)); + m_it.Push(m_starter()); } try @@ -119,7 +125,7 @@ namespace DepthFirstScheduler } else { - m_it.Pop(); + m_last = m_it.Pop(); } return ExecutionStatus.Continue; } @@ -139,9 +145,11 @@ namespace DepthFirstScheduler public static class CoroutineFunctor { - public static CoroutineFunctor Create(Func arg, Func starter) + /// 引数の型 + /// 結果の型 + public static CoroutineFunctor Create(Func arg, Func starter) { - return new CoroutineFunctor(arg, starter); + return new CoroutineFunctor(() => starter(arg())); } } #endregion diff --git a/Assets/VRM/DepthFirstScheduler/Schedulable.cs b/Assets/VRM/DepthFirstScheduler/Schedulable.cs index 1969df591..3605c1b44 100644 --- a/Assets/VRM/DepthFirstScheduler/Schedulable.cs +++ b/Assets/VRM/DepthFirstScheduler/Schedulable.cs @@ -137,10 +137,10 @@ namespace DepthFirstScheduler return schedulable; } - public Schedulable AddCoroutine(IScheduler scheduler, Func starter) + public Schedulable AddCoroutine(IScheduler scheduler, Func starter) { - var func = CoroutineFunctor.Create(() => default(T), _ => starter()); - var schedulable = new Schedulable(scheduler, func); + var func = CoroutineFunctor.Create(() => default(Unit), _ => starter()); + var schedulable = new Schedulable(scheduler, func); AddChild(schedulable); return schedulable; } @@ -168,15 +168,26 @@ namespace DepthFirstScheduler return schedulable; } - public Schedulable ContinueWithCoroutine(IScheduler scheduler, Func starter) + public Schedulable ContinueWithCoroutine(IScheduler scheduler, Func starter) + { + return ContinueWithCoroutine(scheduler, _ => starter()); + } + + public Schedulable ContinueWithCoroutine(IScheduler scheduler, Func starter) { if (Parent == null) { throw new NoParentException(); } - var func = CoroutineFunctor.Create(() => default(T), _ => starter()); - var schedulable = new Schedulable(scheduler, func); + Func getResult = null; + if (Func != null) + { + getResult = Func.GetResult; + } + + var func = CoroutineFunctor.Create(getResult, starter); + var schedulable = new Schedulable(scheduler, func); Parent.AddChild(schedulable); return schedulable; }