slua的东西不是几句话能讲得完,这里只说结论不说原因,原因有空写个Little Slua工程来解释,下面注释中有几个关键点:
LuaVar系列类:LuaFunction,LuaTable,LuaDelegate的使用,
类型表和实例表,__parent代表继承关系,存ud的表是弱表(可以用来缓存c#中引用类型的object对应ud)
import "UnityEngine"
-- 是否导出了UnityEngine.GameObject类
if not UnityEngine.GameObject or not UnityEngine.UI then
error("Click Make/All to generate lua wrap file")
end
-- cach一下,加速,这是lua的优化tip:cach函数,把变量都定义在函数开头等
local pi=math.pi
local class={}
-- Circle.cs里先dofile把文件加载到global,然后通过luaState[key]把global的值压栈给c#使用
-- c#把入栈的luaState["main"]转换为LuaFunction,然后根据ref压栈并pcall之,得到classtable入栈,然后c#把此table转为LuaTable,
-- 就可以通过LuaTable里的ref访问register中的class这个实例数据了。
function main()
-- 先取得GameObject类型表,然后使用里面注册的Find函数(其实是lclosure),然后调用此lclosure, c#层对应的Find函数被调用,把找到的GameObject实例对象存入ObjectCach中
-- 同时作为ud压入栈,并设置GameObject实例表为该ud的元表,这样ud就可以使用GameObject实例表的Getcomponent函数(lclosure),
-- 同理的得到一个元表为UI.Slider实例表的ud,赋值给slider,counttxt类似
local slider = GameObject.Find("Canvas/Slider"):GetComponent(UI.Slider)
local counttxt = GameObject.Find("Canvas/Count"):GetComponent(UI.Text)
-- onValueChaned是UI.Slider实例表中注入的一个属性表,取属性会调用属性表里的第一个函数(lclosure),c#对应的get函数被调用,一个元表为SliderEvent实例表的ud入栈
-- 而SliderEvent实例表.__parent = UnityEvent_float实例表,后者注入了AddListenner函数(lclosure),调用它会调用c#对应函数,c#中把lua传过来的lfunction转为
-- LuaDelegate ld,接着实例化一个UnityAction<float>的委托,委托里会调用ld,并且委托会被Add到前面的ud在c#中的UnityEvent<float>实例中,
-- 这样,当c#的onValueChanged时就会调用该委托,进而调用ld,进而通过ld里的ref调用register里的lfunction,即下面那个函数
slider.onValueChanged:AddListener(
function(v)
class:init(v)
counttxt.text=string.format("cube:%d",v)
end
)
-- 类型表__call会构造一个c#对象存入ObjectCach中,并作为ud入栈,且把对应的实例表设置为其元表
class.root = GameObject("root")
class.ftext = GameObject.Find("Canvas/Text"):GetComponent(UI.Text)
class.r=10
class.cubes={}
class.t=0
class.f=0
class.framet=0
class.max=0
class:init()
return class
end
function class:init(count)
for _,v in ipairs(self.cubes) do
GameObject.Destroy(v[1])
end
self.cubes={}
self.max=count or 400
local P = Resources.Load("Particle System")
self.colors={Color.red,Color.blue,Color.green,Color.cyan,Color.grey,Color.white,Color.yellow,Color.magenta,Color.black}
-- slua对struct做了分离处理:对于Vector2,3,4,Color,Quantion等,比如把一个Vector2从c#传到lua,lua会生成一个Vector2类型的table存它并使用,
-- 把一个table传到c#,如果它是Vector2类型的,c#会生成一个Vector2存其数据,即slua通过数据拷贝来处理这类struct
-- 对于其他struct,slua当做ud来处理
for i=0,self.max do
local cube = GameObject.CreatePrimitive(PrimitiveType.Cube)
cube.transform.position = Vector3(math.cos(i/self.max*pi*2)*self.r,math.sin( i/self.max*pi*2)*self.r,0)
cube.transform:SetParent(self.root.transform)
local mat=cube:GetComponent(Renderer).material
local box=cube:GetComponent(BoxCollider)
GameObject.Destroy(box)
local p = GameObject.Instantiate(P,Vector3.zero,Quaternion.identity)
p.transform:SetParent( cube.transform )
mat.color=self.colors[math.random(#self.colors)]
table.insert(self.cubes,{cube,mat})
end
end
function class:update() -- gc alloc is zero
for i,v in ipairs(self.cubes) do
local offset = i%2==1 and 5 or -5
local r = self.r+math.sin(Time.time)*offset
local angle= i%2==1 and Time.time or -Time.time
local base=Vector3(math.cos(i/self.max*pi*2+angle)*r,
math.sin(i/self.max*pi*2+angle)*r,0)
v[1].transform.position = base
--v[2].color=self.colors[math.random(#self.colors)]
end
if not self.fogStart or self.t>1 then
self.fogStart=Time.time
self.bgCurrent = Camera.main.backgroundColor
self.bgColor=self.colors[math.random(#self.colors)]
end
self.t=(Time.time-self.fogStart)/10
Camera.main.backgroundColor = Color.Lerp(self.bgCurrent,self.bgColor,self.t)
--calc fps
self.f=self.f+1
self.framet=self.framet+Time.deltaTime
if self.framet>=1 then
self.ftext.text=string.format("fps:%d",self.f)
self.f=0
self.framet=self.framet-1
end
end