ADHelper.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { _decorator, Component, Enum, Vec2 } from 'cc';
  2. import { platform } from './Data/platform';
  3. const { ccclass, property } = _decorator;
  4. export enum ADType {
  5. None,
  6. Interstutal,
  7. FullScreen,
  8. Banner,
  9. Native
  10. }
  11. export enum ADShowType {
  12. Single,
  13. Both
  14. }
  15. export enum ADDeleyType {
  16. Const,
  17. Random
  18. }
  19. @ccclass("ADHelperDeleyData")
  20. export class ADHelperDeleyData {
  21. @property({
  22. type: Enum(ADDeleyType),
  23. tooltip: "延迟类型:\nConst:固定时间\nRandom:随机时间"
  24. })
  25. type: ADDeleyType = ADDeleyType.Const;
  26. @property({
  27. tooltip: "延迟时间(常量)", visible() {
  28. return this.type == ADDeleyType.Const;
  29. }
  30. })
  31. public deley_const: number = 0;
  32. @property({
  33. tooltip: "延迟时间(随机值)", visible() {
  34. return this.type == ADDeleyType.Random;
  35. }
  36. })
  37. public deley_random: Vec2 = new Vec2();
  38. }
  39. @ccclass("ADHelperData")
  40. export class ADHelperData {
  41. @property({
  42. type: Enum(ADType),
  43. tooltip: "广告类型:\nNone:不弹广告\nInterstutal:插屏广告\nFullScreen:全屏广告\nBanner:banner广告\nNative:原生信息流广告"
  44. })
  45. type: ADType = ADType.None;
  46. @property({ tooltip: "广告权重" }) weight: number = 0;
  47. @property({ type: ADHelperDeleyData, tooltip: "广告延迟显示时间(秒)" }) deley: ADHelperDeleyData = new ADHelperDeleyData();
  48. }
  49. @ccclass('ADHelper')
  50. export class ADHelper extends Component {
  51. @property({
  52. type: Enum(ADShowType),
  53. tooltip: "广告显示方式:\nsingle:只显示单个广告\nboth:可同时显示多个广告"
  54. })
  55. showType: ADShowType = ADShowType.Single;
  56. @property({ type: [ADHelperData], tooltip: "广告数据集合" }) ads: Array<ADHelperData> = [];
  57. @property({ tooltip: "是否立即显示广告" }) showOnLoad: boolean = true;
  58. private totleWeight = 0;
  59. start() {
  60. for (let i = 0; i < this.ads.length; i++) {
  61. this.totleWeight += this.ads[i].weight;
  62. }
  63. if (this.showOnLoad) {
  64. this.show();
  65. }
  66. }
  67. public show(): void {
  68. this.doRandom();
  69. }
  70. private doRandom(): void {
  71. if (this.showType == ADShowType.Single) {
  72. let random = this.random(0, this.totleWeight);
  73. let curWight = 0;
  74. for (let i = 0; i < this.ads.length; i++) {
  75. curWight += this.ads[i].weight;
  76. if (random <= curWight) {
  77. this.showAD(this.ads[i]);
  78. return;
  79. }
  80. }
  81. } else {
  82. for (let i = 0; i < this.ads.length; i++) {
  83. if (this.random(0, this.totleWeight) <= this.ads[i].weight) {
  84. this.showAD(this.ads[i]);
  85. }
  86. }
  87. }
  88. }
  89. private nativeADIsShow = false;
  90. private BannerADIsShow = false;
  91. private showAD(data: ADHelperData): void {
  92. let deley = 0;
  93. if (data.deley.type == ADDeleyType.Const) {
  94. deley = data.deley.deley_const;
  95. } else if (data.deley.type == ADDeleyType.Random) {
  96. deley = this.random(data.deley.deley_random.x, data.deley.deley_random.y);
  97. }
  98. let self = this;
  99. if (deley) {
  100. this.scheduleOnce(() => {
  101. self._showAD(data);
  102. }, deley);
  103. }
  104. else {
  105. self._showAD(data);
  106. }
  107. }
  108. private _showAD(data) {
  109. switch (data.type) {
  110. case ADType.None:
  111. break;
  112. case ADType.Native:
  113. this.nativeADIsShow = true;
  114. platform.showNativeAD();
  115. break;
  116. case ADType.Interstutal:
  117. platform.showInterstutalAD();
  118. break;
  119. case ADType.FullScreen:
  120. platform.showFullScreenAD();
  121. break;
  122. case ADType.Banner:
  123. this.BannerADIsShow = true;
  124. platform.showBannerAD();
  125. break;
  126. }
  127. }
  128. private random(min: number, max: number): number {
  129. return min + Math.random() * (max - min);
  130. }
  131. onDestroy() {
  132. if (this.nativeADIsShow) {
  133. platform.removeNativeAD();
  134. }
  135. if (this.BannerADIsShow) {
  136. platform.removeBannerAD();
  137. }
  138. }
  139. }