先準備好負責下載的函式:
/**下載圖檔 */
public DownloadTexture(url: string): Promise {
return new Promise((resolve, reject) => {
cc.assetManager.loadRemote(url, function (err: Error, tex: cc.Texture2D) {
if (err) {
console.warn("[LoadTexure]Image ("+url+") Download is not Find !! " , err);
reject(err);
} else {
resolve(tex);
}
});
});
}
/**下載Bundle */
public DownloadBundle(url: string): Promise {
return new Promise((resolve, reject) => {
cc.assetManager.loadRemote(url, function (err: Error, bundle: cc.AssetManager.Bundle) {
if (err) {
console.warn("[LoadBundle]Bundle ("+url+") Download is not Find !! " , err);
reject(err);
} else {
resolve(bundle);
}
});
});
}
/**下載Prefabs */
public DownloadPrefab(bundle: cc.AssetManager.Bundle, path: string): Promise {
return new Promise((resolve, reject) => {
if(!bundle){
console.warn("[LoadBundle]Bundle is null");
reject();
}
bundle.load(path, function (err: Error, prefab: cc.Prefab) {
if (err) {
console.warn("[LoadBundle]Prefab ("+path+") Download is not Find !! " , err);
reject(err);
} else {
resolve(prefab);
}
});
});
}
接下來執行的主流程如下:
public Main(){
let bundle: cc.AssetManager.Bundle = null;
await this.DownloadBundle(downloadPath).catch((err: any) => {
cc.warn("Error!", "Can't Get Bundle (path:"+downloadPath+")");
}).then((result: cc.AssetManager.Bundle) =>{
bundle = result;
});
if (bundle == null) return;
let prefab: cc.Prefab = null;
await this.DownloadPrefab(bundle, "main").catch((err: any) => {
cc.warn("Error!", "Can't Get Prefab (path:"+downloadPath+")");
}).then((result: cc.Prefab) =>{
prefab = result;
});
if (prefab == null) return;
let child: cc.Node = cc.instantiate(prefab);
child.parent = this.node;
}
沒有留言:
張貼留言
歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT