跳至正文
版本:最新版 - a1.156.0 ⚖️

🔊 Sound

用于播放游戏内 2D 和 3D 声音的类。

Client Only🔁Not Distributable🧑‍💻API Source
👪Inheritance
This class shares methods and events from Base Entity, Base Actor.

提示

你还可以直接从磁盘加载原始的 .ogg 文件!请查阅 SpecialPath。

🎒 示例​

Client/Index.lua
local my_sound = Sound(
Vector(-510, 145, 63), -- 位置(如果是 3D 音效)
"nanos-world::A_VR_Confirm", -- 资产路径
false, -- 是否为 2D 音效
true, -- 自动销毁(播放完毕后是否销毁)
SoundType.SFX,
1, -- 音量
1 -- 音调
)
Client/Index.lua
-- 生成位于包中的 ogg 音效
local ogg_sound = Sound(Vector(), "package://my-package/Client/awesome_beep.ogg", true)

🛠 构造函数​

Default Constructor​

No description provided

local my_sound = Sound(location, asset, is_2d_sound?, auto_destroy?, sound_type?, volume?, pitch?, inner_radius?, falloff_distance?, attenuation_function?, keep_playing_when_silent?, loop_mode?, auto_play?)

Parameters

TypeNameDefaultDescription
Vectorlocation Required parameter 仅在 3D 声音时使用
Sound Referenceasset Required parameter 要加载的声音资产
booleanis_2d_soundfalse是否生成为 2D 或 3D 声音
booleanauto_destroytrue播放完成后是否销毁
SoundTypesound_typeSoundType.SFX用于应用用户的音量设置
floatvolume1音量倍率
floatpitch1音高倍率
floatinner_radius400仅在 3D 声音时使用
floatfalloff_distance3600仅在 3D 声音时使用
AttenuationFunctionattenuation_functionAttenuationFunction.Linear仅在 3D 声音时使用
booleankeep_playing_when_silentfalse仅在 3D 声音时使用 - 请谨慎使用,这可能会导致性能问题!
SoundLoopModeloop_modeSoundLoopMode.Default是否应强制声音循环播放
booleanauto_playtrue加载后是否立即播放
备注

auto_destroy 意味着实体在生成后会立即被销毁,失去对游戏内生成的 Sound 的引用。因此,如果 Sound 本身无限循环,它将持续播放直到玩家重新连接。

🗿 静态函数​

Inherited Entity Static Functions
Sound inherits from Base Entity Class, sharing it's methods and functions:
Base Entityscripting-reference/classes/base-classes/entity
ReturnsNameDescription
table of Base EntityGetAll返回一个包含调用此方法的类中所有实体的表
Base EntityGetByIndex返回该类中指定索引处的特定实体
integerGetCount返回该类当前存在的实体数量
table of tableGetInheritedClasses获取使用 继承系统 创建的、直接继承自该类的所有子类列表
iteratorGetPairs返回一个包含该类所有实体的迭代器,用于 pairs() 循环
table or nilGetParentClass如果该类是使用 继承系统 创建的,则获取其父类
tableInherit使用 继承系统 继承此类
booleanIsChildOf如果该类是使用 继承系统 创建的,检查此类是否为另一个类的子类
functionSubscribe为该类的所有实体订阅一个 事件
functionSubscribeRemote订阅从服务器调用的自定义远程事件
Unsubscribe退订此包内该类中此 事件 的所有回调,或者仅取消传入的特定回调

This class doesn't have own static functions.

🦠 函数​

Inherited Entity Functions
Sound inherits from Base Entity Class, sharing it's methods and functions:
Base Entityscripting-reference/classes/base-classes/entity
ReturnsNameDescription
BroadcastRemoteEvent直接在此实体上向所有玩家调用自定义远程事件
BroadcastRemoteInRadiusEvent直接针对此实体调用自定义远程事件,并将该事件传递给半径范围内的所有玩家
CallRemoteEvent直接在此实体上向特定玩家调用自定义远程事件
CallRemoteEvent直接在此实体上调用自定义远程事件
CallRemotePlayersEvent直接在此实体上调用自定义远程事件,并将事件传递给玩家列表
Destroy销毁该实体
FinishSpawn如果此实体是以延迟 spawn_mode 生成的,则完成生成过程并将该实体发送至客户端
table of stringGetAllValuesKeys获取所有存储值的键名列表
tableGetClass获取该实体的类
integerGetID获取该实体的全网通用网络 ID(在客户端和服务器上相同)
anyGetValue获取此实体上存储在给定键处的 值
booleanHasAuthority获取本地上下文是否对此实体拥有主控(如果由客户端生成则为 true,由服务器生成则为 false)
booleanIsA递归检查此实体是否继承自某个类
booleanIsBeingDestroyed如果该实体正在被销毁,则返回 true
booleanIsSpawned获取此实体是否已完成生成
booleanIsValid如果该实体有效(即未被销毁且指向一个有效的实体),则返回 true
SetSpawnMode从继承类的构造函数中覆盖此实体完成生成的时间点
SetValue在此实体中设置一个值
functionSubscribe在这个特定实体上订阅一个事件
functionSubscribeRemote在这个特定实体上订阅一个从服务器调用的自定义远程事件
Unsubscribe退订此包内该实体中此 事件 的所有回调,或者仅取消传入的特定回调
Inherited Actor Functions
Sound inherits from Base Actor Class, sharing it's methods and functions:
Base Actorscripting-reference/classes/base-classes/actor
ReturnsNameDescription
AddActorTag向该 Actor 添加一个虚幻 Actor 标签
AddAngularImpulse向该 Actor 添加角冲量。适用于单次瞬间爆发
AddImpulse向该 Actor 添加冲量。适用于单次瞬间爆发
booleanAttachTo将该 Actor 附加到任何其他 Actor 上,可选择附加在特定骨骼处
Detach将该 Actor 从其附加的 Actor 上分离
table of stringGetActorTags获取该 Actor 上的所有虚幻 Actor 标签
VectorGetAngularForce获取该 Actor 的角外力(由 SetAngularForce() 设置)
floatGetAngularVelocity返回此 Actor 的角速度
table of Base ActorGetAttachedEntities获取附加到该 Actor 的所有 Actor
Base Actor or nilGetAttachedTo获取该 Actor 所附加到的父级 Actor
stringGetAttachedToSocket获取此 Actor 所附加到的骨骼或插槽名称
tableGetBounds获取此 Actor 的包围盒,便于计算其在世界中的尺寸
CollisionTypeGetCollision获取该 Actor 的碰撞类型
integerGetDimension获取该 Actor 的维度
floatGetDistanceFromCamera获取该 Actor 距离相机的距离
VectorGetForce获取该 Actor 的持续外力(由 SetForce() 设置)
floatGetLifeSpan获取此 Actor 被销毁前剩余的时间(秒)
VectorGetLocation获取该 Actor 在游戏世界中的位置
floatGetMass返回此 Actor 的质量
Player or nilGetNetworkAuthority获取该 Actor 的网络主控玩家
VectorGetRelativeLocation如果该 Actor 已附加,获取其相对位置
RotatorGetRelativeRotation如果该 Actor 已附加,获取其相对旋转
RotatorGetRotation获取该 Actor 在游戏世界中的角度值
VectorGetScale获取该 Actor 的缩放比例
floatGetScreenPercentage获取该 Actor 在屏幕中所占尺寸的百分比
tableGetSocketTransform在给定骨骼或插槽名称的情况下,获取世界空间中的插槽变换
VectorGetVelocity获取该 Actor 当前的速度
booleanHasNetworkAuthority如果本地玩家当前是该 Actor 的网络主控(Network Authority),则返回 true
booleanIsGravityEnabled如果该 Actor 启用了重力,则返回 true
booleanIsInWater如果该 Actor 处于水中,则返回 true
booleanIsNetworkDistributed如果该 Actor 当前处于网络分配状态,则返回 true
booleanIsVisible如果该 Actor 可见,则返回 true
RemoveActorTag从该 Actor 中移除一个虚幻 Actor 标签
RotateTo在一定时间内平滑旋转该 Actor 到指定角度
SetAngularForce向该 Actor 添加一个永久的角外力,设为 Vector(0, 0, 0) 可以取消
SetCastShadow设置该 Actor 是否投射阴影
SetCollision设置该 Actor 的碰撞类型
SetDimension设置此 Actor 的维度。Actor 仅能看见并与处于同一维度中的实体进行交互
SetDistanceOptimizationMultiplier设置此 Actor 的距离优化系数
SetForce向该 Actor 添加一个永久的外力,设为 Vector(0, 0, 0) 可以取消
SetGravityEnabled设置该 Actor 是否启用重力
SetHighlightEnabled设置该 Actor 是否启用高亮显示,以及使用哪个高亮索引值
SetLifeSpan设置该 Actor 被销毁前的生存时间(秒)。经过这段时间后,Actor 将被自动销毁。
SetLocation设置该 Actor 在游戏世界中的位置
SetNetworkAuthority设置某个玩家拥有此 Actor 的网络主控
SetNetworkAuthorityAutoDistributed设置该 Actor 是否在玩家之间自动分配网络主控
SetOutlineEnabled设置该 Actor 是否启用描边,以及使用哪个描边索引值
SetRelativeLocation在局部空间中设置该 Actor 的相对位置(仅在此 Actor 已附加时生效)
SetRelativeRotation在局部空间中设置该 Actor 的相对旋转(仅在此 Actor 已附加时生效)
SetRenderCullDistance设置该 Actor 的渲染剔除距离
SetRotation设置该 Actor 在游戏世界中的旋转
SetScale设置该 Actor 的缩放比例
SetVisibility设置该 Actor 是否可见
TranslateTo在一定时间内平滑地将该 Actor 移动到某个位置
booleanWasRecentlyRendered获取该 Actor 最近是否在屏幕上被渲染过
ReturnsNameDescription
FadeIn使用淡入效果播放声音
FadeOut使用淡出效果停止声音
floatGetDuration获取声音的时长
floatGetFalloffDistance获取声音衰减至完全听不见的距离
floatGetInnerRadius获取音量维持 100% 的内半径
floatGetLowPassFilter获取低通滤波器频率
stringGetPath获取此声音的资产路径
floatGetPitch获取音高倍率
SoundTypeGetSoundType获取声音类型,用于应用用户的音量设置
floatGetVolume获取音量倍率
booleanIs2D获取声音是否为 2D 声音
booleanIsPlaying获取声音是否正在播放
Play开始播放声音
SetFalloffDistance如果是 3D 声音,设置声音变得听不到的距离
SetInnerRadius如果是 3D 音效,设置音量维持 100% 的内半径
SetLowPassFilter设置低通滤波器频率。设为 0 以禁用它。
SetPaused暂停或恢复播放声音
SetPitch设置声音的音调
SetVolume设置声音的音量
Stop停止声音
StopDelayed在指定的延迟时间后停止声音

Optimal

FadeIn​

使用淡入效果播放声音
my_sound:FadeIn(fade_in_duration, fade_volume_level?, start_time?)

Parameters

TypeParameterDefaultDescription
floatfade_in_duration Required parameter 淡入所需的秒数
floatfade_volume_level?1.0
淡入目标音量倍率
floatstart_time?0.0
从声音的第几秒开始播放

Optimal

FadeOut​

使用淡出效果停止声音
my_sound:FadeOut(fade_out_duration, fade_volume_level?, destroy_after_fadeout?)

Parameters

TypeParameterDefaultDescription
floatfade_out_duration Required parameter 淡出所需的秒数
floatfade_volume_level?0.0
淡入目标音量倍率
booleandestroy_after_fadeout?false
淡出结束后是否销毁 Sound

Optimal

GetDuration​

获取声音的时长
local duration = my_sound:GetDuration()

Returns

TypeDescription
float声音的持续时长

Optimal

GetFalloffDistance​

获取声音衰减至完全听不见的距离
local falloff_distance = my_sound:GetFalloffDistance()

Returns

TypeDescription
float衰减距离

See also SetFalloffDistance.


Optimal

GetInnerRadius​

获取音量维持 100% 的内半径
local inner_radius = my_sound:GetInnerRadius()

Returns

TypeDescription
float内半径

See also SetInnerRadius.


Optimal

GetLowPassFilter​

获取低通滤波器频率
local frequency = my_sound:GetLowPassFilter()

Returns

TypeDescription
float低通滤波器频率

See also SetLowPassFilter.


Optimal

GetPath​

获取此声音的资产路径
local path = my_sound:GetPath()

Returns

TypeDescription
string资产路径

Optimal

GetPitch​

获取音高倍率
local pitch = my_sound:GetPitch()

Returns

TypeDescription
float音高倍率

See also SetPitch.


Optimal

GetSoundType​

获取声音类型,用于应用用户的音量设置
local sound_type = my_sound:GetSoundType()

Returns

TypeDescription
SoundType声音类型

Optimal

GetVolume​

获取音量倍率
local volume = my_sound:GetVolume()

Returns

TypeDescription
float音量倍率

See also SetVolume.


Optimal

Is2D​

获取声音是否为 2D 声音
local is_2d_sound = my_sound:Is2D()

Returns

TypeDescription
boolean该声音是否为 2D 音效

Optimal

IsPlaying​

获取声音是否正在播放
local is_playing = my_sound:IsPlaying()

Returns

TypeDescription
boolean声音是否正在播放

Optimal

Play​

开始播放声音
my_sound:Play(start_time?)

Parameters

TypeParameterDefaultDescription
floatstart_time?0.0
从声音的第几秒开始播放

Optimal

SetFalloffDistance​

如果是 3D 声音,设置声音变得听不到的距离
my_sound:SetFalloffDistance(falloff_distance)

Parameters

TypeParameterDefaultDescription
floatfalloff_distance Required parameter 从内半径起算,声音衰减至完全听不见的距离

See also GetFalloffDistance.


Optimal

SetInnerRadius​

如果是 3D 音效,设置音量维持 100% 的内半径
my_sound:SetInnerRadius(inner_radius)

Parameters

TypeParameterDefaultDescription
floatinner_radius Required parameter 音量维持 100% 的半径

See also GetInnerRadius.


Optimal

SetLowPassFilter​

设置低通滤波器频率。设为 0 以禁用它。
my_sound:SetLowPassFilter(frequency)

Parameters

TypeParameterDefaultDescription
floatfrequency Required parameter 新的低通滤波器频率。设为 0 可禁用

See also GetLowPassFilter.


Optimal

SetPaused​

暂停或恢复播放声音
my_sound:SetPaused(pause?)

Parameters

TypeParameterDefaultDescription
booleanpause?true
是暂停 (true) 还是恢复 (false)

Optimal

SetPitch​

设置声音的音调
my_sound:SetPitch(new_pitch)

Parameters

TypeParameterDefaultDescription
floatnew_pitch Required parameter 声音的新音高

See also GetPitch.


Optimal

SetVolume​

设置声音的音量
my_sound:SetVolume(new_volume)

Parameters

TypeParameterDefaultDescription
floatnew_volume Required parameter 音量倍率,1 为原始音量

See also GetVolume.


Optimal

Stop​

停止声音
my_sound:Stop()

Optimal

StopDelayed​

在指定的延迟时间后停止声音
my_sound:StopDelayed(delay)

Parameters

TypeParameterDefaultDescription
floatdelay Required parameter 停止前等待的秒数

🚀 事件​

Inherited Entity Events
Sound inherits from Base Entity Class, sharing it's events:
Base Entityscripting-reference/classes/base-classes/entity
NameDescription
ClassRegister当使用 继承系统 注册一个新类时触发
Destroy当实体被销毁时触发
Spawn当实体被生成/创建时触发
ValueChange当实体通过 :SetValue() 改变了值时触发
Inherited Actor Events
Sound inherits from Base Actor Class, sharing it's events:
Base Actorscripting-reference/classes/base-classes/actor
NameDescription
DimensionChange当 Actor 改变其维度时触发
EnterWater当 Actor 进入水体时触发
LeaveWater当 Actor 离开水体时触发
NetworkAuthorityChange当 Actor 更改其网络主控玩家时触发
NetworkAuthorityChange当本地玩家获得或失去对此 Actor 的网络主控时触发

This class doesn't have own events.

🔊 Sound Attenuation(声音衰减)​

该属性定义了决定声音随距离衰减速率的函数。有多种不同的函数可供选择:

🔈 Linear(线性)​

这是默认函数。使用此函数时,音量将线性衰减,因此当听者靠近或远离声源时,音量的变化是恒定的。该函数适用于大型背景类声音之间的交叉淡入淡出,不需要高度聚焦的 3D 空间衰减设置。

🔈 Logarithmic(对数)​

使用此函数时,声音衰减表现为,在近距离时音量变化较大,而在远距离时变化较小。该函数适用于需要良好 3D 定位感的点声源,同时在合理距离内仍清晰听见。

🔈 Inverse(反转)​

使用此函数时,音量的变化类似于对数曲线,但更加夸张。该函数适用于只需要在远距离勉强听见,但当听者非常靠近声源时音量会显著增大的声音。

🔈 Log Reverse(反向对数)​

使用此函数时,近距离时的音量变化较小,而在远距离时变化更加剧烈。该函数适用于需要在较大距离范围内保持高音量的声音。

🔈 Natural Sound(自然声音)​

该函数尝试模拟更“自然”的衰减行为,以产生更符合现实情况的效果。该函数介于对数(Logarithmic)与反比(Inverse)函数之间。