[LayaAir3]骨骼动画加载报错
laya加载骨骼动画,总是报Error: Skin not found: default,但是json里面有"skins":{"default",怎么解决?
下面是代码
const { regClass, property } = Laya;
@regClass()
export class Main extends Laya.Script {
@property({ type: Laya.Button, caption: "切换按钮" })
public btn: Laya.Button;
spine: Laya.Spine2DRenderNode;
//外部皮肤
weaponSkin: Laya.ExternalSkin = new Laya.ExternalSkin();
//外部皮肤列表项
weaponSkinItem: Laya.ExternalSkinItem = new Laya.ExternalSkinItem();
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
// 加载Spine动画资源
Laya.loader.load(["resources/Battle/Spine/diaochan/Diaochan_s38.json"], Laya.Loader.SPINE).then(() => {
// 添加Spine渲染器组件到精灵节点上 并在添加后返回Spine渲染器组件
this.spine = this.owner.addComponent(Laya.Spine2DRenderNode);
this.spine.source = "resources/Battle/Spine/diaochan/Diaochan_s38.json"; // 设置Spine动画数据源
// this.spine.skinName = "default"; // 设置皮肤名称
this.spine.play(0, true); // 播放名称为"att"的攻击动画,true表示循环播放
// this.btn.on(Laya.Event.CLICK, this, this.changeAttachment); //监听点击事件,触发切换武器的方法
//以下基础设置可以在IDE里设置,这样代码就不用添加了,此处仅为演示代码使用方式
//设置外部皮肤对象
// this.spine.externalSkins = [this.weaponSkin];
// // 给外部皮肤对象设置数据
// this.weaponSkin.source = "resources/Battle/Spine/diaochan/Diaochan_s38.json"; // 设置外部皮肤数据源
// this.weaponSkin.items = [this.weaponSkinItem];// 设置外部皮肤列表项
// // 给外部皮肤列表项设置数据
// this.weaponSkinItem.slot = "taidao"; // 设置插槽名称
// this.weaponSkinItem.skin = "default"; // 设置皮肤名称
});
}
//改变武器
changeAttachment(): void {
// 根据当前附件状态切换武器
const newAttachment = this.weaponSkinItem.attachment === "weapon_1" ? "weapon_3" : "weapon_1";
this.setAttachment("taidao", "default", newAttachment);
console.log(`切换到 ${newAttachment}`);
}
//设置武器的附件
setAttachment(slot: string, skinName: string, attachmentName: string): void {
this.weaponSkinItem.slot = slot; // 设置插槽名称
this.weaponSkinItem.skin = skinName; // 设置皮肤名称
this.weaponSkinItem.attachment = attachmentName; // 设置附件名称
this.spine.resetExternalSkin(); // 重置加载的外部皮肤,使设置生效
}
}
下面是代码
const { regClass, property } = Laya;
@regClass()
export class Main extends Laya.Script {
@property({ type: Laya.Button, caption: "切换按钮" })
public btn: Laya.Button;
spine: Laya.Spine2DRenderNode;
//外部皮肤
weaponSkin: Laya.ExternalSkin = new Laya.ExternalSkin();
//外部皮肤列表项
weaponSkinItem: Laya.ExternalSkinItem = new Laya.ExternalSkinItem();
//组件被激活后执行,此时所有节点和组件均已创建完毕,此方法只执行一次
onAwake(): void {
// 加载Spine动画资源
Laya.loader.load(["resources/Battle/Spine/diaochan/Diaochan_s38.json"], Laya.Loader.SPINE).then(() => {
// 添加Spine渲染器组件到精灵节点上 并在添加后返回Spine渲染器组件
this.spine = this.owner.addComponent(Laya.Spine2DRenderNode);
this.spine.source = "resources/Battle/Spine/diaochan/Diaochan_s38.json"; // 设置Spine动画数据源
// this.spine.skinName = "default"; // 设置皮肤名称
this.spine.play(0, true); // 播放名称为"att"的攻击动画,true表示循环播放
// this.btn.on(Laya.Event.CLICK, this, this.changeAttachment); //监听点击事件,触发切换武器的方法
//以下基础设置可以在IDE里设置,这样代码就不用添加了,此处仅为演示代码使用方式
//设置外部皮肤对象
// this.spine.externalSkins = [this.weaponSkin];
// // 给外部皮肤对象设置数据
// this.weaponSkin.source = "resources/Battle/Spine/diaochan/Diaochan_s38.json"; // 设置外部皮肤数据源
// this.weaponSkin.items = [this.weaponSkinItem];// 设置外部皮肤列表项
// // 给外部皮肤列表项设置数据
// this.weaponSkinItem.slot = "taidao"; // 设置插槽名称
// this.weaponSkinItem.skin = "default"; // 设置皮肤名称
});
}
//改变武器
changeAttachment(): void {
// 根据当前附件状态切换武器
const newAttachment = this.weaponSkinItem.attachment === "weapon_1" ? "weapon_3" : "weapon_1";
this.setAttachment("taidao", "default", newAttachment);
console.log(`切换到 ${newAttachment}`);
}
//设置武器的附件
setAttachment(slot: string, skinName: string, attachmentName: string): void {
this.weaponSkinItem.slot = slot; // 设置插槽名称
this.weaponSkinItem.skin = skinName; // 设置皮肤名称
this.weaponSkinItem.attachment = attachmentName; // 设置附件名称
this.spine.resetExternalSkin(); // 重置加载的外部皮肤,使设置生效
}
}
没有找到相关结果
已邀请:
要回复问题请先登录
0 个回复