unity---接入Admob

Posted 格拉格拉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity---接入Admob相关的知识,希望对你有一定的参考价值。

目录

1.Admob SDK 下载地址

2.将下载好的unityPackage sdk导入到unity里

​编辑

 3.解析依赖到项目中

4.设置admob app ID

 5.android 测试Id

 6.IOS 测试ID

 7.测试 app ID

8. SDK初始化与使用示例代码

9.gradle 配置

10. gradle 下载地址


1.Admob SDK 下载地址

Admob SDK 下载地址

2.将下载好的unityPackage sdk导入到unity里

在 Unity 编辑器中打开您的项目,然后依次选择 Assets > Import Package > Custom Package,并找到您下载的 GoogleMobileAdsPlugin.unitypackage 文件。

 3.解析依赖到项目中

在 Unity 编辑器中,依次选择 Assets > External Dependency Manager > Android Resolver > Resolve。Unity 外部依赖项管理器库会将声明的依赖项复制到 Unity 应用的 Assets/Plugins/android 目录中。

4.设置admob app ID

在 Unity 编辑器中,从菜单中依次选择 Assets > Google Mobile Ads > Settings

 5.android 测试Id

 6.ios 测试ID

 7.测试 app ID

ca-app-pub-3940256099942544~3347511713

8. SDK初始化与使用示例代码

public class GoogleAdMobController : MonoBehaviour

    private readonly TimeSpan APPOPEN_TIMEOUT = TimeSpan.FromHours(4);
    private DateTime appOpenExpireTime;
    private AppOpenAd appOpenAd;
    private BannerView bannerView;
    private InterstitialAd interstitialAd;
    private RewardedAd rewardedAd;
    private RewardedInterstitialAd rewardedInterstitialAd;
    private float deltaTime;
    private bool isShowingAppOpenAd;
    public UnityEvent OnAdLoadedEvent;
    public UnityEvent OnAdFailedToLoadEvent;
    public UnityEvent OnAdOpeningEvent;
    public UnityEvent OnAdFailedToShowEvent;
    public UnityEvent OnUserEarnedRewardEvent;
    public UnityEvent OnAdClosedEvent;
    public bool showFpsMeter = true;
    public Text fpsMeter;
    public Text statusText;


    #region UNITY MONOBEHAVIOR METHODS

    public void Start()
    
        MobileAds.SetiOSAppPauseOnBackground(true);

        List<String> deviceIds = new List<String>()  AdRequest.TestDeviceSimulator ;

#if UNITY_IPHONE
        deviceIds.Add("96e23e80653bb28980d3f40beb58915c");
#elif UNITY_ANDROID
        deviceIds.Add("75EF8D155528C04DACBBA6F36F433035");
#endif

        RequestConfiguration requestConfiguration =
            new RequestConfiguration.Builder()
            .SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
            .SetTestDeviceIds(deviceIds).build();
        MobileAds.SetRequestConfiguration(requestConfiguration);

        MobileAds.Initialize(HandleInitCompleteAction);

        AppStateEventNotifier.AppStateChanged += OnAppStateChanged;
    

    private void HandleInitCompleteAction(InitializationStatus initstatus)
    
        Debug.Log("Initialization complete.");
        MobileAdsEventExecutor.ExecuteInUpdate(() =>
        
            statusText.text = "Initialization complete.";
            RequestBannerAd();
        );
    

    private void Update()
    
        if (showFpsMeter)
        
            fpsMeter.gameObject.SetActive(true);
            deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
            float fps = 1.0f / deltaTime;
            fpsMeter.text = string.Format("0:0. fps", fps);
        
        else
        
            fpsMeter.gameObject.SetActive(false);
        
    

    #endregion

    #region HELPER METHODS

    private AdRequest CreateAdRequest()
    
        return new AdRequest.Builder()
            .AddKeyword("unity-admob-sample")
            .Build();
    

    #endregion

    #region BANNER ADS

    public void RequestBannerAd()
    
        PrintStatus("Requesting Banner ad.");

#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
        string adUnitId = "unexpected_platform";
#endif

        if (bannerView != null)
        
            bannerView.Destroy();
        

        bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);

        bannerView.OnAdLoaded += (sender, args) =>
        
            PrintStatus("Banner ad loaded.");
            OnAdLoadedEvent.Invoke();
        ;
        bannerView.OnAdFailedToLoad += (sender, args) =>
        
            PrintStatus("Banner ad failed to load with error: " + args.LoadAdError.GetMessage());
            OnAdFailedToLoadEvent.Invoke();
        ;
        bannerView.OnAdOpening += (sender, args) =>
        
            PrintStatus("Banner ad opening.");
            OnAdOpeningEvent.Invoke();
        ;
        bannerView.OnAdClosed += (sender, args) =>
        
            PrintStatus("Banner ad closed.");
            OnAdClosedEvent.Invoke();
        ;
        bannerView.OnPaidEvent += (sender, args) =>
        
            string msg = string.Format("0 (currency: 1, value: 2",
                                        "Banner ad received a paid event.",
                                        args.AdValue.CurrencyCode,
                                        args.AdValue.Value);
            PrintStatus(msg);
        ;

        // Load a banner ad
        bannerView.LoadAd(CreateAdRequest());
    

    public void DestroyBannerAd()
    
        if (bannerView != null)
        
            bannerView.Destroy();
        
    

    #endregion

    #region INTERSTITIAL ADS

    public void RequestAndLoadInterstitialAd()
    
        PrintStatus("Requesting Interstitial ad.");

#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Clean up interstitial before using it
        if (interstitialAd != null)
        
            interstitialAd.Destroy();
        

        interstitialAd = new InterstitialAd(adUnitId);

        // Add Event Handlers
        interstitialAd.OnAdLoaded += (sender, args) =>
        
            PrintStatus("Interstitial ad loaded.");
            OnAdLoadedEvent.Invoke();
        ;
        interstitialAd.OnAdFailedToLoad += (sender, args) =>
        
            PrintStatus("Interstitial ad failed to load with error: " + args.LoadAdError.GetMessage());
            OnAdFailedToLoadEvent.Invoke();
        ;
        interstitialAd.OnAdOpening += (sender, args) =>
        
            PrintStatus("Interstitial ad opening.");
            OnAdOpeningEvent.Invoke();
        ;
        interstitialAd.OnAdClosed += (sender, args) =>
        
            PrintStatus("Interstitial ad closed.");
            OnAdClosedEvent.Invoke();
        ;
        interstitialAd.OnAdDidRecordImpression += (sender, args) =>
        
            PrintStatus("Interstitial ad recorded an impression.");
        ;
        interstitialAd.OnAdFailedToShow += (sender, args) =>
        
            PrintStatus("Interstitial ad failed to show.");
        ;
        interstitialAd.OnPaidEvent += (sender, args) =>
        
            string msg = string.Format("0 (currency: 1, value: 2",
                                        "Interstitial ad received a paid event.",
                                        args.AdValue.CurrencyCode,
                                        args.AdValue.Value);
            PrintStatus(msg);
        ;

        // Load an interstitial ad
        interstitialAd.LoadAd(CreateAdRequest());
    

    public void ShowInterstitialAd()
    
        if (interstitialAd != null && interstitialAd.IsLoaded())
        
            interstitialAd.Show();
        
        else
        
            PrintStatus("Interstitial ad is not ready yet.");
        
    

    public void DestroyInterstitialAd()
    
        if (interstitialAd != null)
        
            interstitialAd.Destroy();
        
    

    #endregion

    #region REWARDED ADS

    public void RequestAndLoadRewardedAd()
    
        PrintStatus("Requesting Rewarded ad.");
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
        string adUnitId = "unexpected_platform";
#endif

        // create new rewarded ad instance
        rewardedAd = new RewardedAd(adUnitId);

        // Add Event Handlers
        rewardedAd.OnAdLoaded += (sender, args) =>
        
            PrintStatus("Reward ad loaded.");
            OnAdLoadedEvent.Invoke();
        ;
        rewardedAd.OnAdFailedToLoad += (sender, args) =>
        
            PrintStatus("Reward ad failed to load.");
            OnAdFailedToLoadEvent.Invoke();
        ;
        rewardedAd.OnAdOpening += (sender, args) =>
        
            PrintStatus("Reward ad opening.");
            OnAdOpeningEvent.Invoke();
        ;
        rewardedAd.OnAdFailedToShow += (sender, args) =>
        
            PrintStatus("Reward ad failed to show with error: " + args.AdError.GetMessage());
            OnAdFailedToShowEvent.Invoke();
        ;
        rewardedAd.OnAdClosed += (sender, args) =>
        
            PrintStatus("Reward ad closed.");
            OnAdClosedEvent.Invoke();
        ;
        rewardedAd.OnUserEarnedReward += (sender, args) =>
        
            PrintStatus("User earned Reward ad reward: " + args.Amount);
            OnUserEarnedRewardEvent.Invoke();
        ;
        rewardedAd.OnAdDidRecordImpression += (sender, args) =>
        
            PrintStatus("Reward ad recorded an impression.");
        ;
        rewardedAd.OnPaidEvent += (sender, args) =>
        
            string msg = string.Format("0 (currency: 1, value: 2",
                                        "Rewarded ad received a paid event.",
                                        args.AdValue.CurrencyCode,
                                        args.AdValue.Value);
            PrintStatus(msg);
        ;

        // Create empty ad request
        rewardedAd.LoadAd(CreateAdRequest());
    

    public void ShowRewardedAd()
    
        if (rewardedAd != null)
        
            rewardedAd.Show();
        
        else
        
            PrintStatus("Rewarded ad is not ready yet.");
        
    

    public void RequestAndLoadRewardedInterstitialAd()
    
        PrintStatus("Requesting Rewarded Interstitial ad.");

        // These ad units are configured to always serve test ads.
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
            string adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
            string adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
        string adUnitId = "unexpected_platform";
#endif

        // Create an interstitial.
        RewardedInterstitialAd.LoadAd(adUnitId, CreateAdRequest(), (rewardedInterstitialAd, error) =>
        
            if (error != null)
            
                PrintStatus("Rewarded Interstitial ad load failed with error: " + error);
                return;
            

            this.rewardedInterstitialAd = rewardedInterstitialAd;
            PrintStatus("Rewarded Interstitial ad loaded.");

            // Register for ad events.
            this.rewardedInterstitialAd.OnAdDidPresentFullScreenContent += (sender, args) =>
            
                PrintStatus("Rewarded Interstitial ad presented.");
            ;
            this.rewardedInterstitialAd.OnAdDidDismissFullScreenContent += (sender, args) =>
            
                PrintStatus("Rewarded Interstitial ad dismissed.");
                this.rewardedInterstitialAd = null;
            ;
            this.rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent += (sender, args) =>
            
                PrintStatus("Rewarded Interstitial ad failed to present with error: " +
                                                                        args.AdError.GetMessage());
                this.rewardedInterstitialAd = null;
            ;
            this.rewardedInterstitialAd.OnPaidEvent += (sender, args) =>
            
                string msg = string.Format("0 (currency: 1, value: 2",
                                            "Rewarded Interstitial ad received a paid event.",
                                            args.AdValue.CurrencyCode,
                                            args.AdValue.Value);
                PrintStatus(msg);
            ;
            this.rewardedInterstitialAd.OnAdDidRecordImpression += (sender, args) =>
            
                PrintStatus("Rewarded Interstitial ad recorded an impression.");
            ;
        );
    

    public void ShowRewardedInterstitialAd()
    
        if (rewardedInterstitialAd != null)
        
            rewardedInterstitialAd.Show((reward) =>
            
                PrintStatus("Rewarded Interstitial ad Rewarded : " + reward.Amount);
            );
        
        else
        
            PrintStatus("Rewarded Interstitial ad is not ready yet.");
        
    

    #endregion

    #region APPOPEN ADS

    public bool IsAppOpenAdAvailable
    
        get
        
            return (!isShowingAppOpenAd
                    && appOpenAd != null
                    && DateTime.Now < appOpenExpireTime);
        
    

    public void OnAppStateChanged(AppState state)
    
        // Display the app open ad when the app is foregrounded.
        UnityEngine.Debug.Log("App State is " + state);

        // OnAppStateChanged is not guaranteed to execute on the Unity UI thread.
        MobileAdsEventExecutor.ExecuteInUpdate(() =>
        
            if (state == AppState.Foreground)
            
                ShowAppOpenAd();
            
        );
    

    public void RequestAndLoadAppOpenAd()
    
        PrintStatus("Requesting App Open ad.");
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-3940256099942544/3419835294";
#elif UNITY_IPHONE
        string adUnitId = "ca-app-pub-3940256099942544/5662855259";
#else
        string adUnitId = "unexpected_platform";
#endif
        // create new app open ad instance
        AppOpenAd.LoadAd(adUnitId,
                         ScreenOrientation.Portrait,
                         CreateAdRequest(),
                         OnAppOpenAdLoad);
    

    private void OnAppOpenAdLoad(AppOpenAd ad, AdFailedToLoadEventArgs error)
    
        if (error != null)
        
            PrintStatus("App Open ad failed to load with error: " + error);
            return;
        

        PrintStatus("App Open ad loaded. Please background the app and return.");
        this.appOpenAd = ad;
        this.appOpenExpireTime = DateTime.Now + APPOPEN_TIMEOUT;
    

    public void ShowAppOpenAd()
    
        if (!IsAppOpenAdAvailable)
        
            return;
        

        // Register for ad events.
        this.appOpenAd.OnAdDidDismissFullScreenContent += (sender, args) =>
        
            PrintStatus("App Open ad dismissed.");
            isShowingAppOpenAd = false;
            if (this.appOpenAd != null)
            
                this.appOpenAd.Destroy();
                this.appOpenAd = null;
            
        ;
        this.appOpenAd.OnAdFailedToPresentFullScreenContent += (sender, args) =>
        
            PrintStatus("App Open ad failed to present with error: " + args.AdError.GetMessage());

            isShowingAppOpenAd = false;
            if (this.appOpenAd != null)
            
                this.appOpenAd.Destroy();
                this.appOpenAd = null;
            
        ;
        this.appOpenAd.OnAdDidPresentFullScreenContent += (sender, args) =>
        
            PrintStatus("App Open ad opened.");
        ;
        this.appOpenAd.OnAdDidRecordImpression += (sender, args) =>
        
            PrintStatus("App Open ad recorded an impression.");
        ;
        this.appOpenAd.OnPaidEvent += (sender, args) =>
        
            string msg = string.Format("0 (currency: 1, value: 2",
                                        "App Open ad received a paid event.",
                                        args.AdValue.CurrencyCode,
                                        args.AdValue.Value);
            PrintStatus(msg);
        ;

        isShowingAppOpenAd = true;
        appOpenAd.Show();
    

    #endregion


    #region AD INSPECTOR

    public void OpenAdInspector()
    
        PrintStatus("Open ad Inspector.");

        MobileAds.OpenAdInspector((error) =>
        
            if (error != null)
            
                PrintStatus("ad Inspector failed to open with error: " + error);
            
            else
            
                PrintStatus("Ad Inspector opened successfully.");
            
        );
    

    #endregion

    #region Utility

    ///<summary>
    /// Log the message and update the status text on the main thread.
    ///<summary>
    private void PrintStatus(string message)
    
        Debug.Log(message);
        MobileAdsEventExecutor.ExecuteInUpdate(() =>
        
            statusText.text = message;
        );
    

    #endregion

9.gradle 配置

allprojects 
    repositories 
        maven  url 'https://dl.google.com/dl/android/maven2/' 
        google()
        maven  url "https://jitpack.io" 
        maven  url 'https://maven.aliyun.com/repository/jcenter' 
        jcenter()
       
        flatDir 
            dirs 'libs'
        
        mavenLocal()
        maven 
            url "http://maven.aliyun.com/nexus/content/repositories/releases"
        

    





repositories 
        flatDir 
            dirs 'libs'
        
    


/
dependencies 
    ...
    //引入aar
    api(name: 'xxx', ext: 'aar')
 

10. gradle 下载地址

unity 升级gradle版本

以上是关于unity---接入Admob的主要内容,如果未能解决你的问题,请参考以下文章

AppLovin接入笔记

Unity3D - AdMob在Android中的回调造成崩溃的处理方式

Unity3D - AdMob在Android中的回调造成崩溃的处理方式

怎么在unity3d里添加admob广告

Android Google AdMob 广告接入示例

在Unity中使用Admob和Adcolony