xLua中Lua调用C#
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#的更多相关文章
- 【第二篇】xLua中lua加载方式
		
xLua中lua文件加载方式 1. 直接执行字符串方式 LuaEnv luaenv = new LuaEnv(); luaenv.DoString("CS.UnityEngine.Debu ...
 - xLua中C#调用Lua
		
C#调用Lua 一.前提 这里使用的是XLua框架,需要提前配置xlua,设置加载器路径: 可以参考之前的Blog:<xlua入门基础>: 二.C#调用Lua全局变量 lua中所有的全局变 ...
 - xlua中lua对象到c#对象的转型
		
lua中的类型 基础类型 #define LUA_TNIL 0 #define LUA_TBOOLEAN 1 #define LUA_TLIGHTUSERDATA 2 #define LUA_TNUM ...
 - FreeSWITCH IVR中lua调用并执行nodejs代码
		
一.功能需求: 通过FreeSWITCH的IVR按键调用相应的脚本文件:nodejs提供很多的模组,可以方便的与其它系统或者进行任何形式的通讯,我的应用是通过nodejs发送http post请求: ...
 - lua调用不同lua文件中的函数
		
a.lua和b.lua在同一个目录下 a.lua调用b.lua中的test方法,注意b中test的写法 _M 和 a中调用方法: b.lua local _M = {}function _M.test ...
 - xLua中导出Dotween
		
前言 在xlua的lua脚本中使用dotween,官方的文档中有提到可以导出,但未介绍详细的步骤,相信比较多的朋友有需要,刚好项目中也在使用xlua和dotween,所以做个笔记. 基础知识: xLu ...
 - Cocos2d-x下Lua调用自定义C++类和函数的最佳实践[转]
		
Cocos2d-x下Lua调用C++这事之所以看起来这么复杂.网上所有的文档都没讲清楚,是因为存在5个层面的知识点: 1.在纯C环境下,把C函数注册进Lua环境,理解Lua和C之间可以互相调用的本质 ...
 - Lua与C++交互初探之Lua调用C++
		
Lua与C++交互初探之Lua调用C++ 上一篇我们已经成功将Lua的运行环境搭建了起来,也成功在C++里调用了Lua函数.今天我来讲解一下如何在Lua里调用C++函数. Lua作为一个轻量级脚本语言 ...
 - 【转】Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
		
转自:http://segmentfault.com/blog/hongliang/1190000000631630 关于cocos2d-x下Lua调用C++的文档看了不少,但没有一篇真正把这事给讲明 ...
 
随机推荐
- CentOS7中apache的部署与配置
			
一.apache的部署 输入命令 yum list | grep httpd 查看可安装的软件包,选择"httpd.x86_64"安装. 输入命令 yum install http ...
 - 理解ASP.NET Core - [01] Startup
			
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 准备工作:一份ASP.NET Core Web API应用程序 当我们来到一个陌生的环境,第一 ...
 - spring-data-redis 动态切换数据源
			
最近遇到了一个麻烦的需求,我们需要一个微服务应用同时访问两个不同的 Redis 集群.一般我们不会这么使用 Redis,但是这两个 Redis 本来是不同业务集群,现在需要一个微服务同时访问. 其实我 ...
 - python 动图gif合成与分解
			
合成 #!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import sys import imageio def main(imgs_ ...
 - Linux内核编译配置脚本
			
环境 宿主机平台:Ubuntu 16.04.6 目标机:iMX6ULL Linux内核编译配置脚本 在linux开发过程中熟练使用脚本可以大大简化命令行操作,同时对于需要经常重复操作的指令也是一种备忘 ...
 - java代码覆盖实战
			
Jacoco原理 代码插桩 On-the-fly插桩: JVM中通过-javaagent参数指定特定的jar文件启动Instrumentation的代理程序,代理程序在通过Class Loader装载 ...
 - CSS003. 盒子水平垂直居中简写属性(place-items)
			
place-items CSS 中的 place-items 是一个简写属性 ,它允许你在相关的布局(如 Grid 或 Flexbox)中可以同时沿着块级和内联方向对齐元素 (例如:align-it ...
 - Vue项目-初始化之 vue-cli
			
1.初始化项目 a.Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供: 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-servi ...
 - 剑指offer计划16( 排序简单)---java
			
1.1.题目1 剑指 Offer 45. 把数组排成最小的数 1.2.解法 这题看的题解,发现自己思路错了. 这里直接拿大佬的题解来讲吧. 一开始这里就把创一个string的数组来存int数组 Str ...
 - matlab函数randperm()
			
randperm()会返回一个行向量. 1,randperm(n) 输出一个1×n的矩阵,元素值为1~n的整数,每个元素只出现一次,元素的顺序是随机的. 2,randperm(n,k) 输出一个1×k ...