SceneManager.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { _decorator, Component, Node, director, SceneAsset } from 'cc';
  2. import { LoadingView } from '../loadingView/LoadingView';
  3. const { ccclass, property } = _decorator;
  4. export class SceneManager{
  5. private sceneName:string;
  6. /**
  7. * 切换场景
  8. * @param sceneName
  9. */
  10. public Swicth(sceneName:string):void{
  11. this.sceneName=sceneName;
  12. LoadingView.single.Show();
  13. console.log("切换到:"+sceneName);
  14. LoadingView.single.UpdateLabel("切换到:"+sceneName);
  15. director.preloadScene(sceneName,this.OnProgressHandler.bind(this),this.OnCompleteHandler.bind(this))
  16. }
  17. private OnProgressHandler(completedCount: number, totalCount: number, item: any):void{
  18. LoadingView.single.UpdateProgress(completedCount/totalCount);
  19. }
  20. private OnCompleteHandler(error: null | Error, sceneAsset?: SceneAsset):void{
  21. if(error!=null){
  22. console.error(this.sceneName+"加载错误:"+error);
  23. LoadingView.single.UpdateLabel(this.sceneName+"加载错误:"+error);
  24. }
  25. director.loadScene(this.sceneName);
  26. LoadingView.single.Hide();
  27. }
  28. private static instance:SceneManager;
  29. public static get single(){
  30. if(this.instance==null){
  31. this.instance=new SceneManager();
  32. }
  33. return this.instance;
  34. }
  35. }