Skip to content

EffectService ​

概览 ​

字段值
KindService · 服务 Service
Realmcommon

继承关系 ​

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

> Service 继承 Unit 仅表示运行时类型关系;作者可用 API 以当前 Service 页面显式列出的成员为准,不自动包含 Unit 的创建、层级或销毁能力。

面向客户端本地特效播放的便捷服务,核心能力是 PlayLocalEffect,可一次性提交特效资源标识、位置、旋转与缩放,并按需指定持续时长、是否循环与是否异步加载。特效资源标识有严格格式约束,只接受 official://effect/{asset_id} 与 custom://{asset_id} 两种写法,其它形式的标识不会被识别。

适用场景 ​

适合在客户端即时呈现命中、爆炸、治疗等视觉反馈,而不必经由远端事件广播;若由服务端统一派发,通常先触发远端事件,客户端收到通知后再调用该入口在本地播放。

使用要点 ​

先通过 game:GetService("EffectService") 取得服务实例,并在调用前判断返回值是否为空。确认可用后调用 PlayLocalEffect,依次传入特效资源标识、位置、旋转、缩放,再按需补齐 keepTime、loop 与 isAsync 参数。资源标识需写成 official://effect/{asset_id} 或 custom://{asset_id},不要传裸数字或裸路径。

注意事项 ​

最新 Meta 已公开 EffectService,但 2026-08-12 当前编辑器运行包中 game:GetService("EffectService") 仍返回 nil;这是运行时服务导出尚未对齐 Meta。示例会先检查服务是否可用,服务未导出时安全退出。服务可用后,PlayLocalEffect 的 effectId 只接受 official://effect/{path} 或 custom://{asset_id},不接受裸数字、裸路径、official://preset/... 或其他 scheme。返回的 EffectHandle 当前只作为引用,公开契约没有 Stop、Destroy 或属性控制方法;不要编造句柄控制 API。

代码示例 ​

播放本地特效 ​

lua
-- @runtime client
local effectService = game:GetService('EffectService')
if effectService == nil then
    print('EffectService 当前运行包尚未导出')
    return
end
local effectId = 'official://effect/2039'
local position = Vector3.New(0, 3, 0)
local rotation = Quaternion.Identity()
local scale = Vector3.New(1, 1, 1)
local handle = effectService:PlayLocalEffect(effectId, position, rotation, scale, 2.0, false, false)
print('特效句柄:', handle)

关联类型 ​

函数 (1) ​

PlayLocalEffect ​

签名:PlayLocalEffect(effectId: String, position: Vector3, rotation: Quaternion, scale: Vector3, keepTime?: Float, loop?: Bool, isAsync?: Bool) -> Any (EffectHandle(特效句柄引用))

在本地播放一个特效实例,并把特效摆放到指定的位置、朝向与缩放上。effectId 为字符串形式的特效资源标识;position 为 Vector3 类型的世界坐标;rotation 为 Quaternion 类型的旋转;scale 为 Vector3 类型的缩放;keepTime 为可选的特效保留时长;loop 为可选的循环开关;isAsync 为可选的异步加载开关。调用后返回一个特效句柄引用,可用于后续对这次播放的特效进行引用操作。

⚙ 该接口仅在客户端生效;调用前需确认当前运行包已导出 EffectService。effectId 必须使用 official://effect/{path} 或 custom://{asset_id}。

参数类型说明
effectIdString特效资源 URI(仅 official://effect/{asset_id} 或 custom://{asset_id})
positionVector3世界坐标位置(默认 0,0,0)
rotationQuaternion旋转(默认单位四元数)
scaleVector3缩放(默认 1,1,1)
keepTime?Float自动销毁延时秒(默认 -1 不自动销毁)
loop?Bool是否循环(默认 false)
isAsync?Bool是否异步加载(默认 false 同步,true 时不阻塞主线程)

返回值 Any (EffectHandle(特效句柄引用))

示例代码

播放本地特效

lua
-- @runtime client
local effectService = game:GetService('EffectService')
if effectService == nil then
    print('EffectService 当前运行包尚未导出')
    return
end
local handle = effectService:PlayLocalEffect(
    'official://effect/2039',
    Vector3.New(0, 3, 0),
    Quaternion.Identity(),
    Vector3.New(1, 1, 1),
    2.0,
    false,
    false
)
print('特效句柄:', handle)