SLua、Lua 初识

直接看代码吧,都是很简单的。

C#代码:
LuaTest.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;
using System.Collections;
using SLua;

public class LuaTest : MonoBehaviour
{
private LuaSvr m_LuaSvr;

void Start ()
{
m_LuaSvr = new LuaSvr();
m_LuaSvr.init(p =>
{
Debug.Log("p = " + p);
},
() =>
{
m_LuaSvr.start("Test");
});
}

void OnGUI()
{
if (GUILayout.Button("lua start"))
{
if (m_LuaSvr.inited)
{
m_LuaSvr.start("Test");
}
else
{
Debug.Log("LuaSvr not init complete!");
}
}
}

void Update ()
{

}
}

Lua代码:
Test.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
-- 导入单个类
-- Random = UnityEngine.Random
-- GameObject = UnityEngine.GameObject
-- WaitForSeconds = UnityEngine.WaitForSeconds
-- Yield = UnityEngine.Yield

-- 加此判断后,代码可以重复执行,否则会报错 MonoBehaviour had existed, import can't overload it.
if not MonoBehaviour then
-- 直接导入UnityEngine
import"UnityEngine"
end

-- 模拟类实现, 导入
local City = require("City")
local Road = require("Road")

-- slua入口函数
function main()
-- 变量
local a = 2
local b = 2.335
local c = "ccc"
local c2 = 'cc2234'
local d = true
local e = [===[alo123"]===]

-- 输出语句
print("this is Test.txt", a, b, c, c2, d, e, f)
-- 字符串连接
print("a = " .. a .. ", b = " .. b)

-- table 类型
local t = {[1] = 3, ["a"] = 4, [3] = 5}
print(t[1], t["a"], t[3])

--[[
块注释
]]


-- 方法调用
test()
classTest()
listTest()
dictinaryTest()
unityTest()
end

function test()
print("test")
-- if 语句
local f = 2;
if f == a then
print("aaa")
else
print("bbb")
end

local count = 3
-- for 语句
for i = 1, count do
print("i = ", i)
end

-- while 语句
while count > 0 do
print("count = ", count)
count = count - 1
end

count = 3
-- until 语句
repeat
print("-- count = ", count)
count = count - 1
until count < 0

print("random = ", random())
count, a, b, c = random()
print("count = ", count, "a = ", a, "b = ", b, "c = ", c)
end

function random()
-- 引用Unity中的Random类
return Random.Range(0, 100), 2, 3, "addd"
end

function classTest()
-- local City = require("City")
-- new City
local city = City:new(100,200)

-- 两种方法调用
print(city:toString())
print(city.toString(city))

-- local Road = require("Road")
local road = Road:new()
road:setPos(3, 3)
print("road.x = " .. road.x .. ", road.z = " .. road.z)
print(road:toString())
end

function listTest()
-- 模拟实现List
local cityList = {}
print("len = " .. table.getn(cityList))
print("len = " .. #cityList)

-- 往List中添加10个Road
for i=1,10 do
local r = Road:new()
r:setPos(i, i)
-- table.insert使用
table.insert(cityList, 1, r)
end

-- table.remove使用
table.remove(cityList, 1)

-- 遍历List输出
for i=1,#cityList do
print(cityList[i]:toString())
end
end

function dictinaryTest()
local dic = {}

for i = 1, 10 do
local r = Road:new()
r:setPos(i, i)
dic[i] = r
end

table.remove(dic, 3)

for i = 1, table.getn(dic) do
print("dic[" .. i .. "] = " .. dic[i]:toString())
end
end


function unityTest()
local gameObj = GameObject.Find("Main Camera")
print("name = " .. gameObj.name)
-- 创建协程
local c = coroutine.create(coroutineTest)
print("coutine.create")
-- 启动协程
coroutine.resume(c)
print("coutine.resume")
end

function coroutineTest()
local go = GameObject("test")
go:SetActive(false)
print("go:SetActive(false)")
Yield(WaitForSeconds(2))
go:SetActive(true)
print("go:SetActive(true)")
Yield(WaitForSeconds(2))
GameObject.Destroy(go)
print("Destroy(go)")
end

City.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
local City = {}

--City.__index = City

City.x = 0
City.y = 0

--- 三个横线是文档注释 parameter style1: x,y
--
function City:new( ... ) -- ...表示可变参数列表
-- body
local args = {...}
-- table.getn 获取长度
local argc = table.getn(args) -- argc = #args

local obj = {}

--setmetatable(obj,City)

-- 对City的变量初始化
for k,v in pairs(City) do
obj[k] = v
end

-- 根据参数进行赋值
if argc == 2 then
-- 类型判断
if type(args[1]) == "number" and type(args[2]) == "number" then
obj.x = args[1]
obj.y = args[2]
end
end

return obj
end

function City:toString()
return self.x .. "," .. self.y
end

return City

Road.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
local Road = {}

Road.x = 0
Road.z = 0

function Road:new()
local obj = {}

for k,v in pairs(Road) do
obj[k] = v
end

return obj
end

function Road:setPos(x, z)
self.x = x
self.z = z
end

function Road:toString()
return "x = " .. self.x .. ", z = " .. self.z
end

return Road

都是很简单的代码,用来熟悉lua语法。