You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

169 lines
4.9 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. #if UNITY_ANDROID
  15. using System;
  16. using GoogleMobileAds.Api;
  17. using GoogleMobileAds.Common;
  18. using UnityEngine;
  19. namespace GoogleMobileAds.Android
  20. {
  21. public class BannerClient : AndroidJavaProxy, IBannerClient
  22. {
  23. private AndroidJavaObject bannerView;
  24. public BannerClient() : base(Utils.UnityAdListenerClassName)
  25. {
  26. AndroidJavaClass playerClass = new AndroidJavaClass(Utils.UnityActivityClassName);
  27. AndroidJavaObject activity =
  28. playerClass.GetStatic<AndroidJavaObject>("currentActivity");
  29. this.bannerView = new AndroidJavaObject(
  30. Utils.BannerViewClassName, activity, this);
  31. }
  32. public event EventHandler<EventArgs> OnAdLoaded;
  33. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  34. public event EventHandler<EventArgs> OnAdOpening;
  35. public event EventHandler<EventArgs> OnAdClosed;
  36. public event EventHandler<EventArgs> OnAdLeavingApplication;
  37. // Creates a banner view.
  38. public void CreateBannerView(string adUnitId, AdSize adSize, AdPosition position)
  39. {
  40. this.bannerView.Call(
  41. "create",
  42. new object[3] { adUnitId, Utils.GetAdSizeJavaObject(adSize), (int)position });
  43. }
  44. // Creates a banner view with a custom position.
  45. public void CreateBannerView(string adUnitId, AdSize adSize, int x, int y)
  46. {
  47. this.bannerView.Call(
  48. "create",
  49. new object[4] { adUnitId, Utils.GetAdSizeJavaObject(adSize), x, y });
  50. }
  51. // Loads an ad.
  52. public void LoadAd(AdRequest request)
  53. {
  54. this.bannerView.Call("loadAd", Utils.GetAdRequestJavaObject(request));
  55. }
  56. // Displays the banner view on the screen.
  57. public void ShowBannerView()
  58. {
  59. this.bannerView.Call("show");
  60. }
  61. // Hides the banner view from the screen.
  62. public void HideBannerView()
  63. {
  64. this.bannerView.Call("hide");
  65. }
  66. // Destroys the banner view.
  67. public void DestroyBannerView()
  68. {
  69. this.bannerView.Call("destroy");
  70. }
  71. // Returns the height of the BannerView in pixels.
  72. public float GetHeightInPixels()
  73. {
  74. return this.bannerView.Call<float>("getHeightInPixels");
  75. }
  76. // Returns the width of the BannerView in pixels.
  77. public float GetWidthInPixels()
  78. {
  79. return this.bannerView.Call<float>("getWidthInPixels");
  80. }
  81. // Set the position of the banner view using standard position.
  82. public void SetPosition(AdPosition adPosition)
  83. {
  84. this.bannerView.Call("setPosition", (int)adPosition);
  85. }
  86. // Set the position of the banner view using custom position.
  87. public void SetPosition(int x, int y)
  88. {
  89. this.bannerView.Call("setPosition", x, y);
  90. }
  91. // Returns the mediation adapter class name.
  92. public string MediationAdapterClassName()
  93. {
  94. return this.bannerView.Call<string>("getMediationAdapterClassName");
  95. }
  96. #region Callbacks from UnityBannerAdListener.
  97. public void onAdLoaded()
  98. {
  99. if (this.OnAdLoaded != null)
  100. {
  101. this.OnAdLoaded(this, EventArgs.Empty);
  102. }
  103. }
  104. public void onAdFailedToLoad(string errorReason)
  105. {
  106. if (this.OnAdFailedToLoad != null)
  107. {
  108. AdFailedToLoadEventArgs args = new AdFailedToLoadEventArgs()
  109. {
  110. Message = errorReason
  111. };
  112. this.OnAdFailedToLoad(this, args);
  113. }
  114. }
  115. public void onAdOpened()
  116. {
  117. if (this.OnAdOpening != null)
  118. {
  119. this.OnAdOpening(this, EventArgs.Empty);
  120. }
  121. }
  122. public void onAdClosed()
  123. {
  124. if (this.OnAdClosed != null)
  125. {
  126. this.OnAdClosed(this, EventArgs.Empty);
  127. }
  128. }
  129. public void onAdLeftApplication()
  130. {
  131. if (this.OnAdLeavingApplication != null)
  132. {
  133. this.OnAdLeavingApplication(this, EventArgs.Empty);
  134. }
  135. }
  136. #endregion
  137. }
  138. }
  139. #endif