|
3 | 3 | using GoogleMobileAds; |
4 | 4 | using GoogleMobileAds.Api; |
5 | 5 |
|
| 6 | +public class GoogleMobileAdsDemoHandler : IInAppPurchaseHandler |
| 7 | +{ |
| 8 | + private readonly string[] validSkus = { "android.test.purchased" }; |
| 9 | + |
| 10 | + //Will only be sent on a success. |
| 11 | + public void OnInAppPurchaseFinished(IInAppPurchaseResult result) |
| 12 | + { |
| 13 | + result.FinishPurchase(); |
| 14 | + GoogleMobileAdsDemoScript.OutputMessage = "Purchase Succeeded! Credit user here."; |
| 15 | + } |
| 16 | + |
| 17 | + //Check SKU against valid SKUs. |
| 18 | + public bool IsValidPurchase(string sku) |
| 19 | + { |
| 20 | + foreach(string validSku in validSkus) { |
| 21 | + if (sku == validSku) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + } |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + //Return the app's public key. |
| 29 | + public string AndroidPublicKey |
| 30 | + { |
| 31 | + //In a real app, return public key instead of null. |
| 32 | + get { return null; } |
| 33 | + } |
| 34 | +} |
| 35 | + |
6 | 36 | // Example script showing how to invoke the Google Mobile Ads Unity plugin. |
7 | 37 | public class GoogleMobileAdsDemoScript : MonoBehaviour |
8 | 38 | { |
9 | 39 |
|
10 | 40 | private BannerView bannerView; |
11 | 41 | private InterstitialAd interstitial; |
| 42 | + private static string outputMessage = ""; |
| 43 | + |
| 44 | + public static string OutputMessage |
| 45 | + { |
| 46 | + set { outputMessage = value; } |
| 47 | + } |
12 | 48 |
|
13 | 49 | void OnGUI() |
14 | 50 | { |
15 | 51 | // Puts some basic buttons onto the screen. |
16 | 52 | GUI.skin.button.fontSize = (int) (0.05f * Screen.height); |
| 53 | + GUI.skin.label.fontSize = (int) (0.025f * Screen.height); |
17 | 54 |
|
18 | 55 | Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height, |
19 | 56 | 0.8f * Screen.width, 0.1f * Screen.height); |
@@ -63,6 +100,10 @@ void OnGUI() |
63 | 100 | { |
64 | 101 | interstitial.Destroy(); |
65 | 102 | } |
| 103 | + |
| 104 | + Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height, |
| 105 | + 0.8f * Screen.width, 0.05f * Screen.height); |
| 106 | + GUI.Label(textOutputRect, outputMessage); |
66 | 107 | } |
67 | 108 |
|
68 | 109 | private void RequestBanner() |
@@ -111,6 +152,8 @@ private void RequestInterstitial() |
111 | 152 | interstitial.AdClosing += HandleInterstitialClosing; |
112 | 153 | interstitial.AdClosed += HandleInterstitialClosed; |
113 | 154 | interstitial.AdLeftApplication += HandleInterstitialLeftApplication; |
| 155 | + GoogleMobileAdsDemoHandler handler = new GoogleMobileAdsDemoHandler(); |
| 156 | + interstitial.SetInAppPurchaseHandler(handler); |
114 | 157 | // Load an interstitial ad. |
115 | 158 | interstitial.LoadAd(createAdRequest()); |
116 | 159 | } |
|
0 commit comments