GUIProxy.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.active=true;
  54. this.mediator.OnShow(this.data);
  55. }
  56. private LoadUIAssetBundle(){
  57. this.state=1;
  58. loader.loadRes(this.prefabName, Prefab,this.OnLoadUIPrefabProgress.bind(this),this.OnLoadUIPrefabComplete.bind(this));
  59. }
  60. private OnLoadUIPrefabProgress(completedCount: number, totalCount: number, item: any):void{
  61. // console.log(this.prefabName+":"+(finish/total));
  62. }
  63. private OnLoadUIPrefabComplete(error: Error, assets:Prefab):void{
  64. if(error!=null){
  65. throw new Error("请检查UIConst中注册UI时的预制体路径是否正确:"+this.prefabName);
  66. }
  67. this.prefab=assets;
  68. this.state=2;
  69. this.uiNode=cc.instantiate(this.prefab);
  70. this.mediator=this.uiNode.getComponent(GUIMediator);
  71. this.mediator.uiKey=this.key;
  72. //添加到层
  73. LayerManager.single.AddToLayer(this.mediator.LayerIndex,this.uiNode);
  74. if(this.isShowing){
  75. this.AddToLayer();
  76. }
  77. }
  78. /**
  79. * 获取UI所属层
  80. */
  81. get LayerIndex(){
  82. return 3;
  83. }
  84. }