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. Python的dict

    dict把key和value关联起来,可以通过 key来查找 value. 花括号 {} 表示这是一个dict,然后按照 key: value, 写出来即可.最后一个 key: value 的逗号可以 ...

  2. 刷题-力扣-113. 路径总和 II

    113. 路径总和 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/path-sum-ii 著作权归领扣网络所有.商业转载请联系 ...

  3. Git配置多账户

    一般情况下,公司代码company_repos/会存放在公司内部的gitlab上,个人代码privacy_repos/会放在github上.因此我们会有两个git账户:公司账号zioyi@campan ...

  4. python pip/anaconda使用笔记

    Switch to Chinese conda Source, config file is in ~/.condarc conda config --add channels https://mir ...

  5. 洛谷P2115 Sabotage G 题解

    题目 [USACO14MAR]Sabotage G 题解 本蒟蒻又来了,这道题可以用二分答案来解决.我们可以设答案最小平均产奶量为 \(x \ (x \in[1,10000])\) .然后二分搜索 \ ...

  6. Jenkins(6)- 新建用户

    如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 进入用户管理 点击新建用户 填写新 ...

  7. SQL-INSERT触发器练习

    &练习一 有这样的一个基础表A,字段包括:id.type.value.create_time,主要是记录某个类型的状态变化时间和值.在插入类型(type)为'runtime' 的数据时,根据前 ...

  8. java基础语法以及进制的转换

    关键字 关键字: 被Java语言赋予特定含义的单词 关键字特点 组成关键字的字母全部小写 关键字注意事项 goto和const作为保留字存在,目前并不使用 类似IDEA这样的集成工具,针对关键字有特殊 ...

  9. 隐私安全设置:NET:ERR_CERT_AUTHORITY_INVALID message in Chrome.

    背景 访问一个内部网站时,遇到下面的问题,导致网站不能打开:NET:ERR_CERT_AUTHORITY_INVALID message in Chrome.从错误信息来看,这是由于网站的证书问题导致 ...

  10. expression动态构成

    http://blog.csdn.net/tastelife/article/details/7340205 http://blog.csdn.net/tastelife/article/detail ...