Advertisement
Guest User

Untitled

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