@@ -26,11 +26,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626
2727public static class LuaCoroutine
2828{
29- static MonoBehaviour mb = null ;
29+ static MonoBehaviour mb = null ;
3030
3131 static string strCo =
3232 @"
33- local _WaitForSeconds, _WaitForFixedUpdate, _WaitForEndOfFrame, _Yield = WaitForSeconds, WaitForFixedUpdate, WaitForEndOfFrame, Yield
33+ local _WaitForSeconds, _WaitForFixedUpdate, _WaitForEndOfFrame, _Yield, _StopCoroutine = WaitForSeconds, WaitForFixedUpdate, WaitForEndOfFrame, Yield, StopCoroutine
3434 local error = error
3535 local debug = debug
3636 local coroutine = coroutine
@@ -54,8 +54,7 @@ function WaitForSeconds(t)
5454 _resume(co)
5555 end
5656
57- comap[co] = true
58- _WaitForSeconds(t, resume)
57+ comap[co] = _WaitForSeconds(t, resume)
5958 return coroutine.yield()
6059 end
6160
@@ -65,8 +64,7 @@ function WaitForFixedUpdate()
6564 _resume(co)
6665 end
6766
68- comap[co] = true
69- _WaitForFixedUpdate(resume)
67+ comap[co] = _WaitForFixedUpdate(resume)
7068 return coroutine.yield()
7169 end
7270
@@ -76,8 +74,7 @@ function WaitForEndOfFrame()
7674 _resume(co)
7775 end
7876
79- comap[co] = true
80- _WaitForEndOfFrame(resume)
77+ comap[co] = _WaitForEndOfFrame(resume)
8178 return coroutine.yield()
8279 end
8380
@@ -87,8 +84,7 @@ function Yield(o)
8784 _resume(co)
8885 end
8986
90- comap[co] = true
91- _Yield(o, resume)
87+ comap[co] = _Yield(o, resume)
9288 return coroutine.yield()
9389 end
9490
@@ -99,21 +95,29 @@ function StartCoroutine(func)
9995 end
10096
10197 function StopCoroutine(co)
102- comap[co] = false
98+ local _co = comap[co]
99+
100+ if _co == nil then
101+ return
102+ end
103+
104+ comap[co] = nil
105+ _StopCoroutine(_co)
103106 end
104107 " ;
105108
106109 public static void Register ( LuaState state , MonoBehaviour behaviour )
107- {
110+ {
108111 state . BeginModule ( null ) ;
109112 state . RegFunction ( "WaitForSeconds" , WaitForSeconds ) ;
110113 state . RegFunction ( "WaitForFixedUpdate" , WaitForFixedUpdate ) ;
111- state . RegFunction ( "WaitForEndOfFrame" , WaitForEndOfFrame ) ;
112- state . RegFunction ( "Yield" , Yield ) ;
113- state . EndModule ( ) ;
114+ state . RegFunction ( "WaitForEndOfFrame" , WaitForEndOfFrame ) ;
115+ state . RegFunction ( "Yield" , Yield ) ;
116+ state . RegFunction ( "StopCoroutine" , StopCoroutine ) ;
117+ state . EndModule ( ) ;
114118
115119 state . LuaDoString ( strCo ) ;
116- mb = behaviour ;
120+ mb = behaviour ;
117121 }
118122
119123 //另一种方式,非脚本回调方式(用脚本方式更好,可避免lua_yield异常出现在c#函数中)
@@ -148,8 +152,9 @@ static int WaitForSeconds(IntPtr L)
148152 {
149153 float sec = ( float ) LuaDLL . luaL_checknumber ( L , 1 ) ;
150154 LuaFunction func = ToLua . ToLuaFunction ( L , 2 ) ;
151- mb . StartCoroutine ( CoWaitForSeconds ( sec , func ) ) ;
152- return 0 ;
155+ Coroutine co = mb . StartCoroutine ( CoWaitForSeconds ( sec , func ) ) ;
156+ ToLua . PushObject ( L , co ) ;
157+ return 1 ;
153158 }
154159 catch ( Exception e )
155160 {
@@ -169,8 +174,9 @@ static int WaitForFixedUpdate(IntPtr L)
169174 try
170175 {
171176 LuaFunction func = ToLua . ToLuaFunction ( L , 1 ) ;
172- mb . StartCoroutine ( CoWaitForFixedUpdate ( func ) ) ;
173- return 0 ;
177+ Coroutine co = mb . StartCoroutine ( CoWaitForFixedUpdate ( func ) ) ;
178+ ToLua . PushObject ( L , co ) ;
179+ return 1 ;
174180 }
175181 catch ( Exception e )
176182 {
@@ -179,7 +185,7 @@ static int WaitForFixedUpdate(IntPtr L)
179185 }
180186
181187 static IEnumerator CoWaitForFixedUpdate ( LuaFunction func )
182- {
188+ {
183189 yield return new WaitForFixedUpdate ( ) ;
184190 func . Call ( ) ;
185191 }
@@ -190,8 +196,9 @@ static int WaitForEndOfFrame(IntPtr L)
190196 try
191197 {
192198 LuaFunction func = ToLua . ToLuaFunction ( L , 1 ) ;
193- mb . StartCoroutine ( CoWaitForEndOfFrame ( func ) ) ;
194- return 0 ;
199+ Coroutine co = mb . StartCoroutine ( CoWaitForEndOfFrame ( func ) ) ;
200+ ToLua . PushObject ( L , co ) ;
201+ return 1 ;
195202 }
196203 catch ( Exception e )
197204 {
@@ -203,7 +210,7 @@ static IEnumerator CoWaitForEndOfFrame(LuaFunction func)
203210 {
204211 yield return new WaitForEndOfFrame ( ) ;
205212 func . Call ( ) ;
206- }
213+ }
207214
208215 [ MonoPInvokeCallbackAttribute ( typeof ( LuaCSFunction ) ) ]
209216 static int Yield ( IntPtr L )
@@ -212,8 +219,9 @@ static int Yield(IntPtr L)
212219 {
213220 object o = ToLua . ToVarObject ( L , 1 ) ;
214221 LuaFunction func = ToLua . ToLuaFunction ( L , 2 ) ;
215- mb . StartCoroutine ( CoYield ( o , func ) ) ;
216- return 0 ;
222+ Coroutine co = mb . StartCoroutine ( CoYield ( o , func ) ) ;
223+ ToLua . PushObject ( L , co ) ;
224+ return 1 ;
217225 }
218226 catch ( Exception e )
219227 {
@@ -233,6 +241,21 @@ static IEnumerator CoYield(object o, LuaFunction func)
233241 }
234242
235243 func . Call ( ) ;
236- }
244+ }
245+
246+ [ MonoPInvokeCallbackAttribute ( typeof ( LuaCSFunction ) ) ]
247+ static int StopCoroutine ( IntPtr L )
248+ {
249+ try
250+ {
251+ Coroutine co = ( Coroutine ) ToLua . CheckObject ( L , 1 , typeof ( Coroutine ) ) ;
252+ mb . StopCoroutine ( co ) ;
253+ return 0 ;
254+ }
255+ catch ( Exception e )
256+ {
257+ return LuaDLL . toluaL_exception ( L , e ) ;
258+ }
259+ }
237260}
238261
0 commit comments