【Unity】Unity中接入Admob聚合广告平台,可通过中介接入 AppLovin,Unity Ads,Meta等渠道的广告

一、下载Google Admob的SDK插件

到Google Admob官网中,切换到Unity平台在这里插入图片描述

进来之后是这样,注意后面有Unity标识,然后点击下载,跳转到github中,下载最新的Admob插件sdk,导入到Unity中在这里插入图片描述

二、阅读官方文档,了解广告加载流程

通过阅读官方文档,我们可以了解到其中有针对各类广告的Ios和Android的测试广告单元id,这对我们刚接入时测试阶段很有必要
在这里插入图片描述

然后我们以激励广告为例,可以看到接入激励广告的详细流程,官方下面也提供了所有流程的详细代码,其实如果没有特殊需求,官方的代码可以直接复制到我们的项目中就能使用
在这里插入图片描述

三、通过中介接入各渠道的广告

通过Admob中的中介就能接入各渠道的广告,当展示广告时候,他们会自动竞价,展示价格最高的广告。这里我们点击图中箭头,就可以下载对应渠道的最新版本的SDK插件,然后导入到Unity中即可,不需要任何设置,聚合平台会自动调取对应的广告渠道进行展示
在这里插入图片描述
下面是我导入到Unity中的所有插件
在这里插入图片描述
好了,到这里前端的准备基本结束了,相关的插件也都导入完毕了,如果是个人做游戏的话,自己到Admob后台注册对应的账号和Appid以及各个广告位的广告id,以及中介平台的各种广告id和相关联的功能,公司做游戏的话,这些各种id让对应的后台运营人员给到自己就好了,这里只介绍前端程序的相关内容,具体的id申请自行到后台操作一下

四、代码接入

该代码仅在测试阶段,通过官方的测试广告单元id全部通过,展示了出来,包括Banner,激励广告,插屏广告,详细内容根据自己的项目而定

using GoogleMobileAds.Api;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AdManager : Singleton<AdManager>
{


    private int sdkInitializedState = -1;//0--unconsent 1--consen

    private string ADMobRewardUnit = "ca-app-pub-3940256099942544/5224354917";
    private string ADMobInterstitialUnit = "ca-app-pub-3940256099942544/1033173712";
    private string ADMobBannerUnit = "ca-app-pub-3940256099942544/6300978111";
	private RewardedAd _rewardedAd = null;
	private InterstitialAd _interstitialAd = null;
    private BannerView _bannerView;

    private int tryInteTimes = 0;
    private int loadInteTimes = 1;

    private int tryTimes = 0;
    private int maxTryTimes = 0;
    private int loadTimes = 1;

    private void Start()
    {
        Init();
    }

    public void Init()
    {
        MobileAds.RaiseAdEventsOnUnityMainThread = true;
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            // This callback is called once the MobileAds SDK is initialized.
            sdkInitializedState = 1;
            PrepareRewardAds();
            PrepareInterAds();
        });
    }

    private void PrepareRewardAds()
    {
        if (sdkInitializedState < 0)
            return;
        if (_rewardedAd != null)
        {
            _rewardedAd.Destroy();
            _rewardedAd = null;
        }

        var adRequest = new AdRequest();
        RewardedAd.Load(ADMobRewardUnit, adRequest,
            (RewardedAd ad, LoadAdError error) =>
            {
                // if error is not null, the load request failed.
                if (error != null || ad == null)
                {
                    Debug.LogError("Rewarded ad failed to load an ad with error : " + error);
                    return;
                }

                Debug.Log("Rewarded ad loaded with response : " + ad.GetResponseInfo());

                _rewardedAd = ad;
            });
    }


    private void PrepareInterAds()
    {
        if (sdkInitializedState < 0)
            return;
        if (_interstitialAd != null)
        {
            _interstitialAd.Destroy();
            _interstitialAd = null;
        }

        // create our request used to load the ad.
        var adRequest = new AdRequest();

        // send the request to load the ad.
        InterstitialAd.Load(ADMobInterstitialUnit, adRequest,
            (InterstitialAd ad, LoadAdError error) =>
            {
                // if error is not null, the load request failed.
                if (error != null || ad == null)
                {
                    Debug.LogError("interstitial ad failed to load an ad with error : " + error);
                    return;
                }

                Debug.Log("Interstitial ad loaded with response : " + ad.GetResponseInfo());

                _interstitialAd = ad;
            });
    }


    [ContextMenu("测试Banner")]
    public void LoadBannerAd()
    {
        // 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>
    /// Creates a 320x50 banner view at top of the screen.
    /// </summary>
    private void CreateBannerView()
    {
        Debug.Log("Creating banner view");

        // If we already have a banner, destroy the old one.
        if (_bannerView != null)
        {
            _bannerView.Destroy();
        }

        // Create a 320x50 banner at top of the screen
        _bannerView = new BannerView(ADMobBannerUnit, AdSize.Banner, AdPosition.Bottom);
    }


    [ContextMenu("测试插屏广告")]
    public void ShowInterAD()
    {
        if (_interstitialAd != null && _interstitialAd.CanShowAd())
        {
            // SetAdmobInterstitialListener(_interstitialAd);
            _interstitialAd.Show();
        }
        else
        {
            if (++this.tryInteTimes >= this.maxTryTimes)
            {
                this.loadInteTimes = 3;
                this.PrepareInterAds();
                this.tryInteTimes = 0;
            }
            return;
        }
    }


    public void ShowRewardAD(Action successCallback)
    {
        if (_rewardedAd != null && _rewardedAd.CanShowAd())
        {
            SetAdmobRewardListener(_rewardedAd);
            _rewardedAd.Show((Reward reward) =>
            {
                successCallback();
            });
        }
    }


    private void SetAdmobRewardListener(RewardedAd ad)
    {
        // Raised when a click is recorded for an ad.
        ad.OnAdClicked += () =>
        {
            //RewardedAdClicked();
        };
        // Raised when an ad opened full screen content.
        ad.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Rewarded ad full screen content opened.");
        };
        // Raised when the ad closed full screen content.
        ad.OnAdFullScreenContentClosed += () =>
        {
            PrepareRewardAds();
            //RewardedAdClosed();
        };
        // Raised when the ad failed to open full screen content.
        ad.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Rewarded ad failed to open full screen content with error : " + error);
            // RewardedAdFailed();
            PrepareRewardAds();
        };
    }

    private void SetAdmobInterstitialListener(InterstitialAd interstitialAd)
    {
        // Raised when a click is recorded for an ad.
        interstitialAd.OnAdClicked += () =>
        {
            Debug.Log("Interstitial ad was clicked.");
            //InterstitialAdClicked();
        };
        // Raised when an ad opened full screen content.
        interstitialAd.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Interstitial ad full screen content opened.");
           // InterstitialAdDisplayed();
        };
        // Raised when the ad closed full screen content.
        interstitialAd.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Interstitial ad full screen content closed.");
            //InterstitialAdClosed();
            PrepareInterAds();
        };
        // Raised when the ad failed to open full screen content.
        interstitialAd.OnAdFullScreenContentFailed += (AdError error) =>
        {
            Debug.LogError("Interstitial ad failed to open full screen content with error : " + error);
            //InterstitialAdFailed();
            PrepareInterAds();
        };
    }

    [ContextMenu("测试激励广告")]
    public void TestShowRewardAd()
    {
        ShowRewardAD(() => 
        {
            Debug.LogError("激励广告回调");
        });
    }


}

测试方法也在里面,直接挂到Unity实体上运行,右击代码就可以进行测试,展示对应的广告
Over~
看到这里了,觉得有用记得点赞收藏关注哦~

决策树(Decision Tree)是一种常见的分类和回归算法。在构建决策树时,需要选择一个划分标准(即 criterion),用于从当前节点中选择最佳特征进行划分。常见的划分标准有以下两种: 1. 基尼系数(Gini Impurity):基尼系数是指在样本集合中随机抽取两个样本,其类别不一致的概率。具体地,对于样本集合 $D$,它的基尼系数定义为: $$Gini(D) = \sum_{k=1}^{|\mathcal{Y}|} \sum_{k' \neq k} p_k p_{k'} = 1 - \sum_{k=1}^{|\mathcal{Y}|} p_k^2$$ 其中,$|\mathcal{Y}|$ 是类别数目,$p_k$ 是第 $k$ 类样本所占比例。 对于决策树的某个节点 $t$ 和特征 $A$,假设将样本集合 $D$ 划分为 $D_1$ 和 $D_2$ 两部分,其中 $D_1$ 中的样本满足 $A$ 的取值为 $a$,$D_2$ 中的样本满足 $A$ 的取值不为 $a$。则使用基尼系数作为划分标准的信息增益(IG)为: $$IG(D,A) = Gini(D) - \sum_{a \in \mathcal{A}} \frac{|D_1|}{|D|} Gini(D_1) - \frac{|D_2|}{|D|} Gini(D_2)$$ 其中,$\mathcal{A}$ 是特征 $A$ 可能取值的集合。 2. 信息增益(Information Gain):信息增益是指使用某个特征对数据进行划分后,信息熵减少的程度。具体地,在样本集合 $D$ 中,类别 $k$ 的样本所占比例为 $p_k$,则其信息熵定义为: $$Ent(D) = -\sum_{k=1}^{|\mathcal{Y}|} p_k \log_2 p_k$$ 对于决策树的某个节点 $t$ 和特征 $A$,假设将样本集合 $D$ 划分为 $D_1$ 和 $D_2$ 两部分,其中 $D_1$ 中的样本满足 $A$ 的取值为 $a$,$D_2$ 中的样本满足 $A$ 的取值不为 $a$。则使用信息增益作为划分标准的信息增益为: $$IG(D,A) = Ent(D) - \sum_{a \in \mathcal{A}} \frac{|D_1|}{|D|} Ent(D_1) - \frac{|D_2|}{|D|} Ent(D_2)$$ 基于以上两种划分标准,决策树的构建过程就是递归地选择最优特征进行划分的过程。最终得到的决策树可以用于分类和回归任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七月.末

你的鼓励就是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值