GetTable
helloz 4/3/2024 Unity3d
Get the lua script bind by Transform
transform: is the variable currently bind to the script on the object, it is of type: Transform
functions
functions | parameter | describe |
---|---|---|
GetTable(string) | 1. lua file name return: Table | There is only one globally. If you bind the same script multiple times, there will be an overwrite situation, and the most recently bound script will be obtained. |
transform:GetTable(string) | 1. lua file name return: Table | Get the lua script of the current transform |
transform:GetTables(string) | 1.lua file name return: List<"Table"> |
example 1
local tf= GameObject.Find('xxx').transform
local tableLs= tf:GetTables('helloTable2')
local table0= tf:GetTable('helloTable2')
1
2
3
2
3
example 2
Two scripts are bind to the same object, "helloTable" and "helloTable2"
helloTable.lua
function Start()
--获取当前transform 的lua 脚本,
local table0= transform:GetTable('helloTable2')
table0.test()
--获取当前transform 的lua 脚本列表,
local tableLs= transform:GetTables('helloTable2')
print(tableLs,tableLs.Count)
for i = 0, tableLs.Count-1 do
tableLs[i].test1() --tableLs[i]:test1()
end
--全局只有一个,如果多次绑定相同的脚本,会出现覆盖情况了,将获取最近绑定的脚本
local table2= GetTable('helloTable2')
table2.test2()
print(table2.hello) -- 5
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
helloTable2.lua
hello =5
function test()
print('i am helloTable2 test')
end
function test1()
print('i am helloTable2 test1')
end
function test2()
print('i am helloTable2 test2')
end
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12