Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.SceneManagement;
- using GoogleMobileAds;
- using GoogleMobileAds.Api;
- using System;
- using AppodealAds.Unity.Api;
- using AppodealAds.Unity.Common;
- public class AdMobScript : MonoBehaviour
- {
- [SerializeField]
- [Tooltip("Вероятность: 2 = 50%; 3 = 33%; 4 = 25%")]
- int Probability = 1;
- private BannerView bannerView;
- private InterstitialAd interstitial;
- private RewardBasedVideoAd rewardBasedVideo;
- private float deltaTime = 0.0f;
- private static string outputMessage = "";
- public static string OutputMessage
- {
- set { outputMessage = value; }
- }
- void Start()
- {
- // Get singleton reward based video ad reference.
- rewardBasedVideo = RewardBasedVideoAd.Instance;
- // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
- rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
- rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
- rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
- rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
- rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
- rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
- rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
- if (PlayerPrefs.GetInt("IsAdmobCancel") != 1)
- {
- if (UnityEngine.Random.Range(0, Probability) == 0)
- {
- //
- //
- //
- //Вот эта строчка ниже делает запрос к admob'у
- // RequestRewardBasedVideo();
- //Эта строчка ВЫШЕ делает запрос к admob'у (да я два раза написал)
- //
- //
- //
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- else
- {
- Destroy(gameObject);
- }
- }
- void Update()
- {
- // Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
- // value.
- deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
- if (SceneManager.GetActiveScene().name == "LoseMenu" || SceneManager.GetActiveScene().name == "LoseMenuScale")
- {
- //
- //
- //
- //Вот эта строчка ниже выводит рекламу
- // ShowRewardBasedVideo();
- //Эта строчка ВЫШЕ выводит рекламу (да я два раза написал)
- //
- //
- //
- Appodeal.show(Appodeal.REWARDED_VIDEO | Appodeal.INTERSTITIAL);
- }
- }
- /*void OnGUI()
- {
- GUIStyle style = new GUIStyle();
- Rect rect = new Rect(0, 0, Screen.width, Screen.height);
- style.alignment = TextAnchor.LowerRight;
- style.fontSize = (int)(Screen.height * 0.06);
- style.normal.textColor = new Color(0.0f, 0.0f, 0.5f, 1.0f);
- float fps = 1.0f / deltaTime;
- string text = string.Format("{0:0.} fps", fps);
- GUI.Label(rect, text, style);
- // Puts some basic buttons onto the screen.
- GUI.skin.button.fontSize = (int)(0.03f * Screen.height);
- Rect requestBannerRect = new Rect(0.1f * Screen.width, 0.05f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(requestBannerRect, "Request Banner"))
- {
- RequestBanner();
- }
- Rect showBannerRect = new Rect(0.1f * Screen.width, 0.175f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(showBannerRect, "Show Banner"))
- {
- bannerView.Show();
- }
- Rect destroyBannerRect = new Rect(0.1f * Screen.width, 0.3f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(destroyBannerRect, "Destroy Banner"))
- {
- bannerView.Destroy();
- }
- Rect requestInterstitialRect = new Rect(0.1f * Screen.width, 0.425f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(requestInterstitialRect, "Request Interstitial"))
- {
- RequestInterstitial();
- }
- Rect showInterstitialRect = new Rect(0.1f * Screen.width, 0.55f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(showInterstitialRect, "Show Interstitial"))
- {
- ShowInterstitial();
- }
- Rect requestRewardedRect = new Rect(0.1f * Screen.width, 0.675f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(requestRewardedRect, "Request Rewarded Video"))
- {
- RequestRewardBasedVideo();
- }
- Rect showRewardedRect = new Rect(0.1f * Screen.width, 0.8f * Screen.height,
- 0.8f * Screen.width, 0.1f * Screen.height);
- if (GUI.Button(showRewardedRect, "Show Rewarded Video"))
- {
- ShowRewardBasedVideo();
- }
- Rect textOutputRect = new Rect(0.1f * Screen.width, 0.925f * Screen.height,
- 0.8f * Screen.width, 0.05f * Screen.height);
- GUI.Label(textOutputRect, outputMessage);
- }*/
- private void RequestBanner()
- {
- #if UNITY_EDITOR
- string adUnitId = "unused";
- #elif UNITY_ANDROID
- string adUnitId = "INSERT_ANDROID_BANNER_AD_UNIT_ID_HERE";
- #elif UNITY_IPHONE
- string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
- #else
- string adUnitId = "unexpected_platform";
- #endif
- // Create a 320x50 banner at the top of the screen.
- bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
- // Register for ad events.
- bannerView.OnAdLoaded += HandleAdLoaded;
- bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
- bannerView.OnAdLoaded += HandleAdOpened;
- bannerView.OnAdClosed += HandleAdClosed;
- bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
- // Load a banner ad.
- bannerView.LoadAd(createAdRequest());
- }
- private void RequestInterstitial()
- {
- #if UNITY_EDITOR
- string adUnitId = "unused";
- //ca-app-pub-7984576479645503/7366847876 - working
- //ca-app-pub-3940256099942544/1033173712 - test
- #elif UNITY_ANDROID
- string adUnitId = "ca-app-pub-7984576479645503/7366847876";
- #elif UNITY_IPHONE
- string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
- #else
- string adUnitId = "unexpected_platform";
- #endif
- // Create an interstitial.
- interstitial = new InterstitialAd(adUnitId);
- // Register for ad events.
- interstitial.OnAdLoaded += HandleInterstitialLoaded;
- interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
- interstitial.OnAdOpening += HandleInterstitialOpened;
- interstitial.OnAdClosed += HandleInterstitialClosed;
- interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
- // Load an interstitial ad.
- interstitial.LoadAd(createAdRequest());
- ShowInterstitial();
- }
- // Returns an ad request with custom ad targeting.
- private AdRequest createAdRequest()
- {
- return new AdRequest.Builder()
- .AddTestDevice(AdRequest.TestDeviceSimulator)
- .AddTestDevice("3C0B89CB651E178B")
- .AddKeyword("game")
- .SetGender(Gender.Male)
- .SetBirthday(new DateTime(1985, 1, 1))
- .TagForChildDirectedTreatment(false)
- .AddExtra("color_bg", "9B30FF")
- .Build();
- }
- private void RequestRewardBasedVideo()
- {
- #if UNITY_EDITOR
- string adUnitId = "unused";
- //ca-app-pub-7984576479645503/7366847876 - working
- //ca-app-pub-3940256099942544/1033173712 - test from google
- #elif UNITY_ANDROID
- string adUnitId = "ca-app-pub-7984576479645503/7366847876";
- #elif UNITY_IPHONE
- string adUnitId = "INSERT_IOS_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
- #else
- string adUnitId = "unexpected_platform";
- #endif
- rewardBasedVideo.LoadAd(createAdRequest(), adUnitId);
- }
- private void ShowInterstitial()
- {
- if (interstitial.IsLoaded())
- {
- interstitial.Show();
- }
- else
- {
- print("Interstitial is not ready yet.");
- }
- Destroy(gameObject);
- }
- private void ShowRewardBasedVideo()
- {
- if (rewardBasedVideo.IsLoaded())
- {
- rewardBasedVideo.Show();
- Destroy(gameObject);
- }
- else
- {
- RequestInterstitial();
- }
- }
- #region Banner callback handlers
- public void HandleAdLoaded(object sender, EventArgs args)
- {
- print("HandleAdLoaded event received.");
- }
- public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
- {
- print("HandleFailedToReceiveAd event received with message: " + args.Message);
- }
- public void HandleAdOpened(object sender, EventArgs args)
- {
- print("HandleAdOpened event received");
- }
- void HandleAdClosing(object sender, EventArgs args)
- {
- print("HandleAdClosing event received");
- }
- public void HandleAdClosed(object sender, EventArgs args)
- {
- print("HandleAdClosed event received");
- }
- public void HandleAdLeftApplication(object sender, EventArgs args)
- {
- print("HandleAdLeftApplication event received");
- }
- #endregion
- #region Interstitial callback handlers
- public void HandleInterstitialLoaded(object sender, EventArgs args)
- {
- print("HandleInterstitialLoaded event received.");
- }
- public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
- {
- print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
- }
- public void HandleInterstitialOpened(object sender, EventArgs args)
- {
- print("HandleInterstitialOpened event received");
- }
- void HandleInterstitialClosing(object sender, EventArgs args)
- {
- print("HandleInterstitialClosing event received");
- }
- public void HandleInterstitialClosed(object sender, EventArgs args)
- {
- print("HandleInterstitialClosed event received");
- }
- public void HandleInterstitialLeftApplication(object sender, EventArgs args)
- {
- print("HandleInterstitialLeftApplication event received");
- }
- #endregion
- #region RewardBasedVideo callback handlers
- public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
- {
- print("HandleRewardBasedVideoLoaded event received.");
- }
- public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
- {
- print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
- }
- public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
- {
- print("HandleRewardBasedVideoOpened event received");
- }
- public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
- {
- print("HandleRewardBasedVideoStarted event received");
- }
- public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
- {
- print("HandleRewardBasedVideoClosed event received");
- }
- public void HandleRewardBasedVideoRewarded(object sender, Reward args)
- {
- string type = args.Type;
- double amount = args.Amount;
- print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
- type);
- }
- public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
- {
- print("HandleRewardBasedVideoLeftApplication event received");
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement