Dynamic Bind Lua
helloz 4/3/2024 Unity3d
lua bind to GameObject
functions
functions | parameter | describe |
---|---|---|
bindScript(GameObject,string) | 1.GameObject 2.lua string return: lua table | Bind the lua string to the GameObject and return the lua table |
bindScriptByname(GameObject,string) | 1.GameObject 2.lua name return: lua table | Bind lua on GameObject, (by lua file name), and return lua table. lua comes from the current game scene |
unBindScriptByname(GameObject,string) | 1.GameObject 2.lua name | remove the bind lua script. If the gameobject object has multiple lua with the same lua file name, they will all be destroyed |
Example : bindScriptByname
function Test()
--testlua lua file name
local tab= bindScriptByname(gameObject,'transformScale_20224754')
--remove
unBindScriptByname(gameObject,'transformScale_20224754')
end
1
2
3
4
5
6
7
2
3
4
5
6
7
Example : bindScript
The "bindScript" is bound by a code string, and there is no file name, so it cannot be uninstalled by "unBindScriptByname".
(其中“bindScript” 是通过代码字符串绑定,不存在文件名称,无法通过“unBindScriptByname” 卸载。)
External uninstall component
function Start()
local tab1=bindScript(gameObject,"print('hello')")
--or local tab1=bindScript(gameObject,[[print('hello')]])
--remove
if tab1 ~=nil then
GameObject.Destroy(tab1.this)
end
end
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Internal uninstall component
function Start()
local tab1=bindScript(gameObject,[[
function Start()
print('hello');
GameObject.Destroy(this);
end
function OnDestroy()
print("I was destroyed")
end
]])
end
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
lua => no GameObject
Lua script execution: unbound GameObject ,Lua's lifecycle developer maintenance. (lua 脚本执行:非绑定GameObject,lua 的生命周期Dev者维护) gms last version >=1303.0
functions | parameter | describe |
---|---|---|
doMetaScriptByname(string) | 1. lua file name return: Table | Get the lua content through the lua file name, and return the meta table |
doScriptByname(string) | 1. lua file name return: object[] | Pass the lua file name and return the data queue (non-meta table) |
doMetaScriptBystring(string) | 1. lua string return: Table | Get the lua content through l and return the metatable |
doScriptBystring(string) | 2. lua file string return: object[] | Pass the lua content and return the data queue (non-meta table) |
use lua filename
--filename: hello1
function test()
print(123)
end
--filename: hello2
local tab={}
function tab:test()
print(456) end
return tab --Pay attention to the return value
-- meta table
local tab =doMetaScriptByname('hello1')
tab.test() -- 123
-- table
local objs =doScriptByname('hello2')
objs[0].test() -- 456
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
use lua string
--return meta table
local str="function test() print(123) end"
local tab =doMetaScriptBystring(str)
tab.test() -- 123
--return object[]
--Note the way str is written, it uses "return" at the end
local str="local tab={} function tab:test() print(456) end return tab"
local tabs =doScriptBystring(str)
tabs[0].test() --456
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10