赞
踩
1.coruntine_test.lua
- local cs_coroutine = (require 'cs_coroutine')
-
- local a = cs_coroutine.start(function()
- print('coroutine a started')
-
- coroutine.yield(cs_coroutine.start(function()
- print('coroutine b stated inside cotoutine a')
- coroutine.yield(CS.UnityEngine.WaitForSeconds(1))
- print('i am coroutine b')
- end))
- print('coroutine b finish')
-
- while true do
- coroutine.yield(CS.UnityEngine.WaitForSeconds(1))
- print('i am coroutine a')
- end
- end)
-
- cs_coroutine.start(function()
- print('stop coroutine a after 5 seconds')
- coroutine.yield(CS.UnityEngine.WaitForSeconds(5))
- cs_coroutine.stop(a)
- print('coroutine a stoped')
- end)

2.cs_coroutine.lua
- local util = require 'xlua.util'
-
- local gameobject = CS.UnityEngine.GameObject('Coroutine_Runner')
- CS.UnityEngine.Object.DontDestroyOnLoad(gameobject)
- local cs_coroutine_runner = gameobject:AddComponent(typeof(CS.XLuaTest.Coroutine_Runner))
-
- return {
- start = function(...)
- return cs_coroutine_runner:StartCoroutine(util.cs_generator(...))
- end;
-
- stop = function(coroutine)
- cs_coroutine_runner:StopCoroutine(coroutine)
- end
- }
3.C# CoroutineTest
- using UnityEngine;
- using XLua;
- namespace XLuaTest
- {
- public class CoroutineTest : MonoBehaviour
- {
- LuaEnv luaenv = null;
- // Use this for initialization
- void Start()
- {
- luaenv = new LuaEnv();
- luaenv.DoString("require 'coruntine_test'");
- }
-
- // Update is called once per frame
- void Update()
- {
- if (luaenv != null)
- {
- luaenv.Tick();
- }
- }
-
- void OnDestroy()
- {
- luaenv.Dispose();
- }
- }
- }

4.C# Coroutine_Runner
- using UnityEngine;
- using XLua;
- using System.Collections.Generic;
- using System.Collections;
- using System;
-
- namespace XLuaTest
- {
- public class Coroutine_Runner : MonoBehaviour
- {
- }
- public static class CoroutineConfig
- {
- [LuaCallCSharp]
- public static List<Type> LuaCallCSharp
- {
- get
- {
- return new List<Type>()
- {
- typeof(WaitForSeconds),
- typeof(WWW)
- };
- }
- }
- }
- }

5.调试
CoroutineTest.cs挂载到相机后运行即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。