您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 

93 行
3.3 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 System.Collections.Generic;
  17. using UnityEngine;
  18. using GoogleMobileAds.Api;
  19. using GoogleMobileAds.Common;
  20. namespace GoogleMobileAds.Android
  21. {
  22. public class AdLoaderClient : AndroidJavaProxy, IAdLoaderClient
  23. {
  24. private AndroidJavaObject adLoader;
  25. private Dictionary<string, Action<CustomNativeTemplateAd, string>> CustomNativeTemplateCallbacks
  26. {
  27. get; set;
  28. }
  29. public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
  30. public event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded;
  31. public AdLoaderClient(AdLoader unityAdLoader) : base(Utils.UnityAdLoaderListenerClassName)
  32. {
  33. AndroidJavaClass playerClass = new AndroidJavaClass(Utils.UnityActivityClassName);
  34. AndroidJavaObject activity =
  35. playerClass.GetStatic<AndroidJavaObject>("currentActivity");
  36. adLoader = new AndroidJavaObject(Utils.NativeAdLoaderClassName, activity,
  37. unityAdLoader.AdUnitId, this);
  38. this.CustomNativeTemplateCallbacks = unityAdLoader.CustomNativeTemplateClickHandlers;
  39. if (unityAdLoader.AdTypes.Contains(NativeAdType.CustomTemplate))
  40. {
  41. foreach (string templateId in unityAdLoader.TemplateIds)
  42. {
  43. adLoader.Call("configureCustomNativeTemplateAd", templateId,
  44. this.CustomNativeTemplateCallbacks.ContainsKey(templateId));
  45. }
  46. }
  47. adLoader.Call("create");
  48. }
  49. public void LoadAd(AdRequest request)
  50. {
  51. adLoader.Call("loadAd", Utils.GetAdRequestJavaObject(request));
  52. }
  53. public void onCustomTemplateAdLoaded(AndroidJavaObject ad)
  54. {
  55. if (this.OnCustomNativeTemplateAdLoaded != null)
  56. {
  57. CustomNativeEventArgs args = new CustomNativeEventArgs()
  58. {
  59. nativeAd = new CustomNativeTemplateAd(new CustomNativeTemplateClient(ad))
  60. };
  61. this.OnCustomNativeTemplateAdLoaded(this, args);
  62. }
  63. }
  64. void onAdFailedToLoad(string errorReason)
  65. {
  66. AdFailedToLoadEventArgs args = new AdFailedToLoadEventArgs()
  67. {
  68. Message = errorReason
  69. };
  70. OnAdFailedToLoad(this, args);
  71. }
  72. public void onCustomClick(AndroidJavaObject ad, string assetName)
  73. {
  74. CustomNativeTemplateAd nativeAd = new CustomNativeTemplateAd(
  75. new CustomNativeTemplateClient(ad));
  76. this.CustomNativeTemplateCallbacks[nativeAd.GetCustomTemplateId()](nativeAd, assetName);
  77. }
  78. }
  79. }
  80. #endif