Advertisement
Guest User

Code

a guest
May 3rd, 2016
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Blitz3D 12.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. using GoogleMobileAds;
  5. using GoogleMobileAds.Api;
  6. using System;
  7. using AppodealAds.Unity.Api;
  8. using AppodealAds.Unity.Common;
  9. public class AdMobScript : MonoBehaviour
  10. {
  11.  
  12.  
  13.     [SerializeField]
  14.     [Tooltip("Вероятность: 2 = 50%; 3 = 33%; 4 = 25%")]
  15.     int Probability = 1;
  16.  
  17.     private BannerView bannerView;
  18.     private InterstitialAd interstitial;
  19.     private RewardBasedVideoAd rewardBasedVideo;
  20.     private float deltaTime = 0.0f;
  21.     private static string outputMessage = "";
  22.  
  23.     public static string OutputMessage
  24.     {
  25.         set { outputMessage = value; }
  26.     }
  27.  
  28.     void Start()
  29.     {
  30.  
  31.         // Get singleton reward based video ad reference.
  32.         rewardBasedVideo = RewardBasedVideoAd.Instance;
  33.  
  34.         // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
  35.         rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
  36.         rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
  37.         rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
  38.         rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
  39.         rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
  40.         rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
  41.         rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
  42.         if (PlayerPrefs.GetInt("IsAdmobCancel") != 1)
  43.         {
  44.             if (UnityEngine.Random.Range(0, Probability) == 0)
  45.             {
  46.                 //
  47.                 //
  48.                 //
  49.                 //Вот эта строчка ниже делает запрос к admob'у
  50.                // RequestRewardBasedVideo();
  51.                 //Эта строчка ВЫШЕ делает запрос к admob'у (да я два раза написал)
  52.                 //
  53.                 //
  54.                 //
  55.  
  56.                 DontDestroyOnLoad(gameObject);
  57.             }
  58.             else
  59.             {
  60.                 Destroy(gameObject);
  61.             }
  62.         }
  63.         else
  64.         {
  65.             Destroy(gameObject);
  66.         }
  67.     }
  68.  
  69.  
  70.    
  71.     void Update()
  72.     {
  73.         // Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
  74.         // value.
  75.         deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
  76.         if (SceneManager.GetActiveScene().name == "LoseMenu" || SceneManager.GetActiveScene().name == "LoseMenuScale")
  77.         {
  78.             //
  79.             //
  80.             //
  81.             //Вот эта строчка ниже выводит рекламу
  82.             //  ShowRewardBasedVideo();
  83.             //Эта строчка ВЫШЕ выводит рекламу (да я два раза написал)
  84.             //
  85.             //
  86.             //
  87.             Appodeal.show(Appodeal.REWARDED_VIDEO | Appodeal.INTERSTITIAL);
  88.         }
  89.     }
  90.    
  91.  
  92.     /*void OnGUI()
  93.     {
  94.         GUIStyle style = new GUIStyle();
  95.  
  96.         Rect rect = new Rect(0, 0, Screen.width, Screen.height);
  97.         style.alignment = TextAnchor.LowerRight;
  98.         style.fontSize = (int)(Screen.height * 0.06);
  99.         style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
  100.         float fps = 1.0f / deltaTime;
  101.         string text = string.Format("{0:0.} fps", fps);
  102.         GUI.Label(rect, text, style);
  103.  
  104.         // Puts some basic buttons onto the screen.
  105.         GUI.skin.button.fontSize = (int)(0.03f * Screen.height);
  106.  
  107.         Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
  108.                                      0.8f * Screen.width, 0.1f * Screen.height);
  109.         if (GUI.Button(requestBannerRect, "Request Banner"))
  110.         {
  111.             RequestBanner();
  112.         }
  113.  
  114.         Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
  115.                                   0.8f * Screen.width, 0.1f * Screen.height);
  116.         if (GUI.Button(showBannerRect, "Show Banner"))
  117.         {
  118.             bannerView.Show();
  119.         }
  120.  
  121.         Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
  122.                                      0.8f * Screen.width, 0.1f * Screen.height);
  123.         if (GUI.Button(destroyBannerRect, "Destroy Banner"))
  124.         {
  125.             bannerView.Destroy();
  126.         }
  127.  
  128.         Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
  129.                                            0.8f * Screen.width, 0.1f * Screen.height);
  130.         if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
  131.         {
  132.             RequestInterstitial();
  133.         }
  134.  
  135.         Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
  136.                                         0.8f * Screen.width, 0.1f * Screen.height);
  137.         if (GUI.Button(showInterstitialRect, "Show Interstitial"))
  138.         {
  139.             ShowInterstitial();
  140.         }
  141.  
  142.         Rect requestRewardedRect = new Rect(0.1f * Screen.width, 0.675f * Screen.height,
  143.                                        0.8f * Screen.width, 0.1f * Screen.height);
  144.         if (GUI.Button(requestRewardedRect, "Request Rewarded Video"))
  145.         {
  146.             RequestRewardBasedVideo();
  147.         }
  148.  
  149.         Rect showRewardedRect = new Rect(0.1f * Screen.width, 0.8f * Screen.height,
  150.                                     0.8f * Screen.width, 0.1f * Screen.height);
  151.         if (GUI.Button(showRewardedRect, "Show Rewarded Video"))
  152.         {
  153.             ShowRewardBasedVideo();
  154.         }
  155.  
  156.         Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
  157.                                   0.8f * Screen.width, 0.05f * Screen.height);
  158.         GUI.Label(textOutputRect, outputMessage);
  159.     }*/
  160.  
  161.     private void RequestBanner()
  162.     {
  163. #if UNITY_EDITOR
  164.         string adUnitId = "unused";
  165. #elif UNITY_ANDROID
  166.             string adUnitId = "INSERT_ANDROID_BANNER_AD_UNIT_ID_HERE";
  167. #elif UNITY_IPHONE
  168.             string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
  169. #else
  170.         string adUnitId = "unexpected_platform";
  171. #endif
  172.  
  173.         // Create a 320x50 banner at the top of the screen.
  174.         bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
  175.         // Register for ad events.
  176.         bannerView.OnAdLoaded += HandleAdLoaded;
  177.         bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
  178.         bannerView.OnAdLoaded += HandleAdOpened;
  179.         bannerView.OnAdClosed += HandleAdClosed;
  180.         bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
  181.         // Load a banner ad.
  182.         bannerView.LoadAd(createAdRequest());
  183.     }
  184.  
  185.     private void RequestInterstitial()
  186.     {
  187. #if UNITY_EDITOR
  188.         string adUnitId = "unused";
  189.         //ca-app-pub-7984576479645503/7366847876 - working
  190.         //ca-app-pub-3940256099942544/1033173712 - test
  191. #elif UNITY_ANDROID
  192.             string adUnitId = "ca-app-pub-7984576479645503/7366847876";
  193. #elif UNITY_IPHONE
  194.             string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
  195. #else
  196.         string adUnitId = "unexpected_platform";
  197. #endif
  198.  
  199.         // Create an interstitial.
  200.         interstitial = new InterstitialAd(adUnitId);
  201.         // Register for ad events.
  202.         interstitial.OnAdLoaded += HandleInterstitialLoaded;
  203.         interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
  204.         interstitial.OnAdOpening += HandleInterstitialOpened;
  205.         interstitial.OnAdClosed += HandleInterstitialClosed;
  206.         interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
  207.         // Load an interstitial ad.
  208.  
  209.         interstitial.LoadAd(createAdRequest());
  210.  
  211.         ShowInterstitial();
  212.     }
  213.  
  214.     // Returns an ad request with custom ad targeting.
  215.     private AdRequest createAdRequest()
  216.     {
  217.         return new AdRequest.Builder()
  218.                 .AddTestDevice(AdRequest.TestDeviceSimulator)
  219.                 .AddTestDevice("3C0B89CB651E178B")
  220.  
  221.                 .AddKeyword("game")
  222.                 .SetGender(Gender.Male)
  223.                 .SetBirthday(new DateTime(1985, 1, 1))
  224.                 .TagForChildDirectedTreatment(false)
  225.                 .AddExtra("color_bg", "9B30FF")
  226.                 .Build();
  227.     }
  228.  
  229.     private void RequestRewardBasedVideo()
  230.     {
  231. #if UNITY_EDITOR
  232.         string adUnitId = "unused";
  233.         //ca-app-pub-7984576479645503/7366847876 - working
  234.         //ca-app-pub-3940256099942544/1033173712 - test from google
  235. #elif UNITY_ANDROID
  236.             string adUnitId = "ca-app-pub-7984576479645503/7366847876";
  237. #elif UNITY_IPHONE
  238.             string adUnitId = "INSERT_IOS_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
  239. #else
  240.         string adUnitId = "unexpected_platform";
  241. #endif
  242.  
  243.         rewardBasedVideo.LoadAd(createAdRequest(), adUnitId);
  244.     }
  245.  
  246.     private void ShowInterstitial()
  247.     {
  248.         if (interstitial.IsLoaded())
  249.         {
  250.             interstitial.Show();
  251.         }
  252.         else
  253.         {
  254.             print("Interstitial is not ready yet.");
  255.         }
  256.         Destroy(gameObject);
  257.     }
  258.  
  259.     private void ShowRewardBasedVideo()
  260.     {
  261.         if (rewardBasedVideo.IsLoaded())
  262.         {
  263.             rewardBasedVideo.Show();
  264.             Destroy(gameObject);
  265.         }
  266.         else
  267.         {
  268.             RequestInterstitial();
  269.         }
  270.     }
  271.  
  272.     #region Banner callback handlers
  273.  
  274.     public void HandleAdLoaded(object sender, EventArgs args)
  275.     {
  276.         print("HandleAdLoaded event received.");
  277.     }
  278.  
  279.     public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
  280.     {
  281.         print("HandleFailedToReceiveAd event received with message: " + args.Message);
  282.     }
  283.  
  284.     public void HandleAdOpened(object sender, EventArgs args)
  285.     {
  286.         print("HandleAdOpened event received");
  287.     }
  288.  
  289.     void HandleAdClosing(object sender, EventArgs args)
  290.     {
  291.         print("HandleAdClosing event received");
  292.     }
  293.  
  294.     public void HandleAdClosed(object sender, EventArgs args)
  295.     {
  296.         print("HandleAdClosed event received");
  297.     }
  298.  
  299.     public void HandleAdLeftApplication(object sender, EventArgs args)
  300.     {
  301.         print("HandleAdLeftApplication event received");
  302.     }
  303.  
  304.     #endregion
  305.  
  306.     #region Interstitial callback handlers
  307.  
  308.     public void HandleInterstitialLoaded(object sender, EventArgs args)
  309.     {
  310.         print("HandleInterstitialLoaded event received.");
  311.     }
  312.  
  313.     public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
  314.     {
  315.         print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
  316.     }
  317.  
  318.     public void HandleInterstitialOpened(object sender, EventArgs args)
  319.     {
  320.         print("HandleInterstitialOpened event received");
  321.     }
  322.  
  323.     void HandleInterstitialClosing(object sender, EventArgs args)
  324.     {
  325.         print("HandleInterstitialClosing event received");
  326.     }
  327.  
  328.     public void HandleInterstitialClosed(object sender, EventArgs args)
  329.     {
  330.         print("HandleInterstitialClosed event received");
  331.     }
  332.  
  333.     public void HandleInterstitialLeftApplication(object sender, EventArgs args)
  334.     {
  335.         print("HandleInterstitialLeftApplication event received");
  336.     }
  337.  
  338.     #endregion
  339.  
  340.     #region RewardBasedVideo callback handlers
  341.  
  342.     public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
  343.     {
  344.         print("HandleRewardBasedVideoLoaded event received.");
  345.     }
  346.  
  347.     public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
  348.     {
  349.         print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
  350.     }
  351.  
  352.     public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
  353.     {
  354.         print("HandleRewardBasedVideoOpened event received");
  355.     }
  356.  
  357.     public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
  358.     {
  359.         print("HandleRewardBasedVideoStarted event received");
  360.     }
  361.  
  362.     public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
  363.     {
  364.         print("HandleRewardBasedVideoClosed event received");
  365.     }
  366.  
  367.     public void HandleRewardBasedVideoRewarded(object sender, Reward args)
  368.     {
  369.         string type = args.Type;
  370.         double amount = args.Amount;
  371.         print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
  372.                 type);
  373.     }
  374.  
  375.     public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
  376.     {
  377.         print("HandleRewardBasedVideoLeftApplication event received");
  378.     }
  379.  
  380.     #endregion
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement