您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

163 行
5.0 KiB

  1. // Copyright (C) 2015 Google, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using System;
  15. using System.Reflection;
  16. using GoogleMobileAds.Common;
  17. namespace GoogleMobileAds.Api
  18. {
  19. public class BannerView
  20. {
  21. private IBannerClient client;
  22. // Creates a BannerView and adds it to the view hierarchy.
  23. public BannerView(string adUnitId, AdSize adSize, AdPosition position)
  24. {
  25. Type googleMobileAdsClientFactory = Type.GetType(
  26. "GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp");
  27. MethodInfo method = googleMobileAdsClientFactory.GetMethod(
  28. "BuildBannerClient",
  29. BindingFlags.Static | BindingFlags.Public);
  30. this.client = (IBannerClient)method.Invoke(null, null);
  31. client.CreateBannerView(adUnitId, adSize, position);
  32. ConfigureBannerEvents();
  33. }
  34. // Creates a BannerView with a custom position.
  35. public BannerView(string adUnitId, AdSize adSize, int x, int y)
  36. {
  37. Type googleMobileAdsClientFactory = Type.GetType(
  38. "GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp");
  39. MethodInfo method = googleMobileAdsClientFactory.GetMethod(
  40. "BuildBannerClient",
  41. BindingFlags.Static | BindingFlags.Public);
  42. this.client = (IBannerClient)method.Invoke(null, null);
  43. client.CreateBannerView(adUnitId, adSize, x, y);
  44. ConfigureBannerEvents();
  45. }
  46. // These are the ad callback events that can be hooked into.
  47. public event EventHandler<EventArgs> OnAdLoaded;
  48. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  49. public event EventHandler<EventArgs> OnAdOpening;
  50. public event EventHandler<EventArgs> OnAdClosed;
  51. public event EventHandler<EventArgs> OnAdLeavingApplication;
  52. // Loads an ad into the BannerView.
  53. public void LoadAd(AdRequest request)
  54. {
  55. client.LoadAd(request);
  56. }
  57. // Hides the BannerView from the screen.
  58. public void Hide()
  59. {
  60. client.HideBannerView();
  61. }
  62. // Shows the BannerView on the screen.
  63. public void Show()
  64. {
  65. client.ShowBannerView();
  66. }
  67. // Destroys the BannerView.
  68. public void Destroy()
  69. {
  70. client.DestroyBannerView();
  71. }
  72. // Returns the height of the BannerView in pixels.
  73. public float GetHeightInPixels()
  74. {
  75. return client.GetHeightInPixels();
  76. }
  77. // Returns the width of the BannerView in pixels.
  78. public float GetWidthInPixels()
  79. {
  80. return client.GetWidthInPixels();
  81. }
  82. // Set the position of the BannerView using standard position.
  83. public void SetPosition(AdPosition adPosition)
  84. {
  85. client.SetPosition(adPosition);
  86. }
  87. // Set the position of the BannerView using custom position.
  88. public void SetPosition(int x, int y)
  89. {
  90. client.SetPosition(x, y);
  91. }
  92. private void ConfigureBannerEvents()
  93. {
  94. this.client.OnAdLoaded += (sender, args) =>
  95. {
  96. if (this.OnAdLoaded != null)
  97. {
  98. this.OnAdLoaded(this, args);
  99. }
  100. };
  101. this.client.OnAdFailedToLoad += (sender, args) =>
  102. {
  103. if (this.OnAdFailedToLoad != null)
  104. {
  105. this.OnAdFailedToLoad(this, args);
  106. }
  107. };
  108. this.client.OnAdOpening += (sender, args) =>
  109. {
  110. if (this.OnAdOpening != null)
  111. {
  112. this.OnAdOpening(this, args);
  113. }
  114. };
  115. this.client.OnAdClosed += (sender, args) =>
  116. {
  117. if (this.OnAdClosed != null)
  118. {
  119. this.OnAdClosed(this, args);
  120. }
  121. };
  122. this.client.OnAdLeavingApplication += (sender, args) =>
  123. {
  124. if (this.OnAdLeavingApplication != null)
  125. {
  126. this.OnAdLeavingApplication(this, args);
  127. }
  128. };
  129. }
  130. // Returns the mediation adapter class name.
  131. public string MediationAdapterClassName()
  132. {
  133. return this.client.MediationAdapterClassName();
  134. }
  135. }
  136. }