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

自定义的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组件脚本的挂载

  1. 如果需要将一个lua文件挂载到对象上请先挂在C#组件 LuaComponent,如图所示这里写图片描述黑色区域显示红字,代表当前组件尚未挂载lua组件类。此时必须拖动创建好的lua类文件到黑色框内。黑色区域显示青色类名,则代表已经绑定了Lua组件类。

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

  1. 手动添加绑定:点击上图中六个添加按钮分别可以添加不同属性这里写图片描述注意事项:
    • 值类型包括int,float,string,bool四种。
    • 对象类型可以拖入Gameobject,或材质贴图等UnityEngine.Object类型对象
    • 集合类型可以使用Vector4,Vector3,Vector2,Color,Quaternion五种类型
    • 每个类型的第一个输入框为属性名称,如其中valueInt可以在lua类中直接通过self.valueInt来访问
    • 数组类型在lua中都是索引从1开始的lua的数组,如#self.arr_object=3
    • 点击全部清除可以删除所有
    • 如果挂载的是Gameobject,可以从下拉菜单中选择挂载对象所挂载的其他组件如TransformMeshRender等,包括Lua组件。其中lua组件可以精确到哪一个lua类的组件。如果是C#组件,在Lua中获取到的是C#对象。如果是Lua组件,获取到的是Lua组件挂载的Lua类对象(通常是一个Table),可以直接访问其中的属性,调用其中的方法。
  2. 通过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:StartCoroutineself: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脚本,则要不同的使用方式。

  1. 在状态机中挂载LuaAnimatorStateMachineComponent这个C#组件。这里写图片描述
  2. 在挂载使用该AnimtionController的Animtor的gameobject下挂载LuaComponent脚本这里写图片描述
  3. 修改LuaComponent挂载的lua脚本,调用self:addExtendComponent("PTLua.Globel.AnimatorStateMachine"):exportMethods()扩展组件,使其支持该功能。
  4. 如果挂载有多个不同的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
function 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

未完待续。。。。。。。

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值