lua脚本:Assets/Lua/Test3.lua.txt
local move_end = {} local LuaEnumeratorImpl = {} LuaEnumeratorImpl.__index = LuaEnumeratorImpl function LuaEnumeratorImpl.new(coRun) local obj = {} setmetatable(obj, LuaEnumeratorImpl) obj:ctor(coRun) return obj end function LuaEnumeratorImpl:ctor(coRun) self.Current = nil self.m_CoFunc = function() coRun() return move_end end self.m_Co = nil end function LuaEnumeratorImpl:MoveNext() local flag, result = coroutine.resume(self.m_Co) self.Current = result if self.Current == move_end then self.Current = nil return false end return true end function LuaEnumeratorImpl:Reset() self.m_Co = coroutine.create(self.m_CoFunc) end local enu = nil --该lua table会映射为c#的IEnumerator接口实现对象, StartCoroutine的参数也是IEnumerator类型 enu = LuaEnumeratorImpl.new(function() print("stop coroutine after 5 seconds") coroutine.yield(CS.UnityEngine.WaitForSeconds(5)) Test3MonoInst:StopCoroutine(enu) print("coroutine stoped") end) enu:Reset() Test3MonoInst:StartCoroutine(enu)
Test3.cs
public class Test3 : MonoBehaviour { private LuaEnv m_LuaEnv; void Start() { m_LuaEnv = new LuaEnv(); m_LuaEnv.AddLoader((ref string filePath) => { filePath = filePath.Replace('.', '/'); filePath = $"Assets/{filePath}.lua.txt"; var txtAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(filePath); return Encoding.UTF8.GetBytes(txtAsset.text); }); m_LuaEnv.Global.Set("Test3MonoInst", this); m_LuaEnv.DoString("require('Lua.Test3')"); } void OnDestroy() { if (null != m_LuaEnv) { m_LuaEnv.Dispose(); } } }
标签:end,filePath,self,lua,Unity,LuaEnv,协程,LuaEnumeratorImpl From: https://www.cnblogs.com/sailJs/p/18041236