手把手教你搭建用户画像,数据分析效率提升百倍

dockerredis哨兵集群,文末有彩蛋

  返回  

穿山甲广告对接

2021/7/21 14:15:44 浏览:

一、清除其他sdk影响

二、接入穿山甲SDK

1、注册登录  

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

https://www.pangle.cn/ 

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

 

2、导入sdk包

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

 

广告内容包:

PangleSDK.unitypackage

PangleAdapterScripts.unitypackage

广告参考包:

Example.unitypackage

 

3、设置id位置

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

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;
        }
    }
    /// <summary>
    /// 初始化
    /// </summary>
    void Start()
    {
        this.PrintMessage("初始化...");
        Pangle.InitializeSDK(callbackmethod);
    }
    private void callbackmethod(bool success, string message)
    {
        Debug.Log("`````````````````初始化``````" + success + "-----" + message);
        //LoadExpressRewardAd();
    }
    /// <summary>
    /// AdNative对象为加载广告的入口,可用于广告获取。
    /// </summary>
    private AdNative AdNative
    {
        get
        {
            if (this.adNative == null)
            {
                //一定要在初始化后才能调用,否则为空
                this.adNative = SDK.CreateAdNative();
            }
            SDK.RequestPermissionIfNecessary();
            return this.adNative;
        }
    }
    /******************************************  Dispose the Ad. 广告销毁***************************************************/
    /// <summary>
    /// Dispose the Ad.
    /// </summary>
    public void DisposeAds()
    {
        this.PrintMessage("销毁中..."+ this.rewardAd);
        if (this.rewardAd != null)
        {
            this.rewardAd.Dispose();
            this.rewardAd = null;
        }
    }
    /******************************************  The reward video Ad. 激励视频广告***************************************************/
    /// <summary>
    /// Load the reward Ad.
    /// </summary>
    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));
    }
    /// <summary>
    /// Show the reward Ad.
    /// </summary>
    public void ShowExpressRewardAd()
    {
        if (this.rewardAd == null)
        {
            Debug.LogError("请先加载广告");
            this.PrintMessage("请先加载广告");
            return;
        }
        else
        {
            this.rewardAd.ShowRewardVideoAd();
        }
    }
    /// <summary>
    /// Reward video ad listener.
    /// </summary>
    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();
        }
    }
    /// <summary>
    /// Reward video ad interaction listener.
    /// </summary>
    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无内部下载)  ***************************************************/
    /// <summary>
    /// AppDownloadListener 
    /// </summary>
    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("安装完成,点击下载区域打开");
        }
    }
  
}

 做个广告控制组件

 

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

    /// <summary>
    /// 单例
    /// </summary>
    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);
    }
    /// <summary>
    /// 广告缓存成功
    /// </summary>
    public void AdvertisementCache()
    {
        cacheSuccess = true;
    }
    /// <summary>
    /// 缓存广告
    /// </summary>
    public void LoadAdvertisement()
    {
        ad.LoadExpressRewardAd();//如果没有缓存则缓存
    }
    /// <summary>
    /// 播放广告
    /// </summary>
    public void PlayerAdvertisement(Action act,int type)
    {
        if (cacheSuccess)
        {
            cacheSuccess = false;
            action = act;
            ad.ShowExpressRewardAd();//播放
        }
        else
        {
            ad.LoadExpressRewardAd();//如果没有缓存则缓存
        }
    }
    /// <summary>
    /// 发放奖励
    /// </summary>
    public void Award()
    {
        if (action != null)
        {
            action();
        }
        ad.DisposeAds();//销毁
        Invoke("LoadAdvertisement", 2f);
    }
}

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号