cc.director.preloadScene内部也是调用的cc.loader.load,为什么不把progressCallback暴露出来

这个是preloadScene源码:

    /**
     * !#en
     * Preloads the scene to reduces loading time. You can call this method at any time you want.
     * After calling this method, you still need to launch the scene by `cc.director.loadScene`.
     * It will be totally fine to call `cc.director.loadScene` at any time even if the preloading is not
     * yet finished, the scene will be launched after loaded automatically.
     * !#zh 预加载场景,你可以在任何时候调用这个方法。
     * 调用完后,你仍然需要通过 `cc.director.loadScene` 来启动场景,因为这个方法不会执行场景加载操作。
     * 就算预加载还没完成,你也可以直接调用 `cc.director.loadScene`,加载完成后场景就会启动。
     *
     * @method preloadScene
     * @param {String} sceneName - The name of the scene to preload.
     * @param {Function} [onLoaded] - callback, will be called after scene loaded.
     * @param {Error} onLoaded.error - null or the error object.
     */
    preloadScene: function (sceneName, onLoaded) {
        var info = this._getSceneUuid(sceneName);
        if (info) {
            this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
            cc.loader.load({ uuid: info.uuid, type: 'uuid' }, function (error, asset) {
                if (error) {
                    cc.errorID(1210, 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.';
            onLoaded(new Error(error));
            cc.error('preloadScene: ' + error);
        }

实测下面这个方式,可以准确获取场景加载进度的.

        var info = cc.director._getSceneUuid(this.sceneName);
        var self = this;
        if (info) {
            cc.loader.load({ uuid: info.uuid, type: 'uuid' }, (completedCount, totalCount, item) => {
                cc.log("已完成Items:" + completedCount);
                cc.log("全部Items:" + totalCount);
                cc.log("当前Item:" + item.url);
                self._loadingNextStep = parseInt(completedCount / totalCount * 100);
                cc.log("加载进度:" + self._loadingNextStep);
            }, (error, asset) => {
                if (error) {
                    cc.errorID(1210, this.sceneName, error.message);
                } else {
                    cc.log("加载完成");
                }
            });
        }
4赞

厉害诶。模拟器测试通过。感谢分享。!

(我之前都是用假的进度条的)

厉害了

this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
刚在微信调试发现个问题,这一句也得加上:3:

感谢LZ

多谢楼主提供代码

厉害了 不错 用上了
浏览器中测试 通过
> var preloadScene = function(sceneName, onLoaded) {
> var info = cc.director._getSceneUuid(sceneName);
> if (info) {
> cc.director.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
> cc.loader.load({ uuid: info.uuid, type: ‘uuid’ },
>
> (completedCount, totalCount, item) => {
> cc.log(“已完成Items:” + completedCount);
> cc.log(“全部Items:” + totalCount);
> cc.log(“当前Item:” + item.url);
> let _loadingNextStep = (completedCount / totalCount * 100);
> cc.log(“加载进度:” + _loadingNextStep);
> }
>
> , function (error, asset) {
> if (error) {
> }
> if (onLoaded) {
> onLoaded(error, asset);
> }
> });
> }
> else {
> onLoaded(new Error());
> }
> }

2赞

cc.loader.onProgress = function (completedCount, totalCount, item) {
var percent = 100 * completedCount / totalCount;
我用的这个

1赞

为什么 preload 还要拿加载进度……

就是特地做了一个加载页

那直接用 loadScene 不就好了吗?

这个有点 不够 … 不过也挺好的 都会掉这个方法 而不是某一次的…
还是 上面的 解决方法 直接 更起作用

@jare loadScene 可以获取到场景加载进度吗?:sweat:

问题是loadScene 也无法获取进度来显示啊

proloadScene怎么获取进度loadScene就怎么获取进度

所以, loadScene 是要怎么获取场景加载进度呢?参照上面 @ydc__ 提供的代码,_getSceneUuid 这个方法在编辑器会提示错误:类型“Director”上不存在属性“_getSceneUuid”。
而且,既然 preloadScene 或 loadScene 的内部实现是可以获取到加载进度,为何不把加载进度暴露出来呢?而需要开发者自己参照着源码来“硬是又实现了一遍获取场景加载进度”呢?最后就是,_getSceneUuid 还是cc.director 的私有方法,这是什么节奏?

	cc.loader.onProgress = function(completeCount, totalCount) {
	}.bind(this);
	cc.director.loadScene(sceneName);

暴露出来了啊,是你们自己非要研究源码内部,高版本API有没有改我不知道,至少1.8.2直接这么写就行了,不需要研究源码

1.9.1 onProgress 这个api 已经被拿掉了

[quote=“fanhaining, post:18, topic:58565”]
暴露出来了啊
[/quote] 不知道在文档的哪里还有关于 cc.loader.onProgress 的介绍?

[quote=“fanhaining, post:18, topic:58565”]
是你们自己非要研究源码内部
[/quote] 如果官方有直接可以获取到场景加载进度的api,除非是开发者有意阅读源码学习,否则肯定直接使用官方api了。