Skip to content

Tween ​

概览 ​

字段值
KindUnit · 单位 Unit
Realmcommon

继承关系 ​

  • Unit(5 属性 / 28 函数 / 6 事件)
    • [Tween](2 属性 / 3 函数 / 1 事件)

继承成员 ​

1 个来源 / 5 属性 / 28 函数 / 6 事件

补间动画对象,负责把目标对象的属性在一段时间内平滑过渡到指定目标值。它记录当前所处的播放阶段与所作用的实例,并提供播放、暂停、取消三个控制入口,在动画自然走完时抛出完成事件供调用方收尾。

适用场景 ​

需要让物体的位置、朝向或其他可过渡属性以缓动方式逐步逼近目标值时,先通过补间服务生成该对象,再由它驱动整段过渡过程,例如让部件平滑飞到指定坐标。

使用要点 ​

先用 game:GetService("TweenService") 取得补间服务,调用 Create 并传入目标实例、TweenInfo 以及属性目标表来获得补间对象;随后调用 tween:Play() 开始播放,中途可用 tween:Pause() 暂停、用 tween:Cancel() 中止并停止继续更新;随时读取 tween.PlaybackState 判断当前阶段,并连接 Completed 事件处理自然结束后的逻辑。

注意事项 ​

Tween 实例只能由 TweenService:Create() 返回,不可直接构造。Play() 开始播放,Pause() 暂停,Cancel() 取消播放并停止继续更新;不要把 Cancel() 描述为自然完成。需要获知自然结束时,监听 Completed 事件并读取回调中的 playbackState。

代码示例 ​

创建 Tween 并监听完成状态 ​

lua
-- @runtime client
local TweenService = game:GetService("TweenService")
local World = game:GetService("World")

local target = World:CreateUnit("RenderUnit", {
    Name = "FadeTarget",
    RenderMeshId = "official://mesh/59268",
    ModelAlpha = 1,
})

local tweenInfo = TweenInfo.New(0.5, Enums.EasingStyle.Quad, Enums.EasingDirection.InOut)
local tween = TweenService:Create(target, tweenInfo, {
    Position = Vector3.New(5, 0, 0),
})

tween.Completed:Once(function(playbackState)
    print("Tween 完成状态:", playbackState)
end)
tween:Play()

属性 (2) ​

Name类型默认值说明
PlaybackStateTweenPlayStateEnums.TweenPlayState.BeginTween 的 PlaybackState 属性表示该补间动画当前的播放状态,类型为 TweenPlayState 枚举,枚举项包括 Begin、Delayed、Playing、Paused、Completed、Cancelled。该属性为只读属性。
InstanceUnit-Tween 的 Instance 属性表示该补间动画所作用的目标对象,类型为 Unit。它是一个只读属性,用于在拿到一个 Tween 实例后回查这个补间动画绑定的是哪个对象。

关联类型 ​

事件 (1) ​

Completed ​

签名:Completed(playbackState: TweenPlayState) 触发:local

Tween 结束时触发的事件,回调参数 playbackState 表示结束状态。

⚙ 当前公开 Meta 只声明 Completed 事件和 Cancel 方法,未明确 Cancel() 是否一定触发 Completed;处理回调时可根据 playbackState 区分状态,但不要把 Cancel() 当作自然完成。

回调参数类型说明
playbackStateTweenPlayState结束时的播放状态

示例代码

监听 Tween 完成状态

lua
-- @runtime client
local TweenService = game:GetService("TweenService")
local World = game:GetService("World")
local target = World:CreateUnit("RenderUnit", { ModelAlpha = 1 })
local tweenInfo = TweenInfo.New(0.5, Enums.EasingStyle.Quad, Enums.EasingDirection.InOut)
local goal = { Position = Vector3.New(5, 0, 0) }
local tween = TweenService:Create(target, tweenInfo, goal)
tween.Completed:Once(function(playbackState)
    print("Tween 完成状态:", playbackState)
end)
tween:Play()

函数 (3) ​

Play ​

签名:Play() -> void

开始播放 Tween 动画,或从暂停状态恢复播放。

返回值 void

示例代码

播放 Tween

lua
-- @runtime client
local TweenService = game:GetService("TweenService")
local World = game:GetService("World")
local target = World:CreateUnit("RenderUnit", { ModelAlpha = 1 })
local tweenInfo = TweenInfo.New(0.5, Enums.EasingStyle.Quad, Enums.EasingDirection.InOut)
local goal = { Position = Vector3.New(5, 0, 0) }
local tween = TweenService:Create(target, tweenInfo, goal)
tween:Play()

Pause ​

签名:Pause() -> void

暂停当前正在播放的 Tween 动画,保留当前进度。

返回值 void

示例代码

暂停 Tween

lua
-- @runtime client
local TweenService = game:GetService("TweenService")
local World = game:GetService("World")
local target = World:CreateUnit("RenderUnit", { ModelAlpha = 1 })
local tweenInfo = TweenInfo.New(1.0, Enums.EasingStyle.Quad, Enums.EasingDirection.InOut)
local goal = { Position = Vector3.New(5, 0, 0) }
local tween = TweenService:Create(target, tweenInfo, goal)
tween:Play()
tween:Pause()

Cancel ​

签名:Cancel() -> void

取消正在播放的 Tween,使其停止继续更新;不要将取消播放等同于自然完成。

返回值 void

示例代码

取消 Tween

lua
-- @runtime client
local TweenService = game:GetService("TweenService")
local World = game:GetService("World")
local target = World:CreateUnit("RenderUnit", { ModelAlpha = 1 })
local tweenInfo = TweenInfo.New(1.0, Enums.EasingStyle.Quad, Enums.EasingDirection.InOut)
local goal = { Position = Vector3.New(5, 0, 0) }
local tween = TweenService:Create(target, tweenInfo, goal)
tween:Play()
tween:Cancel()