GUIProxy.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { LayerManager } from "./LayerManager";
  2. import { GUIManager } from "./GUIManager";
  3. import { Prefab, Node, loader } from "cc";
  4. import { GUIMediator } from "./GUIMediator";
  5. export default class GUIProxy{
  6. key:number;
  7. prefabName:string;
  8. prefab:Prefab;
  9. uiNode:Node;
  10. mediator:GUIMediator;
  11. /**
  12. * 状态 0 未加载 1加载中 2加载完成
  13. */
  14. state:number=0;
  15. /**
  16. * 是否显示
  17. */
  18. isShowing:boolean;
  19. data:any;
  20. constructor(key:number,prefabName:string){
  21. this.key=key;
  22. this.prefabName=prefabName;
  23. }
  24. /**
  25. * 显示(内部接口)
  26. * @param data
  27. */
  28. Show(data?:any):void{
  29. this.data=data;
  30. this.isShowing=true;
  31. if(this.state==0){
  32. this.LoadUIAssetBundle();
  33. }else if(this.state==1){
  34. //加载中啥也不干
  35. }else{
  36. this.AddToLayer();
  37. }
  38. }
  39. /**
  40. * 内部接口
  41. */
  42. Hide():void{
  43. if(this.mediator){
  44. this.uiNode.active=false;
  45. this.mediator.OnHide();
  46. }
  47. this.isShowing=false;
  48. }
  49. /**
  50. * 添加到层上并显示
  51. */
  52. private AddToLayer():void{
  53. this.uiNode=cc.instantiate(this.prefab);
  54. this.mediator=this.uiNode.getComponent(GUIMediator);
  55. this.mediator.uiKey=this.key;
  56. //添加到层
  57. LayerManager.single.AddToLayer(this.mediator.LayerIndex,this.uiNode);
  58. this.mediator.OnShow(this.data);
  59. }
  60. private LoadUIAssetBundle(){
  61. this.state=1;
  62. loader.loadRes(this.prefabName, Prefab,this.OnLoadUIPrefabProgress.bind(this),this.OnLoadUIPrefabComplete.bind(this));
  63. }
  64. private OnLoadUIPrefabProgress(completedCount: number, totalCount: number, item: any):void{
  65. // console.log(this.prefabName+":"+(finish/total));
  66. }
  67. private OnLoadUIPrefabComplete(error: Error, assets:Prefab):void{
  68. if(error!=null){
  69. throw new Error("请检查UIConst中注册UI时的预制体路径是否正确:"+this.prefabName);
  70. }
  71. this.prefab=assets;
  72. this.state=2;
  73. if(this.isShowing){
  74. this.AddToLayer();
  75. }
  76. }
  77. /**
  78. * 获取UI所属层
  79. */
  80. get LayerIndex(){
  81. return 3;
  82. }
  83. }