UIPage.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { utils } from "../../common-plugin/Scripts/Utils";
  2. import { cocosz } from "./CocosZ";
  3. import UIMgr from "./UIMgr";
  4. export default class UIPage {
  5. protected _page: cc.Node = null;
  6. protected _pageName: string = "";
  7. /** 页面状态 */
  8. protected _isOpen: boolean = false;
  9. /** 记录页面插屏次数 */
  10. public static interstitialCount: { [name: string]: number } = {};
  11. constructor(pageName: string) {
  12. let prefab: cc.Prefab = cocosz.resMgr.getRes(pageName, cc.Prefab);
  13. if (prefab) {
  14. let node: cc.Node = cc.instantiate(prefab);
  15. if (node) {
  16. this._page = node;
  17. this._pageName = pageName;
  18. node.active = false;
  19. node.position = cc.Vec3.ZERO;
  20. this._isOpen = false;
  21. this.getUIRoot().addChild(node);
  22. node.group = 'ui';
  23. }
  24. } else {
  25. cocosz.resMgr.loadAndCacheRes("UI/" + pageName, cc.Prefab, null, (err: Error, res: any) => {
  26. if (res) {
  27. let node: cc.Node = cc.instantiate(res);
  28. if (node) {
  29. this._page = node;
  30. this._pageName = pageName;
  31. node.active = false;
  32. node.position = cc.Vec3.ZERO;
  33. this._isOpen = false;
  34. this.getUIRoot().addChild(node);
  35. node.group = 'ui';
  36. this.onLoad();
  37. this.open();
  38. }
  39. } else {
  40. cc.error(`Prefab ${pageName} is not found!`);
  41. }
  42. })
  43. }
  44. }
  45. public open() {
  46. if (this.isValid()) {
  47. if (!this._isOpen) {
  48. // 恢复开关
  49. UIMgr.canOpen = true;
  50. // 打开界面
  51. this._isOpen = true;
  52. this._page.active = true;
  53. this.onOpen();
  54. // 插屏
  55. let serverValue = cocosz.getConfigByKey("isInterstitial_" + this._pageName);
  56. if (serverValue) {
  57. // 每次都弹
  58. if (serverValue === "true") {
  59. cocosz.isShowAd && utils.adManager.ShowInterstitial();
  60. }
  61. // 几次弹一次
  62. else if ((Number.isInteger(serverValue) && serverValue > 0)) {
  63. if (!UIPage.interstitialCount[this._pageName]) { UIPage.interstitialCount[this._pageName] = 0; }
  64. if (++UIPage.interstitialCount[this._pageName] % serverValue === 0) {
  65. cocosz.isShowAd && utils.adManager.ShowInterstitial();
  66. }
  67. }
  68. }
  69. }
  70. } else {
  71. cc.log("Page is not found!");
  72. }
  73. }
  74. public close() {
  75. if (this._isOpen) {
  76. this._isOpen = false;
  77. this.onClose();
  78. }
  79. if (this.isValid()) {
  80. this._page.active = false;
  81. this.destroy();
  82. }
  83. // 资源销毁
  84. if ("UILoadingPage" === this._pageName) {
  85. cocosz.resMgr.releaseSingleRes(this._pageName, cc.Prefab);
  86. }
  87. }
  88. public destroy() {
  89. if (this._isOpen) {
  90. this._isOpen = false;
  91. }
  92. this.onDestroy();
  93. // 销毁界面
  94. if (this.isValid()) {
  95. this._page.destroy();
  96. }
  97. }
  98. protected getUIRoot() {
  99. return cc.find("Canvas");
  100. }
  101. public isValid() {
  102. return this._page && cc.isValid(this._page);
  103. }
  104. public isOpen(): boolean {
  105. return this.isValid() && this._isOpen;
  106. }
  107. /**
  108. * 子类扩展:页面初始化时调用,注意不是引擎周期函数,此时引擎周期函数还没有调用
  109. */
  110. protected onLoad() {
  111. }
  112. /**
  113. * 子类扩展:页面展示时调用
  114. */
  115. protected onOpen() { }
  116. /**
  117. * 子类扩展:页面关闭时调用
  118. */
  119. protected onClose() { }
  120. /**
  121. * 子类扩展:页面销毁时调用
  122. */
  123. protected onDestroy() { }
  124. // private _playShowAnimation(){
  125. // let animations: cc.Animation[] = this._page.getComponentsInChildren(cc.Animation);
  126. // if(animations){
  127. // for(let i=0;i<animations.length;i++){
  128. // let anim: cc.Animation = animations[i];
  129. // let clip: cc.AnimationClip = anim.defaultClip;
  130. // clip.wrapMode = cc.WrapMode.Normal;
  131. // anim.play(clip.name);
  132. // }
  133. // }
  134. // }
  135. // private _playHideAnimation(){
  136. // let animations: cc.Animation[] = this._page.getComponentsInChildren(cc.Animation);
  137. // if(animations){
  138. // for(let i=0;i<animations.length;i++){
  139. // let anim: cc.Animation = animations[i];
  140. // let clip: cc.AnimationClip = anim.defaultClip;
  141. // clip.wrapMode = cc.WrapMode.Reverse;
  142. // anim.play(clip.name);
  143. // }
  144. // }
  145. // }
  146. }