Unity3D Lua语言学习(一)

来源:互联网 发布:手机淘宝社区首页 编辑:程序博客网 时间:2024/04/30 04:20

我是一名游戏开发者,目前主要是使用Unity3D开发游戏客户端,想学习Lua,掌握热更新技术,尽管近期苹果爸爸宣布要禁止AppStroe上的游戏热更新。

在网上看了许多Lua的学习博客,受益良多,今天总结一下学习Lua的知识。

至于怎样去配置Lua环境和在Vs工程中的添加的引用可以参考链接点击打开链接

这里直接放上我的代码和输出的结果。


C# Main程序

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using LuaInterface;namespace TestLua{    class Program    {        static void Main(string[] args)        {            //创建lua的解释器            Lua lua = new Lua();            Console.WriteLine("C# 中添加lua属性");            // C# 中添加lua属性             lua["num"] = 34;            Console.WriteLine(lua["num"]);            Console.WriteLine();            Console.WriteLine("C# 中编写 lua 脚本");            // C# 中编写 lua 脚本            lua.DoString("name = 'XiaoMing'");            lua.DoString("age = 18");            lua.DoString("print(name, age, num)");            Console.WriteLine();            Console.WriteLine("C# 中执行.lua文件");            // C# 中执行.lua文件            lua.DoFile("Mylua.lua");            Console.WriteLine();            Console.WriteLine("把C#一个类中的普通公共方法注册进去");            // 把C#一个类中的普通公共方法注册进去            Program p = new Program();            // "LuaMethod" 指定注入的方法在lua中的方法名            // p 指定方法所在的对象            // p.GetType().GetMethod("CLRMethod")注入该对象的方法            lua.RegisterFunction("LuaMethod", p, p.GetType().GetMethod("CLRMethod"));            lua.DoString("LuaMethod()");            Console.WriteLine();            Console.WriteLine("把C# 一个共有静态方法注册进去");            // 把C# 一个共有静态方法注册进去            lua.RegisterFunction("MyStaticMethod", null, typeof(Program).GetMethod("MyStaticMethod"));            lua.DoString("MyStaticMethod()");            // 执行.lua文件,并在.lua文件中使用C#的类            lua.DoFile("Script.lua");            Console.ReadKey();        }        public void CLRMethod() {            Console.WriteLine("hehe");        }        public static void MyStaticMethod() {            Console.WriteLine("haha");        }    }}

MyLua.lua 文件中的lua程序,文件需要放在Debug文件夹下

name = "XiaoMing"age = 18print(name,age)

C# 中 Test类

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace TestLua{    class Test    {        private string name = "Yogi";        private int age = 25;        // Default constructor        public Test() {        } //  end  Constructor        // Constructor two!        public Test(string name, int age) {            this.name = name;            this.age = age;        } // end  Constructor        public string Name {            get {                return name;            }        } // end Property        public int Age {            get {                return age;            }        } // end Property        public void Print() {            Console.WriteLine(name + " " + age);        } // end Method        public void OutNameLenghtAndAge(out int lenght) {            lenght = name.Length;        } // end Method        public int ReturnAgeAndRefNameLenght(ref int lenght) {            Console.WriteLine(lenght);            lenght = name.Length;            return age;        } // end Method    }}

Script.lua文件中的lua程序,文件需要放在Debug文件夹下

require "luanet" -- 引入luanet库print()print("Start Lua")luanet.load_assembly("System") -- 加载.dll文件luanet.load_assembly("TestLua")Int32 = luanet.import_type("System.Int32")Test = luanet.import_type("TestLua.Test")print()print("Check import is succeed!")print(Int32)print(Test)print()print("Default constructor!")test1 = Test() -- 使用构造方法1print(test1.Name) -- 访问属性print(test1["Age"]) -- 访问属性test1:Print() -- 访问方法print()print("Constructor two!")test2 = Test("XiaoMing", 18) -- 使用构造方法2print(test2.Name)print(test2.Age)test2:Print()print()print("Test Out Method!")-- Lua 调用 C# 含 out 参数的方法时,无须传入 out 参数。out 参数会以返回值的形式返回-- void 接收函数返回值(函数没有返回值是void为nil)-- lenght 接收 out 返回值(即名字长度)void, lenght = test2:OutNameLenghtAndAge()print(void, lenght)print()print("Test Ref Method!")-- Lua 调用 C# 含 ref 参数的方法时,须要传入 ref 参数。ref 参数也会以返回值的形式返回-- age 接收函数返回值(函数没有返回值是void为nil)-- lenght 接收第一个 ref 返回值(即名字长度)age, lenght = test2:ReturnAgeAndRefNameLenght(100)print(age, lenght)

输出结果:


0 0
原创粉丝点击