xlua用法,lua脚本调用c#

lua调用c#

lua调用c#的对象

所有c#对象都放在CS中

CS.System;CS.System.IO;CS.UnityEngine.GameObject();

lua调用c#静态变量

基本脚本结构

【luai脚本】

-- 全局变量
s1 = "this is lua"
--引用unity方法
local GameObject = CS.UnityEngine.GameObject
local UI = CS.UnityEngine.UI;

【c#脚本】

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;
using System.IO;

namespace XLuaTest1
{
    public class LuaTest : MonoBehaviour
    {
        private LuaEnv luaenv;
        void Start () 
        {
            // 启动lua虚拟机
            luaenv = new LuaEnv();
            // luaenv.AddLoader(LuaLoader);
            // 读取Resources下的脚本
            TextAsset luaScript = Resources.Load<TextAsset>("lua.lua") as TextAsset;
            // 运行脚本
            luaenv.DoString(luaScript.text, "");
            //获取lua全局属性1
            string mStr = luaenv.Global.Get<string>("s1");
            Debug.Log("lua globle string:" + mStr);
        }
        
        void Update () 
        {
            // 每帧gc
            if (luaenv != null)
            {
                luaenv.Tick();
            }
        }

        void Destroy()
        {
            //释放lua
            if (luaenv != null)
            {
                luaenv.Dispose();
            }
        }
        //脚本读取器
        private byte[] LuaLoader(ref string filename)
        {
            TextAsset text = Resources.Load("Lua/" + filename + ".lua") as TextAsset;
            return text.bytes;
        }
    }
}
调用类

【lua脚本】
引用 CS脚本 -> XluaTest1命名空间 -> mClass类

local mClass = CS.XLuaTest1.myClass;
local testObj = mClass()

【C#脚本】

namespace XLuaTest1
{
    public class myClass
    {
    }
}
调用属性、方法

调用属性、静态属性、静态方法用".“连接。调用方法用”:"连接。
【lua脚本】

-- 引用 CS脚本 -> XluaTest1命名空间 -> mClass类
local mClass = CS.XLuaTest1.myClass;
-- 实例化一个类,不需要new关键字
local testObj = mClass()
-- 调用变量、方法
print("num",testObj.num)
testObj:Func()
-- 调用变量、方法, 原始类名 + . + 变量或方法
print("staticNum",mClass.staticNum)
mClass.staticFunc()

【C#脚本】

namespace XLuaTest1
{
    // [LuaCallCSharp]
    public class myClass
    {
        public static int staticNum = 111;
        public static void staticFunc()
        {
            Debug.Log("静态值 = " + staticNum);
        }
        public int num = 222;
        public void Func()
        {
            Debug.Log("变量值 = " + num);
        }
    }
}
调用枚举

【lua脚本】

-- 实例化类, cs > 命名空间 > 类
local myNum = CS.XLuaTest1.myNum()
-- 引用枚举
local enumNum = CS.XLuaTest1.enumNum

-- 调用c#方法,并传入枚举,返回枚举值
local enumValue = myNum:SwitchNum(enumNum.one)
print("enumValue",enumValue)
-- 根据枚举的value,返回key
myNum:SwitchNum(enumNum.__CastFrom(0))
-- 根据枚举的key,返回值
myNum:SwitchNum(enumNum.__CastFrom("one"))
-- 方法  直接传入枚举的值
myNum:SwitchNum(0)
-- 方法  直接传入枚举的key
myNum:SwitchNum("one");

【C#脚本】

namespace XLuaTest1
{
	// 创建枚举
    [LuaCallCSharp]
    public enum enumNum
    {
        one,
        two
    }
    [LuaCallCSharp]
    // 使用枚举的方法
    class myNum
    {
        public enumNum SwitchNum(enumNum num)
        {
            switch (num)
            {
                case enumNum.one:
                    Debug.Log("one");
                    break;
                case enumNum.two:
                    Debug.Log("two");
                    break;
            }
            return num;
        }
    }
}
调用Action

【lua脚本】

local mClass = CS.XLuaTest1.myClass;
local testObj = mClass()
testObj.act();

【C#脚本】

namespace XLuaTest1
{
    [LuaCallCSharp]
    public class myClass
    {
        public Action act = ()=>{
            Debug.Log("c# Action");
        };
}
调用委托

【lua脚本】

// 自定义方法
function lua_act(arg)
    print("lua_act:", arg)
end
// 实例化类
local testObj = CS.XLuaTest1.mClass()
// 引用委托
local act = testObj.act1
// 将c#脚本中的函数与lua脚本中的函数  交给c#的委托
act = act + lua_act + testObj.act2
// 运行委托
act("lua call c#")

【C#脚本】

namespace XLuaTest1
{
    [LuaCallCSharp]
    public class mClass
    {
    	// 定义一个委托
        public delegate void mDelegate(string arg);
        // 委托函数
        public mDelegate act1 = (string s)=>{
            Debug.Log("act1:" + s);
        };
        // 委托函数
        public mDelegate act2 = (string s)=>{
            Debug.Log("act2:" + s);
            
        };
   }
}
调用事件

【lua脚本】

-- 自定义方法
function lua_act(arg)
    print("lua_act:", arg)
end
-- --实例化类
local testObj = CS.XLuaTest1.mClass()
-- 将lua函数 绑定 c#事件
testObj:mEvent("+", lua_act)
-- 将c#函数 绑定 c#事件
testObj:mEvent("+", testObj.act1)
testObj:mEvent("+", testObj.act2)
-- 通过事先定义的接口 运行c#中的事件
testObj:doEvent("lua call c#")

【C#脚本】

namespace XLuaTest1
{
    [LuaCallCSharp]
    public class mClass
    {
    	// 此处 必须使用该关键字 CSharpCallLua
        [CSharpCallLua]
        // 定义委托
        public delegate void mDelegate(string arg);
        // 定义关键字
        public event mDelegate mEvent;
        // c#方法1
        public mDelegate act1 = (string s)=>{
            Debug.Log("act1:" + s);
        };
        // c#方法2
        public mDelegate act2 = (string s)=>{
            Debug.Log("act2:" + s);
        };
        // 运行
        public void doEvent(string s){
            Debug.Log("event:" + s);
            mEvent("do event");
        }
   }
}
泛型

使用扩展的办法处理lua不支持的泛型需求,或其他需求
【lua脚本】

-- --实例化类
local testObj = CS.XLuaTest1.Extended()
-- 传入目标对象,返回c#进行相应处理
testObj.AddComponentRigidbody(gameobject)

【C#脚本】

namespace XLuaTest1
{
    [LuaCallCSharp]
	 static class Extended{
	    public static Rigidbody AddComponentRigidbody(GameObject gameobject)
	    {
	        return gameobject.AddComponent<Rigidbody>();
	    }
   }
}
静态列表

使用扩展的办法处理lua不支持的泛型需求,或其他需求
【lua脚本】

-- --实例化类
local testObj = CS.XLuaTest1.luaCallCSharpList("")
-- 传入目标对象,返回c#进行相应处理
testObj.AddComponentRigidbody(gameobject)

【C#脚本】

namespace XLuaTest1
{
    [LuaCallCSharp]
    public static List<Type> luaCallCSharpList = new List<Type>()
    {
        typeof(GameObject),
    };
}

调用unity对象

创建unity引用
local GameObject = CS.UnityEngine.GameObject
local UI = CS.UnityEngine.UI;
创建unity对象
local go = GameObject("Cube")
修改unity对象属性
--修改
go.name = "myCube"
--访问
print("GameObject1:", go.name)
调用unity对象的方法
go:SetActive(false)
go.SetActive(go, false)
读取unity静态属性
--访问
local Time = CS.UnityEngine.Time
print("Time.deltaTime:", Time.deltaTime)
--修改
Time.timeScale = 0.5
获取unity场景中的物体
local mainCamera = GameObject.Find("Main Camera")
print("mainCamera:", mainCamera.name)
获取unity物体的组件
--Camera组件
local compCamera = mainCamera:GetComponent("Camera") 
print("compCamera:",compCamera.fieldOfView)
--Transform组件
local compTransform = mainCamera:GetComponent("Transform") 
-- 调用组件下的方法
print("compTransform:",compTransform:GetChild(0).name)
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
千年封包解密库 MICROSOFT FOUNDATION CLASS LIBRARY : SubPacket AppWizard has created this SubPacket application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your SubPacket application. SubPacket.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. SubPacket.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CSubPacketApp application class. SubPacket.cpp This is the main application source file that contains the application class CSubPacketApp. SubPacket.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. SubPacket.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. resSubPacket.ico This is an icon file, which is used as the application s icon. This icon is included by the main resource file SubPacket.rc. resSubPacket.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. AppWizard creates one dialog class: SubPacketDlg.h, SubPacketDlg.cpp - the dialog These files contain your CSubPacketDlg class. This class defines the be
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

千年奇葩

从来没受过打赏,这玩意好吃吗?

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

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

打赏作者

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

抵扣说明:

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

余额充值