这是 3.7 版本 Animation 的 addClip 方法的源文件(https://github.com/cocos/cocos-engine/blob/afd22296b417d2b34791c855c066aa4d49059395/cocos/animation/animation-component.ts):
public addClip (clip: AnimationClip, name?: string): AnimationState {
if (js.array.contains(this._clips, clip)) {
this._clips.push(clip);
}
return this.createState(clip, name);
}
这里面的第一个判断,好像是当前实例中 this._clips 包含这个新的 clip 才会 push 进来,这个逻辑的结果,就是要新添加的 clip 实例永远不会真正添加进来。因为他永远不会符合判断条件。
在我实际调试程序时候,确实也是这个行为,我调用 addClip 添加 clip,永远不会加入到集合中,这才去看了源代码。。
这个方法老版本的实现我也看了(https://github.com/cocos/cocos-engine/blob/83a2717facdbdff216d7c4a420abaf670a4b3a18/cocos/animation/animation-component.ts):
public addClip (clip: AnimationClip, name?: string): AnimationState {
if (!ArrayUtils.contains(this._clips, clip)) {
this._clips.push(clip);
}
return this.createState(clip, name);
}
这个逻辑才是对的,列表中不存在,才添加。
框架团队参考下吧。感觉这是个逻辑bug。