Skip to content

Commit 8334404

Browse files
committed
modify WaitUntil/WaitWhile handler to CustomYieldInstruction handler.
1 parent c43bba8 commit 8334404

File tree

2 files changed

+40
-53
lines changed

2 files changed

+40
-53
lines changed

Assets/EditorCoroutines/Scripts/CoroutineWindowExample.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,28 @@ void OnGUI()
4343
this.StopAllCoroutines();
4444
}
4545

46-
if (GUILayout.Button("WaitUntil/WaitWhile"))
47-
{
48-
_status = false;
49-
this.StartCoroutine(ExampleWaitUntilWhile());
50-
}
51-
52-
if (GUILayout.Button("Switch For WaitUntil/WaitWhile:" + (_status ? "On" : "Off")))
53-
{
54-
_status = !_status;
55-
EditorUtility.SetDirty(this);
56-
}
46+
if (GUILayout.Button("WaitUntil/WaitWhile"))
47+
{
48+
_status = false;
49+
this.StartCoroutine(ExampleWaitUntilWhile());
50+
}
51+
52+
if (GUILayout.Button("Switch For WaitUntil/WaitWhile:" + (_status ? "On" : "Off")))
53+
{
54+
_status = !_status;
55+
EditorUtility.SetDirty(this);
56+
}
5757
}
5858

59-
private bool _status;
59+
private bool _status;
6060

61-
IEnumerator ExampleWaitUntilWhile()
62-
{
63-
yield return new WaitUntil(()=>_status);
64-
Debug.Log("Switch On");
65-
yield return new WaitWhile(()=>_status);
66-
Debug.Log("Switch Off");
67-
}
61+
IEnumerator ExampleWaitUntilWhile()
62+
{
63+
yield return new WaitUntil(()=>_status);
64+
Debug.Log("Switch On");
65+
yield return new WaitWhile(()=>_status);
66+
Debug.Log("Switch Off");
67+
}
6868

6969
IEnumerator Example()
7070
{

Assets/EditorCoroutines/Scripts/EditorCoroutines.cs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,21 @@ public bool IsDone(float deltaTime)
7575
}
7676
}
7777

78-
struct YieldWaitUntil : ICoroutineYield
79-
{
80-
public Func<bool> predicate;
81-
82-
public bool IsDone(float deltaTime)
83-
{
84-
return predicate();
85-
}
86-
}
87-
88-
struct YieldWaitWhile : ICoroutineYield
89-
{
90-
public Func<bool> predicate;
91-
92-
public bool IsDone(float deltaTime)
93-
{
94-
return !predicate();
95-
}
96-
}
97-
98-
struct YieldWWW : ICoroutineYield
78+
struct YieldCustomYieldInstruction : ICoroutineYield
79+
{
80+
public CustomYieldInstruction customYield;
81+
82+
public bool IsDone(float deltaTime)
83+
{
84+
/*
85+
* To keep coroutine suspended return true. To let coroutine proceed with execution return false.
86+
* From: https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html
87+
*/
88+
return !customYield.keepWaiting;
89+
}
90+
}
91+
92+
struct YieldWWW : ICoroutineYield
9993
{
10094
public WWW Www;
10195

@@ -374,21 +368,14 @@ static bool Process(EditorCoroutine coroutine)
374368
float seconds = float.Parse(GetInstanceField(typeof(WaitForSeconds), current, "m_Seconds").ToString());
375369
coroutine.currentYield = new YieldWaitForSeconds() {timeLeft = (float) seconds};
376370
}
377-
else if (current is WaitUntil)
371+
else if (current is CustomYieldInstruction)
378372
{
379-
coroutine.currentYield = new YieldWaitUntil()
380-
{
381-
predicate = (Func<bool>)GetInstanceField(typeof(WaitUntil), current, "m_Predicate")
382-
};
383-
}
384-
else if (current is WaitWhile)
385-
{
386-
coroutine.currentYield = new YieldWaitWhile()
387-
{
388-
predicate = (Func<bool>)GetInstanceField(typeof(WaitWhile), current, "m_Predicate")
389-
};
373+
coroutine.currentYield = new YieldCustomYieldInstruction()
374+
{
375+
customYield = current as CustomYieldInstruction
376+
};
390377
}
391-
else if (current is WWW)
378+
else if (current is WWW)
392379
{
393380
coroutine.currentYield = new YieldWWW {Www = (WWW) current};
394381
}

0 commit comments

Comments
 (0)