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();
        }

    }
}

相关文章

学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习...
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面...
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生...
Can’t connect to local MySQL server through socket \'/v...
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 ...
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服...