找回密码
 立即注册
查看: 322|回复: 0

xlua-lua脚本使用协程

[复制链接]
发表于 2021-8-13 21:16 | 显示全部楼层 |阅读模式
lua代码如何使用协程?原理很简单,就是把Unity的MonoBehaviour脚本C#对象封装出来给Lua代码用。
1.创建C#协程对象:CoroutineRunner
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /*
  5.   *  Author:W
  6.   * 协程类封装
  7.   */
  8. public class CoroutineRunner : MonoBehaviour {
  9.         // Use this for initialization
  10.         void Start () {
  11.                
  12.         }
  13.        
  14.         // Update is called once per frame
  15.         void Update () {
  16.                
  17.         }
  18. }
复制代码
2.创建Lua协程工具类:CoroutineTool.lua
  1. -- Tencent is pleased to support the open source community by making xLua available.
  2. -- Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  3. -- 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
  4. -- http://opensource.org/licenses/MIT
  5. -- 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.
  6. function Log(info)
  7.   print("MyLua Log===="..info);
  8. end
  9. --引入xlua工具
  10. local util = require 'xlua.util';
  11. --创建协程游戏对象并添加上协程C#脚本
  12. local gameobject = CS.UnityEngine.GameObject('Coroutine_Runner');
  13. CS.UnityEngine.Object.DontDestroyOnLoad(gameobject);
  14. local coroutineRunner = gameobject:AddComponent(typeof(CS.CoroutineRunner));
  15. return
  16. {
  17.    --协程API封装:开启与关闭
  18.    StartCoroutine = function(...)
  19.       return  coroutineRunner:StartCoroutine(util.cs_generator(...))
  20.    end;
  21.    StopCoroutine = function(coroutine)
  22.      coroutineRunner:StopCoroutine(coroutine)
  23.    end;
  24. }
复制代码
3.协程测试:
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XLua;
  5. public class LuaCoroutineTest : MonoBehaviour {
  6.         private LuaEnv luaEnv;
  7.         public TextAsset luaCoroutine;
  8.         void Awake()
  9.         {
  10.                 luaEnv = new LuaEnv();
  11.                 luaEnv.DoString("require 'CoroutineTest'");
  12.         }
  13.         // Use this for initialization
  14.         void Start () {
  15.                
  16.         }
  17.        
  18.         // Update is called once per frame
  19.         void Update () {
  20.                
  21.         }
  22.         void OnDestroy()
  23.         {
  24.                 if (luaEnv != null)
  25.                         luaEnv.Dispose();
  26.         }
  27. }
复制代码
  1. -- Tencent is pleased to support the open source community by making xLua available.
  2. -- Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  3. -- 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
  4. -- http://opensource.org/licenses/MIT
  5. -- 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.
  6. --引入lua协程工具
  7. local coroutineTool = require 'CoroutineTool'
  8. --测试
  9. --开启协程测试
  10. local coroutineA = coroutineTool.StartCoroutine(function()
  11.    print('coroutineA is starting');
  12.    --子协程B执行
  13.    coroutine.yield(coroutineTool.StartCoroutine(function()
  14.       print('coroutineB is starting inside coroutineA');
  15.       coroutine.yield(CS.UnityEngine.WaitForSeconds(1));
  16.       print('coroutineB is running...');
  17.    end));
  18.    print('coroutineB is finished!');
  19.    --协程A执行
  20.    while true do
  21.        coroutine.yield(CS.UnityEngine.WaitForSeconds(1));
  22.            print('coroutineA is running...');
  23.    end
  24. end);
  25. --协程A关闭时间设定
  26. coroutineTool.StartCoroutine(function()
  27.    print('Stop coroutineA after 7 seconds');
  28.    coroutine.yield(CS.UnityEngine.WaitForSeconds(5));
  29.    coroutineTool.StopCoroutine(coroutineA);
  30.    print('coroutineA is stopped');
  31. end)
复制代码
4.运行结果如下:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-24 11:31 , Processed in 0.091697 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表