Spine

阅读时间:2分钟更新于 2024-11-11 15:2

Galacean Effects 支持 Spine 骨骼动画文件的播放,设计使用教程参考:导入 Spine 文件

插件引入

需要额外接入 插件(请确保插件版本号与 @galcean/effects 版本号一致)。

$ npm i @galacean/effects-plugin-spine --save
import { Player } from '@galacean/effects';
// 确保在 @galacean/effects 引入之后再引入插件, 插件版本保持和 @galacean/effects 一致
import '@galacean/effects-plugin-spine';

API

获取指定动画时长

const animationDuration = getAnimationDuration(skeletonData, animationName);

获取创建纹理的参数

import { getTextureOptions } from '@galacean/effects-plugin-spine';

const { magFilter, minFilter, wrapS, wrapT, pma } = getTextureOptions(atlasBuffer);

获取动画列表/皮肤列表

  1. 在元素 cache 上使用函数获取
import { SpineComponent } from '@galacean/effects-plugin-spine';
const comp = await new Player().loadScene(scene);
const item = comp.getItemByName('itemName')
const { skeletonData } = item.getComponent(SpineComponent).cache;
const animationList = getAnimationList(skeletonData);
const skinList = getSkinList(skeletonData);
  1. 从 atals 和 skeleton 二进制数据中获取
const atlas = getAtlasFromBuffer(atlasBuffer);
const skeletonFile = getSkeletonFromBuffer(skeletonBuffer, skeletonType);
const skeletonData = createSkeletonData(atlas, skeletonFile, skeletonType);
const skinList = getSkinList(skeletonData);
const animationList = getAnimationList(skeletonData);

设置动画

设置单个动画
const comp = await new Player().loadScene(scene);
const item = comp.getItemByName(name);

item.getComponent(SpineComponent).setAnimation(animationName);
设置一组动画
const comp = await new Player().loadScene(scene);
const item = comp.getItemByName(name);
const animationList = [animationName1, animationName2, animationName3];

item.getComponent(SpineComponent).setAnimation(animationList);
设置一组动画并循环最后一个
const comp = await new Player().loadScene(scene);
const item = comp.getItemByName(name);
const animationList = [animationName1, animationName2, animationName3];

item.getComponent(SpineComponent).setAnimationListLoopEnd(animationList);

设置播放速度

const comp = await new Player().loadScene(scene);
const item = comp.getItemByName(name);

item.getComponent(SpineComponent).setSpeed(speed);

设置动画 mix 时间

  1. 设置动画的默认 mix 时间:
const comp = await new Player({ autoplay: false }).loadScene(scene);
const item = comp.getItemByName(name);

item.getComponent(SpineComponent).setDefaultMixDuration(mix);
  1. 设置指定动作的 mix 时间:
const comp = await new Player({ autoplay: false }).loadScene(scene);
const item = comp.getItemByName(name);

item.getComponent(SpineComponent).setMixDuration(animationOut, animationIn, mix);

注意:

  • 需要在 player.play() 前调用,所以初始化 Player 时设置 autoplayfalse,并手动调用 play()
Preview