由于cc.director.preloadScene(sceneName,onload) 只有这两个方法可以使用
本来加载场景时间有点久 也有需求要等待加载进度
本人不甘心 去读了下 引擎的源码(引擎组代码还是很值得大家去读一读的)
里面是支持 预加载场景进的 就自己拉出来重写了下
也分享给大家 直接贴代码
ts 使用
Global.preloadScene(this, "sceneName", (error, item) => {
if (cc.sys.isBrowser) { // 浏览器设置
// (<any>cc.loader).onProgress = null;
// if (this.pb) this.pb.progress = 1;
cc.director.loadScene('sceneName');
return;
}
}, (e, a) => {
// console.log(this.pb, e, a)
if (this.pb) this.pb.progress = e / a;
});
ts 代码 封装
/** 重写引擎方法 添加进度方法 */
static preloadScene(_This, sceneName, onLoaded?, onProgress?) {
let director: any = cc.director;
let info = director._getSceneUuid(sceneName);
if (info) {
director.emit((<any>cc.Director).EVENT_BEFORE_SCENE_LOADING,sceneName);
cc.loader.load({ uuid: info.uuid, type: 'uuid' }, onProgress == null ? null : function (e, a) {
if (onProgress) onProgress.call(_This, e, a);
}, function (error, asset) {
if (error) {
(<any>cc).errorID(1215, sceneName, error.message);
}
if (onLoaded) {
onLoaded(error, asset);
}
});
}
else {
var error = 'Can not preload the scene "' + sceneName + '" because it is not in the build settings.';
if (onLoaded) onLoaded(new Error(error));
cc.error('preloadScene: ' + error);
}
}
js 代码 封装
Global.preloadScene = function(_This, sceneName, onLoaded, onProgress) {
var director = cc.director;
var info = director._getSceneUuid(sceneName);
if (info) {
director.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING,sceneName);
cc.loader.load({
uuid: info.uuid,
type: "uuid"
}, null == onProgress ? null : function(e, a) {
onProgress && onProgress.call(_This, e, a);
}, function(error, asset) {
error && cc.errorID(1215, sceneName, error.message);
onLoaded && onLoaded(error, asset);
});
} else {
var error = 'Can not preload the scene "' + sceneName + '" because it is not in the build settings.';
onLoaded && onLoaded(new Error(error));
cc.error("preloadScene: " + error);
}
};