|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | +using GoogleMobileAds.Api; |
| 5 | + |
| 6 | +namespace GoogleMobileAds.Snippets |
| 7 | +{ |
| 8 | + internal class RewardedAdSnippets |
| 9 | + { |
| 10 | +#if UNITY_ANDROID |
| 11 | + private const string AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917"; |
| 12 | +#elif UNITY_IPHONE |
| 13 | + private const string AD_UNIT_ID = "ca-app-pub-3940256099942544/1712485313"; |
| 14 | +#else |
| 15 | + private const string AD_UNIT_ID = "unused"; |
| 16 | +#endif |
| 17 | + |
| 18 | + void LoadAd() |
| 19 | + { |
| 20 | + // [START load_ad] |
| 21 | + // Create our request used to load the ad. |
| 22 | + var adRequest = new AdRequest(); |
| 23 | + |
| 24 | + // Send the request to load the ad. |
| 25 | + RewardedAd.Load(AD_UNIT_ID, adRequest, (RewardedAd ad, LoadAdError error) => |
| 26 | + { |
| 27 | + if (error != null) |
| 28 | + { |
| 29 | + // The ad failed to load. |
| 30 | + return; |
| 31 | + } |
| 32 | + // The ad loaded successfully. |
| 33 | + }); |
| 34 | + // [END load_ad] |
| 35 | + } |
| 36 | + |
| 37 | + void ServerSideVerification(RewardedAd rewardedAd) |
| 38 | + { |
| 39 | + // [START ssv] |
| 40 | + // Create and pass the SSV options to the rewarded ad. |
| 41 | + var options = new ServerSideVerificationOptions |
| 42 | + { |
| 43 | + CustomData = "SAMPLE_CUSTOM_DATA_STRING" |
| 44 | + }; |
| 45 | + |
| 46 | + rewardedAd.SetServerSideVerificationOptions(options); |
| 47 | + // [END ssv] |
| 48 | + } |
| 49 | + |
| 50 | + void ShowAd(RewardedAd rewardedAd) |
| 51 | + { |
| 52 | + // [START show_ad] |
| 53 | + if (rewardedAd != null && rewardedAd.CanShowAd()) |
| 54 | + { |
| 55 | + rewardedAd.Show((Reward reward) => |
| 56 | + { |
| 57 | + // The ad was showen and the user earned a reward. |
| 58 | + }); |
| 59 | + } |
| 60 | + // [END show_ad]] |
| 61 | + } |
| 62 | + |
| 63 | + void ListenToAdEvents(RewardedAd rewardedAd) |
| 64 | + { |
| 65 | + // [START ad_events] |
| 66 | + rewardedAd.OnAdPaid += (AdValue adValue) => |
| 67 | + { |
| 68 | + // Raised when the ad is estimated to have earned money. |
| 69 | + }; |
| 70 | + rewardedAd.OnAdImpressionRecorded += () => |
| 71 | + { |
| 72 | + // Raised when an impression is recorded for an ad. |
| 73 | + }; |
| 74 | + rewardedAd.OnAdClicked += () => |
| 75 | + { |
| 76 | + // Raised when a click is recorded for an ad. |
| 77 | + }; |
| 78 | + rewardedAd.OnAdFullScreenContentOpened += () => |
| 79 | + { |
| 80 | + // Raised when the ad opened full screen content. |
| 81 | + }; |
| 82 | + rewardedAd.OnAdFullScreenContentClosed += () => |
| 83 | + { |
| 84 | + // Raised when the ad closed full screen content. |
| 85 | + }; |
| 86 | + rewardedAd.OnAdFullScreenContentFailed += (AdError error) => |
| 87 | + { |
| 88 | + // Raised when the ad failed to open full screen content. |
| 89 | + }; |
| 90 | + // [END ad_events]] |
| 91 | + } |
| 92 | + |
| 93 | + void DestroyAd(RewardedAd rewardedAd) |
| 94 | + { |
| 95 | + // [START destroy_ad] |
| 96 | + if (rewardedAd != null) |
| 97 | + { |
| 98 | + rewardedAd.Destroy(); |
| 99 | + } |
| 100 | + // [END destroy_ad]] |
| 101 | + } |
| 102 | + |
| 103 | + void ReloadAd(RewardedAd rewardedAd) |
| 104 | + { |
| 105 | + // [START reload_ad] |
| 106 | + rewardedAd.OnAdFullScreenContentClosed += () => |
| 107 | + { |
| 108 | + // Reload the ad so that we can show another as soon as possible. |
| 109 | + var adRequest = new AdRequest(); |
| 110 | + RewardedAd.Load(AD_UNIT_ID, adRequest, (RewardedAd ad, LoadAdError error) => |
| 111 | + { |
| 112 | + // Handle ad loading here. |
| 113 | + }); |
| 114 | + }; |
| 115 | + // [END reload_ad]] |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments