xLua中Lua调用C#

1.前提

这里使用的是XLua框架,需要提前配置xlua,设置加载器路径;

可以参考之前的Blog:《xlua入门基础》

//调用段,所有的lua代码都写在LuaCallCSharp.lua文件中
public class LuaCallCSharp1 : MonoBehaviour
{
void Start()
{
XluaEnv.I.DoString("LuaCallCSharp");
} private void OnDestroy()
{
XluaEnv.I.Free();
}
}

2.调用C#类

静态类

public static class TestStatic
{
public static void ShowName(string name, int id)
{
Debug.Log($"name:{name},id:{id}");
}
}

--静态类
CS.TestStatic.ShowName("littlePerilla",1012);

动态类

public class NPC
{
public string name;
public int attack; public NPC(string name,int attack)
{
this.name = name;
this.attack = attack;
} public int Hp { get; set; } public void Attack()
{
Debug.Log($"attack:{attack},Hp:{Hp}");
}
}

--类对象
local hero = CS.NPC("Angel",100)
hero.Hp = 110
hero:Attack()

调用Unity官方Api

--创建物体
local go = CS.UnityEngine.GameObject("LuaObj ")
--添加组件
go:AddComponent(typeof(CS.UnityEngine.BoxCollider))

Lua不支持泛型,所有用到泛型的地方需要把每种可能的重载都写一遍;

调用父类和子类

public class Father
{
public string name = "father"; public virtual void Say()
{
Debug.Log($"{name}:我在被调用");
}
} public class Child :Father
{
public string name = "child"; public override void Say()
{
Debug.Log($"{name}:我在被调用");
}
}

local father = CS.Father()
father:Say() local child = CS.Child()
child:Say()

类拓展方法

拓展类必须为静态类,类必须加特性[LuaCallCSharp];《C#类拓展方法》;

会受到xlua版本和unity版本影响,导致调用失败,xlua官方推荐版本是2017(太过时了);

public class TestExtension
{
public string Test1()
{
return "test";
}
} [LuaCallCSharp]
public static class MyExtension
{
public static void Test2(this TestExtension obj)
{
Debug.Log("ExtensionFunc:"+ obj.Test1());
}
}

local testEx = CS.TestExtension()

print(testEx:Test1())
testEx:Test2()

3.调用C#结构体

结构体和类相似,都有构造方法;

public struct TestStruct
{
public int id;
public string name; public void Output()
{
Debug.Log(id);
Debug.Log(name);
}
}

--结构体
local teststrut = CS.TestStruct() teststrut.id = 12
teststrut.name = "littlePerilla" teststrut:Output()

4.调用C#枚举

枚举使用的userdate自定义数据类型;

public enum State
{
idle = 0,
walk,
run,
attack
}

--枚举使用的userdate自定义数据类型
local state = CS.State.idle print(state) --转换获得枚举
print(CS.State.__CastFrom(1))
print(CS.State.__CastFrom("run"))

5.调用C#中委托

静态委托赋值调用必须释放;

动态委托不必要,但是最好也释放;

调用委托前先做为空判定;

Lua中没有+=或-=方法,只能通过a = a+b来实现多播;

public delegate void DelegateLua();

public class TestDelegate
{
public static DelegateLua deStatic; public DelegateLua deDynamic; public static void Func()
{
Debug.Log("静态委托");
} public void Func2()
{
Debug.Log("动态委托");
}
}

--静态委托赋值调用必须释放
CS.TestDelegate.deStatic = CS.TestDelegate.Func
CS.TestDelegate.deStatic()
CS.TestDelegate.deStatic = nil local func = function ()
-- body
print("lua函数替换委托")
end --lua函数赋值委托
CS.TestDelegate.deStatic = func
--多播委托,确定deStatic不为空,lua没有+=和-=
if(CS.TestDelegate.deStatic ~= nil)then
CS.TestDelegate.deStatic = CS.TestDelegate.deStatic + func
else
CS.TestDelegate.deStatic = func
end --调用前判定不为空
if(CS.TestDelegate.deStatic ~= nil)then
CS.TestDelegate.deStatic()
end CS.TestDelegate.deStatic = nil
--动态委托
local test = CS.TestDelegate() local func1 = function()
print("动态委托调用")
end test.deDynamic = func1
test.deDynamic()
test.deDynamic = nil

6.调用C#事件

事件的调用不能直接复制,必须使用(“+”,function);

事件回调完成也需要释放;

public delegate void EventLua();

public class TestEvent
{
public event EventLua luaEvent1; public static event EventLua luaEvent2; public static void Func()
{
Debug.Log("静态事件");
} public static void CallEvent2()
{
if (luaEvent2 != null)
luaEvent2();
} public void CallEvent1()
{
if (luaEvent1 != null)
luaEvent1();
}
}

--静态事件
CS.TestEvent.luaEvent2("+",CS.TestEvent.Func)
CS.TestEvent.CallEvent2()
CS.TestEvent.luaEvent2("-",CS.TestEvent.Func) --动态事件
local test = CS.TestEvent() local func = function ()
print("动态事件")
end test:luaEvent1("+",func)
test:CallEvent1()
test:luaEvent1("-",func)

xLua中Lua调用C#的更多相关文章

  1. 【第二篇】xLua中lua加载方式

     xLua中lua文件加载方式 1. 直接执行字符串方式 LuaEnv luaenv = new LuaEnv(); luaenv.DoString("CS.UnityEngine.Debu ...

  2. xLua中C#调用Lua

    C#调用Lua 一.前提 这里使用的是XLua框架,需要提前配置xlua,设置加载器路径: 可以参考之前的Blog:<xlua入门基础>: 二.C#调用Lua全局变量 lua中所有的全局变 ...

  3. xlua中lua对象到c#对象的转型

    lua中的类型 基础类型 #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUM ...

  4. FreeSWITCH IVR中lua调用并执行nodejs代码

    一.功能需求: 通过FreeSWITCH的IVR按键调用相应的脚本文件:nodejs提供很多的模组,可以方便的与其它系统或者进行任何形式的通讯,我的应用是通过nodejs发送http post请求: ...

  5. lua调用不同lua文件中的函数

    a.lua和b.lua在同一个目录下 a.lua调用b.lua中的test方法,注意b中test的写法 _M 和 a中调用方法: b.lua local _M = {}function _M.test ...

  6. xLua中导出Dotween

    前言 在xlua的lua脚本中使用dotween,官方的文档中有提到可以导出,但未介绍详细的步骤,相信比较多的朋友有需要,刚好项目中也在使用xlua和dotween,所以做个笔记. 基础知识: xLu ...

  7. Cocos2d-x下Lua调用自定义C++类和函数的最佳实践[转]

    Cocos2d-x下Lua调用C++这事之所以看起来这么复杂.网上所有的文档都没讲清楚,是因为存在5个层面的知识点: 1.在纯C环境下,把C函数注册进Lua环境,理解Lua和C之间可以互相调用的本质 ...

  8. Lua与C++交互初探之Lua调用C++

    Lua与C++交互初探之Lua调用C++ 上一篇我们已经成功将Lua的运行环境搭建了起来,也成功在C++里调用了Lua函数.今天我来讲解一下如何在Lua里调用C++函数. Lua作为一个轻量级脚本语言 ...

  9. 【转】Cocos2d-x下Lua调用自定义C++类和函数的最佳实践

    转自:http://segmentfault.com/blog/hongliang/1190000000631630 关于cocos2d-x下Lua调用C++的文档看了不少,但没有一篇真正把这事给讲明 ...

随机推荐

  1. ASP截取字符 截取字符之间的字符

    ASP截取字符:MID函数Mid(变量或字串符,开始字节, 结尾字节(可不填)) InStrRev(变量, "字串符")  最后出现位置InStr(变量, "字串符&qu ...

  2. centos7 grep 的使用

    2021-07-29 grep(Global search Regular Expression and Print out the line) "Global search" 表 ...

  3. 一种封装Retrofit的方法,可以自动解析Gson,回避Method return type must not include a type variable or wildcard: retrofit2.Call<T>的问题

    封装目的:屏蔽底层实现,提供统一接口,并支持Gson自动转化 最初封装: //请求方法 interface RequestListener { interface PostListener { @PO ...

  4. docker快速创建轻量级的可移植的容器(一)

    系列其他内容 docker快速创建轻量级的可移植的容器✓ docker&flask快速构建服务接口 docker&uwsgi高性能WSGI服务器生产部署必备 docker&gu ...

  5. Pytest系列(22)- allure的特性,@allure.link()、@allure.issue()、@allure.testcase()的详细使用

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 上一篇文章介绍了两种allu ...

  6. k8s garbage collector分析(1)-启动分析

    k8s garbage collector分析(1)-启动分析 garbage collector介绍 Kubernetes garbage collector即垃圾收集器,存在于kube-contr ...

  7. Git 系列教程(6)- 查看 commit 提交历史

    查看提交历史 在提交了若干更新,又或者克隆了某个项目之后,如何查看提交历史 git log 官方栗子 运行下面的命令获取该项目: git clone https://github.com/scha 运 ...

  8. MacOS隐藏及显示文件

    ​ 显示隐藏文件 显示所有文件 defaults write com.apple.finder AppleShowAllFiles -boolean true killall Finder 不显示隐藏 ...

  9. Nginx优化与防盗链

    目录: 一.隐藏版本号 二.修改用户与组 三.缓存时间 四.日志切割 五.连接超时 六.更改进程数 七.配置网页压缩 一.隐藏版本号 可以使用 Fiddler 工具抓取数据包,查看 Nginx版本 也 ...

  10. 使用Java MVC模式设计一个学生管理系统

    最近在做web实验,要求是用jsp+servlet+mysql实现一个学生管理系统,完成对数据库的增删改查. 效果图:   代码: package dao; import java.util.List ...