您的位置:首页 > 移动开发 > Unity3D

Unity中另类的使用Lua的一种方式

2017-08-02 12:05 459 查看

自定义的Unity中Lua粘合(基于Tolua#)

为了适应很多组里的程序不习惯写lua,而且习惯了挂组件。。。开发了这套工具。Lua的组件很像C#的behavior了。性能不是很优秀,但是易上手,适合快速开发很小的项目。想要源代码的可以加我QQ。下面只是贴出具体的使用和功能。

基础功能

扩展功能

基础功能

Lua组件脚本的创建

在对应需要创建的文件夹下右键菜单创建文件,点击后输入文件名,会在文件中自动创建对应名称类(类似C#中的组件类)



创建后的类文件如下图,如非必要,请勿修改基础代码。

--游戏lua组件模版 Editor中通过Luacomponent挂载
--[====[ *******添加序列化内容 请勿删除********

******************************************]====]
print("Example")
Example = class("Example",LuaBehaviour)
function Example:ctor()
Example.super.ctor(self)
self.name = "Example"
end

function Example:Awake()

end

function Example:Start()

end

function Example:Update()

end

return Example


Lua组件脚本的挂载

如果需要将一个lua文件挂载到对象上请先挂在C#组件 LuaComponent,如图所示

黑色区域显示红字,代表当前组件尚未挂载lua组件类。此时必须拖动创建好的lua类文件到黑色框内。黑色区域显示青色类名,则代表已经绑定了Lua组件类。

Lua组件脚本绑定序列化属性

手动添加绑定:点击上图中六个添加按钮分别可以添加不同属性

注意事项:

值类型包括
int
,
float
,
string
,
bool
四种。

对象类型可以拖入
Gameobject
,或材质贴图等
UnityEngine.Object
类型对象

集合类型可以使用
Vector4
,
Vector3
,
Vector2
,
Color
,
Quaternion
五种类型

每个类型的第一个输入框为属性名称,如其中
valueInt
可以在lua类中直接通过
self.valueInt
来访问

数组类型在lua中都是索引从1开始的lua的数组,如
#self.arr_object=3


点击全部清除可以删除所有

如果挂载的是
Gameobject
,可以从下拉菜单中选择挂载对象所挂载的其他组件如
Transform
MeshRender
等,包括Lua组件。其中lua组件可以精确到哪一个lua类的组件。如果是C#组件,在Lua中获取到的是C#对象。如果是Lua组件,获取到的是Lua组件挂载的Lua类对象(通常是一个Table),可以直接访问其中的属性,调用其中的方法。

通过Lua中的定义来生成:点击黑色框旁边的打开按钮,打开lua类文件。在注释中加入下面代码,然后点击加载lua定义属性按钮,之后可以看到自动加载了对应的绑定。 强烈推荐使用这种方式绑定

--游戏lua组件模版 Editor中通过Luacomponent挂载
--[====[ *******添加序列化内容 请勿删除********
Serialize<Object>(a)
Serialize<Boolean>(b,false)
Serialize<Number>(c,0)
Serialize<String>(c,0)
Serialize<Vector4>(d)
******************************************]====]




支持的绑定代码如下表所示

示例代码对应类型默认参数
Serialize<Boolean>(a,false)
booltrue
Serialize<Number>(a,0)
floattrue
Serialize<String>(a,test)
stringtrue
Serialize<Object>(a)
Object(具体根据选择决定)false
Serialize<Vector4>(a)
Vector4false
Serialize<Vector3>(a)
Vector3false
Serialize<Vector2>(a)
Vector2false
Serialize<Quaternion>(a)
Quaternionfalse
Serialize<Color>(a)
Colorfalse
Serialize<Boolean[]>(a,{1,2,3})
bool[]true
Serialize<Number[]>(a,{true,false})
float[]true
Serialize<String[]>(a,{aaa,bbb})
string[]true
Serialize<Object[]>(a)
Object[](具体根据选择决定)false
Serialize<Vector4[]>(a)
Vector4[]false
Serialize<Vector3[]>(a)
Vector3[]false
Serialize<Vector2[]>(a)
Vector2[]false
Serialize<Quaternion[]>(a)
Quaternion[]false
Serialize<Color[]>(a)
Color[]false

扩展功能

宏定义

示例代码
if UNITY_EDITOR then print("test") end


示例代码对应类型默认参数
UNITY_EDITOR编辑器模式
DEBUGDEBUG模式
UNITY_IOSIOS平台
UNITY_ANDROID安卓平台

协程的使用

注意使用
self:StartCoroutine
self:StopCoroutine
会随着对象的销毁自动停止,全局的则不能

co = self:StartCoroutine(function()
print("aaa")
WaitForSeconds(3)
print("bbb")
WaitForFixedUpdate()
print("ccc")
WaitForEndOfFrame()
print("ddd")
Yield(0)
print("eee")
end)

self:StopCoroutine(co)


AnimationEvent响应

在具有animtion的gameobject下挂载一个lua脚本,打开animtion面板,增加事件,在面板中Function选择
LuaAniEvent
,参数填入一个json字符串
{"type":"EventCanGotoStory","param":0}
其中

其中
type
字段为触发的lua中的方法名称。

其中
param
字段为触发时传给lua方法的参数。





在绑定的lua中添加下面方法,会在动画播放到关键帧时触发,不填则不触发

function Example:EventCanGotoStory(param)
print(param)
end


LuaStateMachine

LuaBehavoir
支持扩展组件,
PTLua.Globel.StateMachine
为状态机的扩展,调用过
self:addExtendComponent("PTLua.Globel.StateMachine"):exportMethods()
后自身将会具有状态机功能

self:addExtendComponent("PTLua.Globel.StateMachine"):exportMethods()
self:setupState({
initial = PageFiremanMain.States.Idle,

events = {
{name = PageFiremanMain.StateEvents.PlayMovie, from = PageFiremanMain.States.Idle, to = PageFiremanMain.States.Enter},
{name = PageFiremanMain.StateEvents.StartGame, from = PageFiremanMain.States.Enter, to = PageFiremanMain.States.PlayIdle},

{name = PageFiremanMain.StateEvents.PigJump, from = PageFiremanMain.States.PlayIdle, to = PageFiremanMain.States.PlayJump},
{name = PageFiremanMain.StateEvents.PigJumpFinish, from = PageFiremanMain.States.PlayJump, to = PageFiremanMain.States.PlayIdle},

{name = PageFiremanMain.StateEvents.PlayEndMovie, from = PageFiremanMain.States.PlayIdle, to = PageFiremanMain.States.Exit},
{name = PageFiremanMain.StateEvents.ShowResult, from = PageFiremanMain.States.Exit, to = PageFiremanMain.States.Result},
{name = PageFiremanMain.StateEvents.ResetGame, from = PageFiremanMain.States.Result, to = PageFiremanMain.States.Idle},
},

callbacks = {
onIdle = handler(self, self.onIdle),
onEnter = handler(self, self.onEnter),
onPlayIdle = handler(self, self.onPlay),
onPlayJump = handler(self, self.onPigJump),
onExit = handler(self, self.onExit),
onResult = handler(self, self.onResult),
}
})


AnimatorStateMachine

C#中原生支持在Animtor的动画状态机中对不同状态挂载脚本。如果需要触发Lua脚本,则要不同的使用方式。

在状态机中挂载
LuaAnimatorStateMachineComponent
这个C#组件。


在挂载使用该AnimtionController的Animtor的gameobject下挂载LuaComponent脚本


修改LuaComponent挂载的lua脚本,调用
self:addExtendComponent("PTLua.Globel.AnimatorStateMachine"):exportMethods()
扩展组件,使其支持该功能。

如果挂载有多个不同的LuaComponent响应,可以对每个不同的State下的
LuaAnimatorStateMachineComponent
黑色框内拖入对应的Lua类的文件。如果不拖入,那么该状态会影响所有挂载的扩展过的Lua脚本。

function Example:ctor()
Example.super.ctor(self)
self.name = "Example"

self:addExtendComponent("PTLua.Globel.AnimatorStateMachine"):exportMethods()
end

function Example:OnStateEnter(animator,stateInfo,layerIndex)
print("OnStateEnter")
end
function Example:OnStateUpdate(animator,stateInfo,layerIndex)
print("OnStateUpdate")
end
function Example:OnStateExit(animator,stateInfo,layerIndex)
print("OnStateExit")
end
function Example:OnStateMove(animator,stateInfo,layerIndex)
print("OnStateMove")
end
functi
9ac2
on Example:OnStateIK(animator,stateInfo,layerIndex)
print("OnStateIK")
end


扩展事件调用

Unity的MonoBehavior中拥有一些扩展的方法触发,如触发器和应用触发。这些方法默认不打开支持,如果使用需要在扩展支持折叠框里的选项页中勾选(为了避免不必要的C#对Lua层的访问)对应的支持,并在lua中添加对应C#的同名方法。支持和方法对照表如下:



示例代码对应支持选项
OnTriggerEnter(collider)Trigger
OnTriggerExit(collider)Trigger
OnTriggerStay(collider)Trigger
OnCollisionEnter(collisionInfo)Collision
OnCollisionExit(collisionInfo)Collision
OnCollisionStay(collisionInfo)Collision
OnApplicationPause(pauseStatus)Application
OnApplicationFocus(hasFocus)Application
OnApplicationQuit()Application
未完待续。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: