Created
July 18, 2024 06:36
-
-
Save shohan4556/62faf9718ccc262509bec677cf0af6aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using GoogleMobileAds.Api; | |
namespace GlobalScripts | |
{ | |
public class SholoGutiAdsManager : MonoBehaviour | |
{ | |
private BannerView bannerView; | |
private InterstitialAd interstitialAd; | |
private RewardedAd rewardedAd; | |
public bool isAddLoaded; | |
/// <summary> | |
/// Test AD ID : ca-app-pub-3940256099942544/6300978111 | |
/// Real Ad ID : ca-app-pub-2151861431834316/9283212931 | |
/// </summary> | |
#if UNITY_ANDROID | |
private string Banner_adUnitId = "ca-app-pub-2151861431834316/9283212931"; | |
#elif UNITY_IPHONE | |
private string Banner_adUnitId = "ca-app-pub-3940256099942544/2934735716"; | |
#else | |
private string Banner_adUnitId = "unused"; | |
#endif | |
BannerView _bannerView; | |
public static SholoGutiAdsManager Instance; | |
/// <summary> | |
/// Creates a 320x50 banner view at top of the screen. | |
/// </summary> | |
public void CreateBannerView() | |
{ | |
Debug.Log("Creating banner view"); | |
// If we already have a banner, destroy the old one. | |
if (_bannerView != null) | |
{ | |
DestroyBannerView(); | |
} | |
// Create a 320x50 banner at top of the screen | |
_bannerView = new BannerView(Banner_adUnitId, AdSize.Banner, AdPosition.Bottom); | |
} | |
public void Start() | |
{ | |
if(Instance == null) | |
{ | |
Instance = this; | |
} | |
else | |
{ | |
Destroy(Instance); | |
} | |
DontDestroyOnLoad(Instance); | |
// Initialize the Google Mobile Ads SDK. | |
MobileAds.Initialize((InitializationStatus initStatus) => | |
{ | |
print("Ads Loaded"); | |
CreateBannerView(); | |
isAddLoaded = true; | |
}); | |
} | |
public void LoadAd() | |
{ | |
// create an instance of a banner view first. | |
if (_bannerView == null) | |
{ | |
CreateBannerView(); | |
} | |
// create our request used to load the ad. | |
var adRequest = new AdRequest(); | |
// send the request to load the ad. | |
Debug.Log("Loading banner ad."); | |
_bannerView.LoadAd(adRequest); | |
} | |
// <summary> | |
/// listen to events the banner view may raise. | |
/// </summary> | |
private void ListenToAdEvents() | |
{ | |
// Raised when an ad is loaded into the banner view. | |
_bannerView.OnBannerAdLoaded += () => | |
{ | |
Debug.Log("Banner view loaded an ad with response : " | |
+ _bannerView.GetResponseInfo()); | |
}; | |
// Raised when an ad fails to load into the banner view. | |
_bannerView.OnBannerAdLoadFailed += (LoadAdError error) => | |
{ | |
Debug.LogError("Banner view failed to load an ad with error : " | |
+ error); | |
}; | |
// Raised when the ad is estimated to have earned money. | |
_bannerView.OnAdPaid += (AdValue adValue) => | |
{ | |
Debug.Log(String.Format("Banner view paid {0} {1}.", | |
adValue.Value, | |
adValue.CurrencyCode)); | |
}; | |
// Raised when an impression is recorded for an ad. | |
_bannerView.OnAdImpressionRecorded += () => | |
{ | |
Debug.Log("Banner view recorded an impression."); | |
}; | |
// Raised when a click is recorded for an ad. | |
_bannerView.OnAdClicked += () => | |
{ | |
Debug.Log("Banner view was clicked."); | |
}; | |
// Raised when an ad opened full screen content. | |
_bannerView.OnAdFullScreenContentOpened += () => | |
{ | |
Debug.Log("Banner view full screen content opened."); | |
}; | |
// Raised when the ad closed full screen content. | |
_bannerView.OnAdFullScreenContentClosed += () => | |
{ | |
Debug.Log("Banner view full screen content closed."); | |
}; | |
} | |
/// <summary> | |
/// Destroys the banner view. | |
/// </summary> | |
public void DestroyBannerView() | |
{ | |
if (_bannerView != null) | |
{ | |
Debug.Log("Destroying banner view."); | |
_bannerView.Destroy(); | |
_bannerView = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment