xlua-lua脚本使用协程

18 篇文章 0 订阅
这篇博客介绍了如何通过XLua在Unity中创建C#和Lua之间的协程通信。首先创建了一个C#的CoroutineRunner类来承载协程,然后在Lua中创建CoroutineTool工具类,用于启动和停止协程。在测试部分展示了如何从Lua开始协程,包括在协程内部启动子协程,并在特定时间后关闭协程。
摘要由CSDN通过智能技术生成

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(...)
      return  coroutineRunner: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.运行结果如下:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Data菌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值