Unity 实现 Lua 脚本组件的挂载

3 篇文章 0 订阅

Unity 实现 Lua 脚本组件的挂载

为了让 Lua 开发效率更高,实现 Lua 脚本挂载到 Unity 游戏对象上,既满足了 Lua 的热更需要,同时也达到了 C# 组件的方便

先来看一下最终效果
在这里插入图片描述
lua 脚本如下

---@class ComLuaTest:LuaMonoBehaviour @by wx771720@outlook.com 2022-02-23 20:09:51
---
---@field name string @[export string]
---@field intValue number @[export int]
---@field floatValue number @[export float]
---@field intValueLimited number @[export int range(-10,10)]
---@field floatValueLimited number @[export float range(1,100)]
---@field boolValue boolean @[export bool]
---@field txtValue Text @[export Text]
---@field v2Value Vector2 @[export Vector2]
---@field v3Value Vector3 @[export Vector3]
---@field v4Value Vector4 @[export Vector4]
---
---@field names string[] @[export string repeat]
---@field intValues number[] @[export int repeat]
---@field floatValues number[] @[export float repeat]
---@field objectValues GameObject[] @[export GameObject repeat]
---@field v2Values Vector2[] @[export Vector2 repeat]
---@field v3Values Vector3[] @[export Vector3 repeat]
---@field v4Values Vector4[] @[export Vector4 repeat]

local ComLuaTest = xx.Class("ComLuaTest")
---构造函数
function ComLuaTest:ctor(gameObject) end

function ComLuaTest:onCSAwake() end
function ComLuaTest:onCSEnable() end
function ComLuaTest:onCSStart() end
function ComLuaTest:onCSDisable() end
function ComLuaTest:onCSDestroy() end

return ComLuaTest

声明格式

---@field Name LuaType @[export CSType repeat range(min,max)]

不区分大小写

  • Name : 属性名
  • LuaType : EmmyLua 定义的数据类型,比如 number, boolean, string 等
  • [export xxx] : 固定格式,表示声明将该属性显示到 Unity 组件中
  • CSType : 支持显示的数据类型,详细见下面说明
  • repeat : 指定了该属性为数组
  • range : 只能用在 int + float,限制数据范围

支持类型

基础类型

  • bool : 布尔值
  • int : 整数
  • float : 小数
  • string : 字符串

Unity 类型

  • Object
  • Vector2
  • Vector3
  • Vector4
  • Texture
  • Sprite
  • GameObject
  • Transform
  • RectTransform
  • RawImage
  • Image
  • Text
  • InputField
  • Button
  • ScrollRect
  • Slider
  • Animator

自定义类型

  • GIF
  • Scaler
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wx771720

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值