unity sdkandroid-TopOn 广告聚合平台接入

1、TopOn 广告聚合平台接入(按文档走基本没问题)

官方文档: TopOn Document

注意事项(我是按2018接入的)切记用最近版本,版本不对可能导致广告展示不出来

1、生成mainTemplate.gradle文件,每次添加新sdk后打包出现有jar未引用到,就重新生成下mainTemplate.gradle,因为部分jar是通过.gradle引入的,如下如:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          xmlns:tools="http://schemas.android.com/tools" 
          package="com.game.demo" 
          android:versionCode="2" 
          android:versionName="1.1">
  
  <uses-sdk android:usesCleartextTraffic="true" />
  
  <!--必须要有的权限-->
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

  <!-- Optional : -->
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />


  <!--其中usesCleartextTraffic的配置必须设置,主要作用是让游戏里可以使用http请求(必须使用)-->
  <application android:usesCleartextTraffic="true">
    <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name"  android:hardwareAccelerated="true">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <!--这个设置主要是为了适配9.0以上的机器(必须使用)-->
    <uses-library android:name="org.apache.http.legacy" android:required="false" />
  </application>
</manifest>

2、如何选择Android的部分AAR或者Jar不进行打包**,把Android平台的打包选择去掉,然后选择Apply,就完成剔除打包的操作

using AnyThinkAds.Api;
using MiniJSON;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AdsController : MonoBehaviour
{
    public static AdsController instance;
    public string APPKEY = "dcb025aeda75aa70184c57f6d7bd4ba8";
    public string APPID = "a5fbb24fb267fd";
    public bool isDebug = true;

    private void Awake()
    {
        instance = this;

    }

    void Start()
    {
        initSDK();
    }

    private class InitListener : ATSDKInitListener
    {
        public void initSuccess()
        {
            Debug.Log("Developer Develop callback SDK initSuccess");

            BannerAd.instance.Invoke("loadBannerAd", 1f);
            InterstitialAd.instance.loadInterstitialAd();
            NativeAd.instance.loadNative();
            VidoeAd.instance.Invoke("loadVideo", 0.3f);
        }
        public void initFail(string msg)
        {
            Debug.Log("Developer callback SDK initFail:" + msg);
            AdsController.instance.Invoke("initSDK", 2);
        }
    }

    private class GetLocationListener : ATGetUserLocationListener
    {
        public void didGetUserLocation(int location)
        {
            Debug.Log("Developer callback didGetUserLocation(): " + location);
            if (location == ATSDKAPI.kATUserLocationInEU && ATSDKAPI.getGDPRLevel() == ATSDKAPI.UNKNOWN)
            {
                ATSDKAPI.showGDPRAuth();
            }
        }
    }

    //初始化Toponsdk
    public void initSDK()
    {
        //		DebugConsole.Log ("init sdk");
        Debug.Log("Developer Version of the runtime: " + Application.unityVersion);
        Debug.Log("Developer init sdk");
        Debug.Log(" Developer Screen size : {" + Screen.width + ", " + Screen.height + "}");

        if (isDebug)
        {
            ATSDKAPI.setLogDebug(true);
        }

        ATSDKAPI.setChannel("toutiao");

        Debug.Log("Developer DataConsent: " + ATSDKAPI.getGDPRLevel());
        Debug.Log("Developer isEUTrafic: " + ATSDKAPI.isEUTraffic());

        ATSDKAPI.getUserLocation(new GetLocationListener());

        ATSDKAPI.initSDK(APPID, APPKEY, new InitListener());

    }




}

3、原生广告
v5.7.8 版本有个bug,不能出来原生广告要替换anythink_bridge.aar 

百度网盘 请输入提取码    提取码:78e4

Android上需要注意的:
目前使用Unity直接打出Android的APK是不能使用原生视频广告,因为Unity打包APK默认会把游戏的Activity硬件加速关闭,所以无法展示视频。如果需要展示原生广告视频的话,必须使用export project的方式处理,处理方式如下

修改AndroidManifest里的属性,将图中的hardwareAccelerated的属性设置成true 

拉取广告,ATSize 控制广告底框大小区域

    public void loadNative()
    {
        if (callbackListener == null)
        {
            callbackListener = new ATNativeCallbackListener();
            ATNativeAd.Instance.setListener(callbackListener);
        }
        //----- v5.6.8以上 -----
        Dictionary<string, object> jsonmap = new Dictionary<string, object>();
        ATSize nativeSize = new ATSize(900, 600, false);
        nativeSize = new ATSize(900, 600);
        jsonmap.Add(ATNativeAdLoadingExtra.kATNativeAdLoadingExtraNativeAdSizeStruct, nativeSize);

        ATNativeAd.Instance.loadNativeAd(nativeAdId, jsonmap);
    }

展示广告,具体原生内容(icon,text,video)显示位置


    public void showNative()
    {
        Debug.Log("Developer show native....");
        if (isShow&& isAdReady())
        {
            ATNativeConfig conifg = new ATNativeConfig();

            string bgcolor = "#ffffff";
            string textcolor = "#000000";

            //显示的位置(左上角未原点)
            int rootbasex =(screenWidth-900)/2 , rootbasey = screenHeight - 600;
            //int rootbasex = 100, rootbasey = 100;
            //父框架
            int x = rootbasex, y = rootbasey, width = 300 * 3, height = 200 * 3, textsize = 17;
            conifg.parentProperty = new ATNativeItemProperty(x, y, width, height, bgcolor, textcolor, textsize);

            //adlogo 
            x = 0 * 3; y = 0 * 3; width = 30 * 3; height = 20 * 3; textsize = 17;
            conifg.adLogoProperty = new ATNativeItemProperty(x, y, width, height, bgcolor, textcolor, textsize);


            //adicon
            x = 0 * 3; y = 50 * 3 - 50; width = 60 * 3; height = 50 * 3; textsize = 17;
            conifg.appIconProperty = new ATNativeItemProperty(x, y, width, height, bgcolor, textcolor, textsize);

            //ad cta 
            x = 0 * 3; y = 150 * 3; width = 300 * 3; height = 50 * 3; textsize = 17;
            conifg.ctaButtonProperty = new ATNativeItemProperty(x, y, width, height, "#ff21bcab", "#ffffff", textsize);

            //ad desc
            x = 60 * 3; y = 100 * 3; width = 240 * 3 - 20; height = 50 * 3 - 10; textsize = 10;
            conifg.descProperty = new ATNativeItemProperty(x, y, width, height, bgcolor, "#777777", textsize);


            //ad image
            x = 60 * 3; y = 0 * 3 + 20; width = 240 * 3 - 20; height = 100 * 3 - 10; textsize = 17;
            conifg.mainImageProperty = new ATNativeItemProperty(x, y, width, height, bgcolor, textcolor, textsize);

            //ad title 
            x = 0 * 3; y = 100 * 3; width = 60 * 3; height = 50 * 3; textsize = 12;
            conifg.titleProperty = new ATNativeItemProperty(x, y, width, height, bgcolor, textcolor, textsize);

            ATNativeAdView anyThinkNativeAdView = new ATNativeAdView(conifg);
            aTNativeAdView = anyThinkNativeAdView;

            Debug.Log("Developer renderAdToScene--->");
            ATNativeAd.Instance.renderAdToScene(nativeAdId, aTNativeAdView);

        }

    }

4、插屏广告、激励视频、banner广告按官方文档即可

激励视频存在onRewardedVideoAdPlayClosed先调用,onReward后调用,所以奖励写在onReward中,

5、开屏广告(由于没有unity的版本,所以需要在AS里面加入)

官方文档:TopOn Document

UnityPlayerActivity.cs

 //  跳转到开屏activity
public void ShowSplashAd()
{
    Intent intent = new Intent(UnityPlayerActivity.this, SplashAdShowActivity.class);
    startActivity(intent);
}

 SplashAdShowActivity.cs

public class SplashAdShowActivity extends FragmentActivity implements ATSplashAdListener {
    ATSplashAd splashAd;
    String unitId ="b5fbb27b736f6d";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_ad_show);
        //String unitId = getIntent().getStringExtra("unitId");
        FrameLayout container = findViewById(R.id.splash_ad_container);
        ViewGroup.LayoutParams layoutParams = container.getLayoutParams();
        /**You should set size to the layout param.**/
        layoutParams.width = getResources().getDisplayMetrics().widthPixels;
        layoutParams.height = (int) (getResources().getDisplayMetrics().heightPixels * 0.9);

        ATMediationRequestInfo atMediationRequestInfo = null;

        穿山甲(Pangle)
        //atMediationRequestInfo = new TTATRequestInfo("5121885", "887406066", false);
        //atMediationRequestInfo.setAdSourceId("200088");

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        if (preferences.getBoolean("oneOpen",false)==false)
        {//首次开屏加载穿山甲的id
            SharedPreferences.Editor editor = preferences.edit();
            editor.putBoolean("oneOpen", true);
            editor.commit();

            穿山甲(Pangle)
            atMediationRequestInfo = new TTATRequestInfo("5121885", "887406066", false);
            atMediationRequestInfo.setAdSourceId("200088");

            Log.i("SplashAd", "oneshow");

        }else {
            Log.i("SplashAd", "twoshow");

        }

        splashAd = new ATSplashAd(this, container, unitId, atMediationRequestInfo, this);

//        atMediationRequestInfo = new MintegralATRequestInfo("100947", "ef13ef712aeb0f6eb3d698c4c08add96", "210169", "276803");
//        atMediationRequestInfo.setAdSourceId("71606");
//
//        atMediationRequestInfo = new GDTATRequestInfo("1101152570", "8863364436303842593");
//        atMediationRequestInfo.setAdSourceId("71602");

//        atMediationRequestInfo = new SigmobiATRequestInfo("1282", "27531c7c64157934", "e04d1ac9231");
//        atMediationRequestInfo.setAdSourceId("71608");
//
//        atMediationRequestInfo = new BaiduATRequestInfo("e866cfb0", "2058622");
//        atMediationRequestInfo.setAdSourceId("71609");

//        atMediationRequestInfo  = new KSATRequestInfo("501400010", "5014000234");
//        atMediationRequestInfo.setAdSourceId("88377");


    }

    @Override
    public void onAdLoaded() {

        Log.i("SplashAd", "onAdLoaded---------");
    }

    @Override
    public void onNoAdError(AdError adError) {
        Log.i("SplashAd", "onNoAdError---------:" + adError.printStackTrace());
        jumpToMainActivity();

        try {
            UnityPlayer.UnitySendMessage("AppsFlyerObject", "AdShowSplash","");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private Handler handler = new Handler();

    @Override
    public void onAdShow(ATAdInfo entity) {
        Log.i("SplashAd", "onAdShow:\n" + entity.toString());
        if (entity.getNetworkFirmId() == 22) {
            /**
             * Only for Baidu:
             * The display time and the skipped advertising style can be configured through the Baidu's backstage(Recommend)
             * , and can be customized and modified in your application.
             */
            skipViewSetting(); //If setting skipview by Baidu's backstage, you should not run this method.
        }

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    UnityPlayer.UnitySendMessage("AppsFlyerObject", "AFTopon_ilrdString",entity.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 1000);
    }

    private void skipViewSetting() {
        final TextView skipView = findViewById(R.id.splash_ad_skip);
        CountDownTimer countDownTimer = new CountDownTimer(5000L, 1000L) {
            @Override
            public void onTick(long millisUntilFinished) {
                skipView.setText((millisUntilFinished / 1000) + " | 跳过");
            }

            @Override
            public void onFinish() {
                jumpToMainActivity();
            }
        };

        countDownTimer.start();
        skipView.setVisibility(View.VISIBLE);

        skipView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToMainActivity();
            }
        });
    }

    @Override
    public void onAdClick(ATAdInfo entity) {
        Log.i("SplashAd", "onAdClick:\n" + entity.toString());

    }

    @Override
    public void onAdDismiss(ATAdInfo entity) {
        Log.i("SplashAd", "onAdDismiss:\n" + entity.toString());
        jumpToMainActivity();
    }

    @Override
    public void onAdTick(long millisUtilFinished) {
        Log.i("SplashAd", "onAdTick---------:" + millisUtilFinished);
    }

    boolean hasHandleJump = false;

    public void jumpToMainActivity() {
        if (!hasHandleJump) {
            hasHandleJump = true;
            finish();
            //Toast.makeText(this, "start your MainActivity.", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Toast.makeText(this, "SplashAdShowActivity onDestroy", Toast.LENGTH_SHORT).show();
        if (splashAd != null) {
            splashAd.onDestory();
        }

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习编程?其实不难,不过在学习编程之前你得先了解你的目的是什么?这个很重要,因为目的决定你的发展方向、决定你的发展速度。
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面设计类、前端与移动、开发与测试、营销推广类、数据运营类、运营维护类、游戏相关类等,根据不同的分类下面有细分了不同的岗位。
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生学习Java开发,但要结合自身的情况,先了解自己适不适合去学习Java,不要盲目的选择不适合自己的Java培训班进行学习。只要肯下功夫钻研,多看、多想、多练
Can’t connect to local MySQL server through socket \'/var/lib/mysql/mysql.sock问题 1.进入mysql路径
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 sqlplus / as sysdba 2.普通用户登录
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服务器有时候会断掉,所以写个shell脚本每五分钟去判断是否连接,于是就有下面的shell脚本。
BETWEEN 操作符选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或者日期。
假如你已经使用过苹果开发者中心上架app,你肯定知道在苹果开发者中心的web界面,无法直接提交ipa文件,而是需要使用第三方工具,将ipa文件上传到构建版本,开...
下面的 SQL 语句指定了两个别名,一个是 name 列的别名,一个是 country 列的别名。**提示:**如果列名称包含空格,要求使用双引号或方括号:
在使用H5混合开发的app打包后,需要将ipa文件上传到appstore进行发布,就需要去苹果开发者中心进行发布。​
+----+--------------+---------------------------+-------+---------+
数组的声明并不是声明一个个单独的变量,比如 number0、number1、...、number99,而是声明一个数组变量,比如 numbers,然后使用 nu...
第一步:到appuploader官网下载辅助工具和iCloud驱动,使用前面创建的AppID登录。
如需删除表中的列,请使用下面的语法(请注意,某些数据库系统不允许这种在数据库表中删除列的方式):
前不久在制作win11pe,制作了一版,1.26GB,太大了,不满意,想再裁剪下,发现这次dism mount正常,commit或discard巨慢,以前都很快...
赛门铁克各个版本概览:https://knowledge.broadcom.com/external/article?legacyId=tech163829
实测Python 3.6.6用pip 21.3.1,再高就报错了,Python 3.10.7用pip 22.3.1是可以的
Broadcom Corporation (博通公司,股票代号AVGO)是全球领先的有线和无线通信半导体公司。其产品实现向家庭、 办公室和移动环境以及在这些环境...
发现个问题,server2016上安装了c4d这些版本,低版本的正常显示窗格,但红色圈出的高版本c4d打开后不显示窗格,
TAT:https://cloud.tencent.com/document/product/1340