Просмотр исходного кода

Merge pull request #3027 from bobalazek/master

Added onProgress to assets manager
David Catuhe 7 лет назад
Родитель
Сommit
7ba352b980
2 измененных файлов с 47 добавлено и 6 удалено
  1. 1 1
      .vscode/settings.json
  2. 46 5
      src/Tools/babylon.assetsManager.ts

+ 1 - 1
.vscode/settings.json

@@ -36,4 +36,4 @@
         "assets":true
     },
     "typescript.check.workspaceVersion": false
-}
+}

+ 46 - 5
src/Tools/babylon.assetsManager.ts

@@ -1,4 +1,4 @@
-module BABYLON {
+module BABYLON {
 
     export enum AssetTaskState {
         INIT,
@@ -80,6 +80,24 @@
 
     }
 
+    export interface IAssetsProgressEvent {
+        remainingCount: number;
+        totalCount: number;
+        task: IAssetTask;
+    }
+
+    export class AssetsProgressEvent implements IAssetsProgressEvent {
+        remainingCount: number;
+        totalCount: number;
+        task: IAssetTask;
+
+        constructor(remainingCount: number, totalCount: number, task: IAssetTask) {
+            this.remainingCount = remainingCount;
+            this.totalCount = totalCount;
+            this.task = task;
+        }
+    }
+
     export class MeshAssetTask extends AbstractAssetTask implements IAssetTask {
         public loadedMeshes: Array<AbstractMesh>;
         public loadedParticleSystems: Array<ParticleSystem>;
@@ -245,12 +263,14 @@
         public onFinish: (tasks: IAssetTask[]) => void;
         public onTaskSuccess: (task: IAssetTask) => void;
         public onTaskError: (task: IAssetTask) => void;
+        public onProgress: (remainingCount: number, totalCount: number, task: IAssetTask) => void;
 
         //Observables
 
         public onTaskSuccessObservable = new Observable<IAssetTask>();
         public onTaskErrorObservable = new Observable<IAssetTask>();
         public onTasksDoneObservable = new Observable<IAssetTask[]>();
+        public onProgressObservable = new Observable<IAssetsProgressEvent>();
 
         public useDefaultLoadingScreen = true;
 
@@ -308,9 +328,30 @@
             return task;
         }
 
-        private _decreaseWaitingTasksCount(): void {
+        private _decreaseWaitingTasksCount(task: AbstractAssetTask): void {
             this.waitingTasksCount--;
 
+            try {
+                if (this.onProgress) {
+                    this.onProgress(
+                        this.waitingTasksCount,
+                        this.tasks.length,
+                        task
+                    );
+                }
+
+                this.onProgressObservable.notifyObservers(
+                    new AssetsProgressEvent(
+                        this.waitingTasksCount,
+                        this.tasks.length,
+                        task
+                    )
+                );
+            } catch (e) {
+                Tools.Error("Error running progress callbacks.");
+                console.log(e);
+            }
+
             if (this.waitingTasksCount === 0) {
                 try {
                     if (this.onFinish) {
@@ -335,7 +376,7 @@
                         this.onTaskSuccess(task);
                     }
                     this.onTaskSuccessObservable.notifyObservers(task);
-                    this._decreaseWaitingTasksCount();
+                    this._decreaseWaitingTasksCount(task);
                 } catch (e) {
                     error("Error executing task success callbacks", e);
                 }
@@ -351,7 +392,7 @@
                     this.onTaskError(task);
                 }
                 this.onTaskErrorObservable.notifyObservers(task);
-                this._decreaseWaitingTasksCount();
+                this._decreaseWaitingTasksCount(task);
             }
 
             task.run(this._scene, done, error);
@@ -385,4 +426,4 @@
             return this;
         }
     }
-} 
+}