You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

重写AnimationPlayer函数无效,无法追踪时间轴求助

解决AnimationPlayer重写函数无打印输出的问题

问题原因

  1. 编辑器时间轴操作不触发自定义重写方法:在编辑器中拖动时间轴时,引擎直接调用底层C++逻辑,不会触发GDScript重写的seek方法。只有通过代码调用seek,或进入**编辑器运行模式(Play in Editor)**时,自定义方法才会生效。
  2. 编辑器预览操作绕过GDScript层:编辑器内的播放、拖动时间轴等预览行为属于内部操作,不走我们的tool脚本重写接口。

解决方案

方案1:用信号捕获seek事件(支持编辑器+运行时)

AnimationPlayer提供seek_finished信号,无论是代码调用seek还是编辑器拖动时间轴后松开,都会触发该信号,可用于追踪seek操作:

tool
extends AnimationPlayer

func _ready():
    connect("seek_finished", self, "_on_seek_finished")
    # 可选:监听动画启动事件
    connect("animation_started", self, "_on_animation_started")

func _on_seek_finished(seconds: float):
    print("seek完成,当前时间:", seconds)

func _on_animation_started(anim_name: String):
    print("开始播放动画:", anim_name)

# 保留重写的seek方法,用于代码主动调用时的打印
func seek(seconds: float, update: bool = false) -> void:
    print("代码调用seek,目标时间:", seconds)
    .seek(seconds, update)

方案2:实时查询动画位置(编辑器拖动时实时追踪)

如果需要在拖动时间轴过程中实时打印当前时间,可在_process中主动查询:

tool
extends AnimationPlayer

var last_recorded_time: float = -1.0
# 控制打印频率,避免刷屏
const TIME_THRESHOLD: float = 0.01

func _process(delta: float):
    if get_current_animation() != null:
        var current_time = get_current_animation_position()
        if abs(current_time - last_recorded_time) > TIME_THRESHOLD:
            print("当前时间轴位置:", current_time)
            last_recorded_time = current_time

方案3:在运行模式下测试重写方法

若仅验证代码调用时的seek逻辑,点击编辑器Play in Editor按钮进入运行模式,此时通过代码调用seek或播放动画,自定义重写方法会触发打印。

关键注意点

  • tool脚本仅让代码在编辑器中生效,但无法拦截所有编辑器内部的引擎操作,比如实时拖动时间轴的过程。
  • 要完全追踪编辑器中的时间轴操作,只能通过主动查询或依赖引擎提供的信号,无法通过重写seek方法实现。

内容的提问来源于stack exchange,提问作者cak3_lover

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 06:35:36