穿山甲广告对接

一、清除其他sdk影响

二、接入穿山甲SDK

1、注册登录  

广告对接的第三方的穿山甲。 需要先在穿山甲注册好自己的账号链接

https://www.pangle.cn/ 

创建项目应用和代码位,获取ID

穿山甲广告对接_第1张图片

 

2、导入sdk包

下载SDK  https://www.pangle.cn/union/media/union/download?doc_sort=adAccess

穿山甲广告对接_第2张图片

 

广告内容包:

PangleSDK.unitypackage

PangleAdapterScripts.unitypackage

广告参考包:

Example.unitypackage

穿山甲广告对接_第3张图片

 

3、设置id位置

Android脚本初始化代码位于PangleAdapterScripts/Scripts/Android/Pangle脚本:

穿山甲广告对接_第4张图片

4、广告

 激励广告Demo:

using ByteDance.Union;
using UnityEngine;
using UnityEngine.UI;

public sealed class MyAd : MonoBehaviour
{
    public Text information;
    public RewardVideoAd rewardAd;
    private AdNative adNative;
    private string AndroidSlotID = "906358800";
    public void PrintMessage(string message)
    {
        if (information != null)
        {
            information.text = message;
        }
    }
    /// 
    /// 初始化
    /// 
    void Start()
    {
        this.PrintMessage("初始化...");
        Pangle.InitializeSDK(callbackmethod);
    }
    private void callbackmethod(bool success, string message)
    {
        Debug.Log("`````````````````初始化``````" + success + "-----" + message);
        //LoadExpressRewardAd();
    }
    /// 
    /// AdNative对象为加载广告的入口,可用于广告获取。
    /// 
    private AdNative AdNative
    {
        get
        {
            if (this.adNative == null)
            {
                //一定要在初始化后才能调用,否则为空
                this.adNative = SDK.CreateAdNative();
            }
            SDK.RequestPermissionIfNecessary();
            return this.adNative;
        }
    }
    /******************************************  Dispose the Ad. 广告销毁***************************************************/
    /// 
    /// Dispose the Ad.
    /// 
    public void DisposeAds()
    {
        this.PrintMessage("销毁中..."+ this.rewardAd);
        if (this.rewardAd != null)
        {
            this.rewardAd.Dispose();
            this.rewardAd = null;
        }
    }
    /******************************************  The reward video Ad. 激励视频广告***************************************************/
    /// 
    /// Load the reward Ad.
    /// 
    public void LoadExpressRewardAd()
    {
        DisposeAds();
        var adSlot = new AdSlot.Builder()
            .SetCodeId(AndroidSlotID)
            .SetSupportDeepLink(true)
            .SetImageAcceptedSize(1080, 1920)
            .SetRewardName("金币") // 奖励的名称
            .SetRewardAmount(2) // 奖励的数量
            .SetUserID("user123") // 用户id,必传参数
            .SetMediaExtra("media_extra") // 附加参数,可选
            .SetOrientation(AdOrientation.Horizontal) // 必填参数,期望视频的播放方向
            .Build();
        this.AdNative.LoadRewardVideoAd(adSlot, new RewardVideoAdListener(this));
    }
    /// 
    /// Show the reward Ad.
    /// 
    public void ShowExpressRewardAd()
    {
        if (this.rewardAd == null)
        {
            Debug.LogError("请先加载广告");
            this.PrintMessage("请先加载广告");
            return;
        }
        else
        {
            this.rewardAd.ShowRewardVideoAd();
        }
    }
    /// 
    /// Reward video ad listener.
    /// 
    private sealed class RewardVideoAdListener : IRewardVideoAdListener
    {
        private MyAd example;

        public RewardVideoAdListener(MyAd example)
        {
            this.example = example;
        }

        public void OnError(int code, string message)
        {
            Debug.LogError("OnRewardError: " + message);
            this.example.PrintMessage("OnRewardError: " + message);
        }

        public void OnRewardVideoAdLoad(RewardVideoAd ad)
        {
            Debug.Log("OnRewardVideoAdLoad");
            this.example.PrintMessage("OnRewardVideoAdLoad");
            ad.SetRewardAdInteractionListener(
                new RewardAdInteractionListener(this.example));
            ad.SetDownloadListener(
                new AppDownloadListener(this.example));
            this.example.rewardAd = ad;
        }

        public void OnExpressRewardVideoAdLoad(ExpressRewardVideoAd ad)
        {
        }

        public void OnRewardVideoCached()
        {
            Debug.Log("OnRewardVideoCached");
            this.example.PrintMessage("OnRewardVideoCached");
            AdController.Instance.AdvertisementCache();
        }
    }
    /// 
    /// Reward video ad interaction listener.
    /// 
    private sealed class RewardAdInteractionListener : IRewardAdInteractionListener
    {
        private MyAd example;

        public RewardAdInteractionListener(MyAd example)
        {
            this.example = example;
        }

        public void OnAdShow()
        {
            Debug.Log("rewardVideoAd show");
            this.example.PrintMessage("rewardVideoAd show");
        }

        public void OnAdVideoBarClick()
        {
            Debug.Log("rewardVideoAd bar click");
            this.example.PrintMessage("rewardVideoAd bar click");
        }

        public void OnAdClose()
        {
            Debug.Log("rewardVideoAd close");
            this.example.PrintMessage("rewardVideoAd close");
            this.example.DisposeAds();
        }

        public void OnVideoComplete()
        {
            Debug.Log("rewardVideoAd complete");
            this.example.PrintMessage("rewardVideoAd complete");
        }

        public void OnVideoError()
        {
            Debug.LogError("rewardVideoAd error");
            this.example.PrintMessage("rewardVideoAd error");
        }

        public void OnRewardVerify(bool rewardVerify, int rewardAmount, string rewardName)
        {
            Debug.Log("verify:" + rewardVerify + " amount:" + rewardAmount +" name:" + rewardName);
            this.example.PrintMessage("verify:" + rewardVerify + " amount:" + rewardAmount + " name:" + rewardName);
            AdController.Instance.Award();
        }
    }
    /******************************************  广告下载相关(只有安卓才有相应回调,iOS无内部下载)  ***************************************************/
    /// 
    /// AppDownloadListener 
    /// 
    private sealed class AppDownloadListener : IAppDownloadListener
    {
        private MyAd example;

        public AppDownloadListener(MyAd example)
        {
            this.example = example;
        }

        public void OnIdle()
        {
        }

        public void OnDownloadActive(
            long totalBytes, long currBytes, string fileName, string appName)
        {
            Debug.Log("下载中,点击下载区域暂停");
            this.example.PrintMessage("下载中,点击下载区域暂停");
        }

        public void OnDownloadPaused(
            long totalBytes, long currBytes, string fileName, string appName)
        {
            Debug.Log("下载暂停,点击下载区域继续");
            this.example.PrintMessage("下载暂停,点击下载区域继续");
        }

        public void OnDownloadFailed(
            long totalBytes, long currBytes, string fileName, string appName)
        {
            Debug.LogError("下载失败,点击下载区域重新下载");
            this.example.PrintMessage("下载失败,点击下载区域重新下载");
        }

        public void OnDownloadFinished(
            long totalBytes, string fileName, string appName)
        {
            Debug.Log("下载完成,点击下载区域重新下载");
            this.example.PrintMessage("下载完成,点击下载区域重新下载");
        }

        public void OnInstalled(string fileName, string appName)
        {
            Debug.Log("安装完成,点击下载区域打开");
            this.example.PrintMessage("安装完成,点击下载区域打开");
        }
    }
  
}

 做个广告控制组件

穿山甲广告对接_第5张图片

 

using System;
using UnityEngine;
using UnityEngine.UI;
/// 
/// 广告控制类
/// 
public class AdController : MonoBehaviour
{
    //public static AdController Instance;//广告控制器
    public bool cacheSuccess;//广告是否缓存成功
    public MyAd ad;//广告
    public Action action;//奖励方法

    /// 
    /// 单例
    /// 
    private static AdController instance = null;//实例
    public static AdController Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject obj = new GameObject();
                instance = (AdController)obj.AddComponent(typeof(AdController));
            }
            return instance;
        }
    }
    public void Awake()
    {
        //所有单例脚本在场景变化后依然存在
        DontDestroyOnLoad(this.gameObject);//切换场景时,这个游戏物体不删除
        if (instance == null)
        {
            instance = this as AdController;
        }
        else
        {
            Destroy(gameObject);
        }
        Invoke("LoadAdvertisement", 2f);
    }
    /// 
    /// 广告缓存成功
    /// 
    public void AdvertisementCache()
    {
        cacheSuccess = true;
    }
    /// 
    /// 缓存广告
    /// 
    public void LoadAdvertisement()
    {
        ad.LoadExpressRewardAd();//如果没有缓存则缓存
    }
    /// 
    /// 播放广告
    /// 
    public void PlayerAdvertisement(Action act,int type)
    {
        if (cacheSuccess)
        {
            cacheSuccess = false;
            action = act;
            ad.ShowExpressRewardAd();//播放
        }
        else
        {
            ad.LoadExpressRewardAd();//如果没有缓存则缓存
        }
    }
    /// 
    /// 发放奖励
    /// 
    public void Award()
    {
        if (action != null)
        {
            action();
        }
        ad.DisposeAds();//销毁
        Invoke("LoadAdvertisement", 2f);
    }
}

你可能感兴趣的:(unity3D,unity)