你好,可以参考这个代码在原生平台加载 spine 资源:
// Learn TypeScript:
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
@ccclass
export default class NewClass extends cc.Component {
@property
mySpineJsonUrl: string = "http://192.168.50.104:8000/assets/resources/spineRaptor/raptor.json";
@property
mySpineTextureUrl: string = "http://192.168.50.104:8000/assets/resources/spineRaptor/raptor.png";
@property
mySpineAtlasUrl: string = "http://192.168.50.104:8000/assets/resources/spineRaptor/raptor.atlas";
@property(cc.Label)
myLabel: cc.Label = null;
_downloader: any;
_bonesTask: any;
_atlasTask: any;
_storagePath: string;
_inited: any;
_localSpineJsonUrl: string;
_localSpineAtlasUrl: string;
start () {
this.myLabel.string = "开始下载...";
this._downloader = new jsb.Downloader();
this._downloader.setOnFileTaskSuccess(this.onSucceed.bind(this));
this._downloader.setOnTaskProgress(this.onProgress.bind(this));
this._downloader.setOnTaskError(this.onError.bind(this));
this._storagePath = jsb.fileUtils.getWritablePath() + 'dragonBone-spine_Dome/downloader/';
this._inited = jsb.fileUtils.createDirectory(this._storagePath);
if (!this._inited) {
this.myLabel.string = 'Failed to create storage path, downloader won\'t work correctly';
}
this._bonesTask = this._downloader.createDownloadFileTask(this.mySpineJsonUrl, this._storagePath + 'raptor.json');
this.node.on("download spinejson finish",()=>{
this._atlasTask = this._downloader.createDownloadFileTask(this.mySpineAtlasUrl, this._storagePath + 'raptor.atlas');
})
}
onSucceed (task) {
switch (task.requestURL) {
case this.mySpineJsonUrl:
this._localSpineJsonUrl = task.storagePath;
this.node.emit("download spinejson finish");
break;
case this.mySpineAtlasUrl:
this._localSpineAtlasUrl = task.storagePath;
this.node.emit("download spineAtlas finish");
break;
}
this.node.on("download spineAtlas finish", ()=>{
this.myLabel.string = "下载成功...";
var spineNode = new cc.Node();
spineNode.name = 'newSpine';
spineNode.setPosition(0, 0);
var skeleton = spineNode.addComponent(sp.Skeleton);
cc.find("Canvas").addChild(spineNode);
var imageUrl = this.mySpineTextureUrl;
var skeUrl = this._localSpineJsonUrl;
var atlasUrl = this._localSpineAtlasUrl;
cc.loader.load(imageUrl, (error, texture) => {
cc.loader.load({ url: atlasUrl, type: 'txt' }, (error, atlasJson) => {
cc.loader.load({ url: skeUrl, type: 'txt' }, (error, spineJson) => {
var asset = new sp.SkeletonData();
asset._uuid = skeUrl;
asset.skeletonJson = spineJson;
asset.atlasText = atlasJson;
asset.textures = [texture];
asset.textureNames = ['raptor.png'];
skeleton.skeletonData = asset;
skeleton.animation = 'walk';
skeleton._updateSkeletonData();
});
});
});
});
}
onProgress (task, bytesReceived, totalBytesReceived, totalBytesExpected) {
this.myLabel.string = "正在下载..."
}
onError () {
this.myLabel.string = "下载失败...";
}
// update (dt) {}
}
CocosCreator 模拟器可以这样加载,并且在 ios 和 android 模拟器都测试通过。