主题
SoundUnit
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(5 属性 / 28 函数 / 6 事件)
- [SoundUnit](9 属性 / 8 函数 / 3 事件)
继承成员
1 个来源 / 5 属性 / 28 函数 / 6 事件
- 来自 Unit(5 属性 / 28 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent、Tags - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId、HasTag、AddTag、RemoveTag - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
SoundUnit 是负责在场景中播放音频的声效单位,用 SoundId 指定音频资源,并可通过 Volume、Speed、Looped、Duration、FadeDistance、CampRoleId 等属性控制音量、播放速率、循环方式、存活时长与距离衰减。它提供 Play、Stop 两个播放控制函数,以及 SetVolume、SetSpeed、SetVolumeSpeed、SetFadeDistance、SetDuration、SetPosition 等调节函数,并借助 Played、Stopped、Ended 事件对外反馈播放状态变化。
适用场景
用于播放一次性音效或循环背景音。3D 音效可以通过 Parent 跟随 BasePart 派生单位或 Attachment;没有跟随父节点时,用 SetPosition 设置发声位置。
使用要点
通过 game:CreateUnit 或 World:CreateUnit 创建 SoundUnit,设置 SoundId,并在 Play 前连接所需事件。需要跟随时设置 Parent;固定位置音效使用 SetPosition。Duration 控制单位的存活倒计时,不表示音频文件时长。
注意事项
先设置有效的 SoundId,再调用 Play;自动播放可使用 Playing。音量和播放速率分别由 Volume、Speed 控制。Parent 为 BasePart 派生单位或 Attachment 时,3D 音效自动跟随父节点,此时 SetPosition 不生效;SoundGroup 父节点只控制音组音量,不提供位置跟随。Duration 大于 0 时立即开始存活倒计时,与是否正在播放无关,到期销毁整个 SoundUnit;-1 表示不计时,仍需按用途清理。Played 表示开始播放,Stopped 表示停止播放;Ended 只表示非循环音效自然播放结束,手动 Stop 和单位销毁不触发 Ended。事件订阅应在 Play 或 Stop 前建立。
代码示例
创建音效并监听播放状态
lua
-- @runtime client
local World = game:GetService("World")
local soundUnit = World:CreateUnit("SoundUnit", {
SoundId = "official://audio/5147",
Looped = false,
Volume = 80,
})
soundUnit.Played:Once(function()
print("音效开始播放")
end)
soundUnit.Stopped:Once(function()
print("音效停止播放")
end)
soundUnit:Play()属性 (9)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
SoundId | String | - | 音效资源的 URI。 |
SoundType | String | 3D | 音效类型,如 2D 或 3D。 |
Speed | Float | 1.0 | 音效的播放速率。 |
Volume | Float | 100 | 音效的音量大小。 |
Duration | Float | -1 | 单位存活倒计时,单位为秒;大于 0 时从设置时刻开始计时,到期销毁整个 SoundUnit,与音频是否播放无关。默认 -1 表示不计时,不是音频文件时长。 |
CampRoleId | Int | -1 | 音效所属的阵营 ID。 |
FadeDistance | Float | -1 | 3D 音效的衰减距离。 |
Playing | Bool | false | 是否在创建后自动开始播放。 |
Looped | Bool | false | 是否循环播放音效。 |
关联类型
事件 (3)
Played
签名:Played() 触发:local
当 SoundUnit 开始播放时触发此事件。
⚙ 播放开始时触发;该事件不表示音频已经播放完成。
示例代码
监听播放开始
lua
-- @runtime client
local World = game:GetService("World")
local soundUnit = World:CreateUnit("SoundUnit", { SoundId = "official://audio/5147" })
soundUnit.Played:Once(function()
print("音效开始播放")
end)
soundUnit:Play()Stopped
签名:Stopped() 触发:local
当 SoundUnit 停止播放时触发此事件。
⚙ 播放停止时触发。若要捕获主动停止,应在调用 Stop 前建立订阅。
示例代码
监听播放停止
lua
-- @runtime client
local World = game:GetService("World")
local soundUnit = World:CreateUnit("SoundUnit", { SoundId = "official://audio/5147" })
soundUnit.Stopped:Once(function()
print("音效停止播放")
end)
soundUnit:Play()
soundUnit:Stop()Ended
签名:Ended() 触发:local
Ended 是 SoundUnit 提供的本地事件,在音效播放结束时触发。该事件不携带任何回调参数,作者可以为它注册回调,用于在音效播放完毕后执行后续处理。
示例代码
监听 Ended 事件处理非循环音效播放结束
lua
-- @runtime client
-- 获取 World 服务,用于创建音效实例
local world = game:GetService("World")
-- 创建非循环音效,资源自然播放完毕后会触发 Ended
local sound = world:CreateUnit("SoundUnit", {
SoundId = "official://audio/10001",
Looped = false,
Volume = 60,
})
-- 先订阅 Ended 事件再播放,避免播放结束信号被错过
sound.Ended:Once(function()
print("音效自然播放结束,Ended 事件已触发")
end)
-- 开始播放并确认播放状态
sound:Play()
print("音效已开始播放,Playing =", sound.Playing)函数 (8)
SetVolumeSpeed
签名:SetVolumeSpeed(Volume: Int, Speed: Float) -> void
设置 SoundUnit 的音量和播放速率。
⚙ 音量参数范围为 0–100;速率参数范围为 0–1,1 表示正常速度。
| 参数 | 类型 | 说明 |
|---|---|---|
Volume | Int | 音量 |
Speed | Float | 速率 |
返回值 void
示例代码
同时设置音量和播放速率
lua
-- @runtime client
local World = game:GetService('World')
local soundUnit = World:CreateUnit('SoundUnit', { SoundId = 'official://audio/5147', Looped = false })
soundUnit:SetVolumeSpeed(70, 0.8)
soundUnit:Play()SetSpeed
签名:SetSpeed(Speed: Float) -> void
设置当前 SoundUnit 实例的播放速率。
⚙ 速率参数范围为 0–1,1 表示正常速度。
| 参数 | 类型 | 说明 |
|---|---|---|
Speed | Float | 速率 |
返回值 void
示例代码
设置正常播放速度
lua
-- @runtime client
local World = game:GetService('World')
local soundUnit = World:CreateUnit('SoundUnit', { SoundId = 'official://audio/5147', Looped = false })
soundUnit:SetSpeed(1.0)
soundUnit:Play()SetVolume
签名:SetVolume(Volume: Int) -> void
设置 SoundUnit 实例的播放音量。
| 参数 | 类型 | 说明 |
|---|---|---|
Volume | Int | 音量 |
返回值 void
示例代码
设置音效音量
lua
-- @runtime client
-- 获取 World 服务用于创建 SoundUnit
local world = game:GetService("World")
-- 创建一个 SoundUnit 实例
local soundUnit = world:CreateUnit("SoundUnit", {
SoundId = "official://audio/5147"
})
-- 设置音量为 80(范围 0-100)
soundUnit:SetVolume(80)
-- 播放音效
soundUnit:Play()
print("音效音量已设置为 80")SetFadeDistance
签名:SetFadeDistance(FadeDistance: Float) -> void
设置 3D 声音的衰减距离,距离越远声音衰减越明显。
| 参数 | 类型 | 说明 |
|---|---|---|
FadeDistance | Float | 3D 音效的衰减距离。 |
返回值 void
示例代码
设置 3D 音效衰减距离
lua
-- @runtime client
local World = game:GetService('World')
local soundUnit = World:CreateUnit('SoundUnit', {
SoundId = 'official://audio/5147',
SoundType = '3D',
})
soundUnit:SetFadeDistance(50.0)
soundUnit:SetPosition(Vector3.New(0, 0, 0))
soundUnit:Play()SetDuration
签名:SetDuration(duration: Float) -> void
从当前时刻重新设置 SoundUnit 的存活倒计时,到期销毁整个音效单位;与音频是否播放无关。
⚙ duration 单位为秒;大于 0 时开始计时,-1 表示不计时。停止播放不会取消单位的存活倒计时。
| 参数 | 类型 | 说明 |
|---|---|---|
duration | Float | 存活时长(秒,-1 永久) |
返回值 void
示例代码
用 SetDuration 设置音效存活时长
lua
-- @runtime client
-- 获取 World 服务,用于创建音效实例
local world = game:GetService("World")
-- 创建一个循环音效,后续通过 SetDuration 控制它的存活时长
local sound = world:CreateUnit("SoundUnit", {
SoundId = "official://audio/10001",
Looped = true,
Volume = 40,
})
-- 设置播放速率与音量,保证音效参数符合预期
sound:SetSpeed(1)
sound:SetVolume(40)
-- 设置 5 秒存活时长,到期后该音效自动结束
sound:SetDuration(5)
sound:Play()
print("音效开始播放,存活时长 =", sound.Duration)SetPosition
签名:SetPosition(position: Vector3) -> void
SetPosition 用于设置 SoundUnit 在三维空间中的位置,接收一个 Vector3 作为参数,调用后不返回任何结果。
| 参数 | 类型 | 说明 |
|---|---|---|
position | Vector3 | 位置 |
返回值 void
示例代码
设置 3D 音效位置
lua
-- @runtime client
local World = game:GetService('World')
local soundUnit = World:CreateUnit('SoundUnit', {
SoundId = 'official://audio/5147',
SoundType = '3D',
})
soundUnit:SetPosition(Vector3.New(10, 5, 0))
soundUnit:Play()Stop
签名:Stop() -> void
停止当前正在播放的音效。
返回值 void
示例代码
停止播放音效
lua
-- @runtime client
-- 创建 SoundUnit 并设置音频资源
local world = game:GetService("World")
local soundUnit = world:CreateUnit("SoundUnit", {
SoundId = "official://audio/10001",
Looped = false
})
-- 先播放音效
soundUnit:Play()
-- 停止播放
soundUnit:Stop()
print("音效已停止")Play
签名:Play() -> void
开始播放 SoundUnit 关联的音效。
返回值 void
示例代码
播放音效
lua
-- @runtime client
-- 创建 SoundUnit 并设置音频资源
local world = game:GetService("World")
local soundUnit = world:CreateUnit("SoundUnit", {
SoundId = "official://audio/10001",
Looped = false
})
-- 调用 Play 开始播放
soundUnit:Play()
print("音效已开始播放")