You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.0 KiB
92 lines
3.0 KiB
|
1 week ago
|
import { _decorator, AssetManager, assetManager, Component, Node, sp } from 'cc';
|
||
|
|
import { ensureSpineAssetResetEnums } from '../module/lang/spineRuntimePatch';
|
||
|
|
const { ccclass, property } = _decorator;
|
||
|
|
|
||
|
|
@ccclass('SetSpine')
|
||
|
|
export class SetSpine extends Component {
|
||
|
|
|
||
|
|
anim: sp.Skeleton = null;
|
||
|
|
protected onLoad(): void {
|
||
|
|
this.anim = this.getComponent(sp.Skeleton);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置 Spine 资源
|
||
|
|
* @param bundleName - Asset Bundle 的名称
|
||
|
|
* @param spinePath - Spine 资源的路径
|
||
|
|
* @param animationName - 动画名称,默认值为空字符串
|
||
|
|
* @param loop - 是否循环播放,默认值为 true
|
||
|
|
*/
|
||
|
|
setSpine(bundleName: string, spinePath: string, animationName: string = "", loop: boolean = true) {
|
||
|
|
if (this.anim) {
|
||
|
|
this.loadBundle(bundleName).then(bundle => {
|
||
|
|
this.loadSpineAsset(bundle, spinePath).then(spineAsset => {
|
||
|
|
ensureSpineAssetResetEnums(spineAsset);
|
||
|
|
this.anim.skeletonData = spineAsset;
|
||
|
|
if (animationName) {
|
||
|
|
this.anim.setAnimation(0, animationName, loop);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 设置 Spine 动画
|
||
|
|
* @param animationName - 动画名称
|
||
|
|
* @param loop - 是否循环播放,默认值为 true
|
||
|
|
*/
|
||
|
|
setAnimation(animationName: string, loop: boolean = true) {
|
||
|
|
if (this.anim) {
|
||
|
|
this.anim.setAnimation(0, animationName, loop);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 加载 Asset Bundle
|
||
|
|
* @param bundleName - Asset Bundle 的名称
|
||
|
|
* @returns Promise<AssetManager.Bundle> - 返回加载完成的 Asset Bundle
|
||
|
|
*/
|
||
|
|
private loadBundle(bundleName: string): Promise<AssetManager.Bundle> {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
let bundle = assetManager.getBundle(bundleName);
|
||
|
|
if (bundle) {
|
||
|
|
resolve(bundle);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
assetManager.loadBundle(bundleName, (err, bundle) => {
|
||
|
|
if (err) {
|
||
|
|
reject(`Failed to load bundle: ${bundleName}, error: ${err.message}`);
|
||
|
|
} else {
|
||
|
|
resolve(bundle);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 加载 Spine 资源
|
||
|
|
* @param bundle - Asset Bundle
|
||
|
|
* @param spinePath - Spine 资源的路径
|
||
|
|
* @returns Promise<sp.SkeletonData> - 返回加载完成的 Spine 数据
|
||
|
|
*/
|
||
|
|
private loadSpineAsset(bundle: AssetManager.Bundle, spinePath: string): Promise<sp.SkeletonData> {
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
let asset = bundle.get(spinePath, sp.SkeletonData);
|
||
|
|
if (asset) {
|
||
|
|
resolve(asset);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
bundle.load(spinePath, sp.SkeletonData, (err, asset) => {
|
||
|
|
if (err) {
|
||
|
|
reject(`Failed to load Spine asset: ${spinePath}, error: ${err.message}`);
|
||
|
|
} else {
|
||
|
|
resolve(asset);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|