Ilingis 发表于 2021-8-13 21:16

xlua-lua脚本使用协程

lua代码如何使用协程?原理很简单,就是把Unity的MonoBehaviour脚本C#对象封装出来给Lua代码用。
1.创建C#协程对象:CoroutineRunner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
*Author:W
* 协程类封装
*/
public class CoroutineRunner : MonoBehaviour {

        // Use this for initialization
        void Start () {
               
        }
       
        // Update is called once per frame
        void Update () {
               
        }
}2.创建Lua协程工具类:CoroutineTool.lua
-- Tencent is pleased to support the open source community by making xLua available.
-- Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
-- Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
-- http://opensource.org/licenses/MIT
-- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

function Log(info)
print("MyLua Log===="..info);
end

--引入xlua工具
local util = require 'xlua.util';

--创建协程游戏对象并添加上协程C#脚本
local gameobject = CS.UnityEngine.GameObject('Coroutine_Runner');
CS.UnityEngine.Object.DontDestroyOnLoad(gameobject);
local coroutineRunner = gameobject:AddComponent(typeof(CS.CoroutineRunner));


return
{
   --协程API封装:开启与关闭
   StartCoroutine = function(...)
      returncoroutineRunner:StartCoroutine(util.cs_generator(...))
   end;

   StopCoroutine = function(coroutine)
   coroutineRunner:StopCoroutine(coroutine)
   end;
}

3.协程测试:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
public class LuaCoroutineTest : MonoBehaviour {

        private LuaEnv luaEnv;

        public TextAsset luaCoroutine;

        void Awake()
        {
                luaEnv = new LuaEnv();
                luaEnv.DoString("require 'CoroutineTest'");
        }

        // Use this for initialization
        void Start () {
               
        }
       
        // Update is called once per frame
        void Update () {
               
        }

        void OnDestroy()
        {
                if (luaEnv != null)
                        luaEnv.Dispose();
        }
}-- Tencent is pleased to support the open source community by making xLua available.
-- Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
-- Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
-- http://opensource.org/licenses/MIT
-- Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

--引入lua协程工具
local coroutineTool = require 'CoroutineTool'

--测试

--开启协程测试
local coroutineA = coroutineTool.StartCoroutine(function()
   print('coroutineA is starting');

   --子协程B执行
   coroutine.yield(coroutineTool.StartCoroutine(function()
      print('coroutineB is starting inside coroutineA');
      coroutine.yield(CS.UnityEngine.WaitForSeconds(1));
      print('coroutineB is running...');
   end));

   print('coroutineB is finished!');

   --协程A执行
   while true do
       coroutine.yield(CS.UnityEngine.WaitForSeconds(1));
           print('coroutineA is running...');
   end

end);

--协程A关闭时间设定
coroutineTool.StartCoroutine(function()
   print('Stop coroutineA after 7 seconds');
   coroutine.yield(CS.UnityEngine.WaitForSeconds(5));
   coroutineTool.StopCoroutine(coroutineA);
   print('coroutineA is stopped');
end)
4.运行结果如下:

页: [1]
查看完整版本: xlua-lua脚本使用协程