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
- Unity3D Lua语言学习(一)
- lua语言的学习(一)
- lua语言的学习(一.1)
- lua语言的学习(一.2)
- do Lua语言学习(一) end
- Unity3D热更新<一> 学习Lua
- Lua游戏开发(一)---Lua语言
- lua学习(一)
- lua学习(一)
- lua学习(一)
- unity3d学习(一)
- Lua语言入门(一)
- lua语言的学习一安装
- Lua继承(Lua学习一)
- LUA学习(一) 初步认识lua
- Lua学习笔记(一)
- cocos2d Lua学习(一)
- Lua学习笔记(一)
- PAT1098【插入排序+堆排序】
- 数据库-数据库、基本表、视图的创建,触发器的使用
- pta 习题集5-17 家谱处理
- Dima and Trap Graph CodeForces
- 第一次来
- Unity3D Lua语言学习(一)
- html5自定义属性data-*
- JQuery实现页面倒计时并刷新
- SQL-CHECK约束的用法
- Android安装
- 解决问题:Qt5 OpenCV “uring startup program exited with code 0xc0000135” “ exited with code -1073741515”
- 自动获取类型的auto和decltype类型说明符
- CCF-CSP 201409-4 最优配餐(BFS)
- Java提高篇(三四)-----fail-fast机制