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

news2024/10/17 17:19:13

一、下载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~
看到这里了,觉得有用记得点赞收藏关注哦~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2216069.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

js 实现斐波那契数列

斐波那契数列&#xff1a;所谓斐波那契数列指的是数列&#xff1a;1&#xff0c;1&#xff0c;2&#xff0c;3&#xff0c;5&#xff0c;8&#xff0c;13&#xff0c;21&#xff0c;……。即数列满足递推公式&#xff0c;F(0)0&#xff0c;F(1)1 实现代码&#xff1a; 第一种&…

【记录】Django数据库的基础操作

数据库连接 在Django中使用 mysqlclient 这个包用于数据库的连接&#xff0c;切换至 Django环境中直接 pip install mysqlclient 安装此包 1 数据库连接配置 在项目目录下的setting.py中配置 DATABASES {default: {ENGINE: django.db.backends.mysql,NAME: mini,#数据库名US…

(IOS)VMware虚拟机上安装win10系统(超详细)

简介 虚拟机是一种软件实现的计算机系统&#xff0c;可以在现有的操作系统平台上运行一个或多个虚拟的操作系统。它通过在主机操作系统上创建一个虚拟的硬件平台&#xff0c;并在其上运行一个完整的操作系统&#xff0c;来模拟一个真实的物理计算机。虚拟机可以提供一种隔离的…

青少年编程能力等级测评CPA C++(二级)试卷(1)

青少年编程能力等级测评CPA C&#xff08;二级&#xff09;试卷&#xff08;1&#xff09; 一、单项选择题&#xff08;共20题&#xff0c;每题3.5分&#xff0c;共70分&#xff09; CP2_1_1&#xff0e;下列C程序段中&#xff0c;对二维数组arr的正确定义是&#xff08; &am…

PL/SQL Developer如何连接Oracle数据库(汉化)

简介 PL/SQL Developer是一种用于Oracle数据库开发的集成开发环境&#xff08;IDE&#xff09;。它提供了一个可视化的界面&#xff0c;使开发人员能够方便地编写、调试和执行PL/SQL代码。PL/SQL Developer还具有其他功能&#xff0c;如数据库对象浏览器、SQL编辑器、数据导入…

python深浅拷贝,可变变量与不可变变量

赋值 在 python 中&#xff0c;赋值是将一个值或对象分配给一个变量的过程。赋值操作符是 &#xff0c;用于将右侧的值或对象赋给左侧的变量。 赋值&#xff1a;l2的值会随着原对象l1的值一同改变 l1 [1, 2, 3, 4] print(l1:, l1) l2 l1 print(l2:, l2) 给li列表新增元素 …

Java基础:面向对象编程3

1 Java可变长参数 1.1 概述 Java 的可变长参数&#xff08;Varargs&#xff09;是在 Java 1.5 中引入的功能&#xff0c;允许方法接受任意数量的相同类型的参数。可变参数的语法是在参数类型后面加上三个点&#xff08;...&#xff09;&#xff0c;例如 int... numbers。 1.…

Zsh 安装与配置

目录 1 环境配置 1.1 基本工具安装 1.2 安装 oh-my-zsh 1.3 从.bashrc中迁移配置&#xff08;可选&#xff09; 2 主题配置 2.1 内置主题 2.2 自定义主题 2.2.1 推荐主题 3 插件安装 3.1 推荐插件 3.1.1 zsh -autosuggestions 3.1.2 zsh-syntax-highlighting 3.2 启…

kubernetes中的微服务

目录 一 什么是微服务 二 微服务的类型 三 ipvs模式 3.1 ipvs模式配置方式 四 微服务类型详解 4.1 clusterip 4.2 ClusterIP中的特殊模式headless 4.3 nodeport 4.4 loadbalancer 4.5 metalLB 4.6 externalname 五 Ingress-nginx 5.1 ingress-nginx功能 5.2 部署…

【数据结构】1.顺序表

「前言」 &#x1f308;个人主页&#xff1a; 代码探秘者 &#x1f308;C语言专栏&#xff1a;C语言 &#x1f308;C专栏&#xff1a; C &#x1f308;喜欢的诗句:天行健,君子以自强不息. 线性表 线性表&#xff08;List&#xff09;&#xff1a;零个或多个数据元素的有限序列…

软考(网工)——数据通信基础

&#x1f550;信道特性 1️⃣概念 通信得目的就是传递信息。通信中产生和发送信息得一端叫信源&#xff0c;接受信息的一段叫信宿&#xff0c;信源和信宿之间的通信线路称为信道。 2️⃣信道带宽 W 模拟信道&#xff1a;Wf2-f1&#xff08;f2 和 f1分别表示&#xff1a;信道…

树的中心——dfs

题目 代码 #include <bits/stdc.h> using namespace std; const int N 1e510, M N*2; int h[N], e[M], ne[M], idx; int n; int ans 2e9; bool st[N]; void add(int a, int b) // 添加一条边a->b {e[idx] b, ne[idx] h[a], h[a] idx ; } int dfs(int u) {int…

芯片记录一下

1、MC34063 电源管理DCDC 输入电压&#xff1a;-0.3~40V 输出电压&#xff1a;Vout1.25*&#xff08;1R2/R1&#xff09; 1.25V~40V

【报错解决】安装scikit-rebate包报错

scikit-rebate ReBATE是一套基于Relief的机器学习特征选择算法 报错信息 解决方案 conda install numpy scipy scikit-learnpip install skrebate依次运行以上两步&#xff0c;即可成功安装&#xff01;

如何实时监测你的光纤资源?

光纤资源作为重要的通信基础设施&#xff0c;实时监测光纤资源的状态是运营管理好光纤资源的重要手段&#xff0c;那常用的监测指标维度与方法有那些呢&#xff1f; 维度1&#xff1a;资源数量 资源数量主是建立资源的基础档案&#xff0c;掌握光缆的型号、路由&#xff1b;光…

健康推荐系统:SpringBoot技术实现

3系统分析 3.1可行性分析 通过对本基于智能推荐的卫生健康系统实行的目的初步调查和分析&#xff0c;提出可行性方案并对其一一进行论证。我们在这里主要从技术可行性、经济可行性、操作可行性等方面进行分析。 3.1.1技术可行性 本基于智能推荐的卫生健康系统采用SSM框架&#…

python 位运算 笔记

起因&#xff0c; 目的: 位运算&#xff0c;令我头疼的地方。算法题里面也是经常见到。 位运算。 按位或&#xff0c;OR, | , 只要有一个为1&#xff0c; 结果就是1&#xff0c;否则为0按位异或&#xff0c;XOR, ^, 2个数不同&#xff0c;结果为1&#xff0c; 否则为0&#…

k8s jenkins 2.421动态创建slave

k8s jenkins 动态创建slave 简述使用jenkins动态slave的优势&#xff1a;制作jenkins-slave从节点jenkins-slave-latest:v1配置jenkins动态slave配置 Pod Template配置容器模板挂载卷 测试 简述 持续构建与发布是我们日常工作中必不可少的一个步骤&#xff0c;目前大多公司都采…

《OpenCV计算机视觉》—— 使用DNN模块实现图片风格迁移

文章目录 OpenCV中的DNN模块一、功能概述二、支持的模型格式三、基本使用方法四、DNN 模块的特点五、常见应用示例 示例&#xff1a;图片风格迁移 OpenCV中的DNN模块 OpenCV中的DNN&#xff08;Deep Neural Network&#xff09;模块是一个功能强大的工具&#xff0c;它允许开发…

python pip安装requirements.txt依赖与国内镜像

python pip安装requirements.txt依赖与国内镜像 如果网络通畅&#xff0c;直接pip安装依赖&#xff1a; pip install -r requirements.txt 如果需要国内的镜像&#xff0c;可以考虑使用阿里的&#xff0c;在后面加上&#xff1a; -i http://mirrors.aliyun.com/pypi/simple --…