codecamp
Vungle广告入门

Vungle广告入门

Unity篇

首先作者是只会添加个按钮并且设置点击事件的Unity小菜,机缘巧合工作室的一位同事需要接入广告,所以看了一下相关文档

& 官方中文文档:https://support.vungle.com/hc/zh-cn/articles/204311244-Vungle-Unity-入门指南
& 官方英文文档:https://support.vungle.com/hc/en-us/articles/204311244-Get-Started-with-Vungle-Unity
注意:中文文档有些地方会把代码给一并翻译了

详情看代码:

这个是Unity中的c#脚本事件,用了NGUI,直接绑定到按钮上选择里面的OnClick方法就行了

注意先导入vungle的unity插件在进行编码,下载链接:
https://s3.amazonaws.com/vvv-releases/prime31-unity/VunglePlugin.unitypackage

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class btn : MonoBehaviour {
    string logTag = "vungle";
    Dictionary<string, object> options;
    // Use this for initialization
    void Start () {
        DebugLog("Initializing the Vungle SDK");
        //您的 App ID 可在您的应用程序页面上的 Vungle Dashboard 中找到   
        Vungle.init("Test_Android", "Test_iOS", "Test_Windows");
        //为了当调用Vungle.playAdWithOption (options)的时候,加载相应的弹窗文本设置,详情看文档,或字面意思
        options = new Dictionary<string, object> ();
        options ["incentivized"] = true;
        options ["alertTitle"] = "已经看玩80%";
        options ["alertText"] = "可以退出";
        options ["closeText"] = "关闭";
        options ["continueText"] = "继续观看";
        options ["immersive"] = true;
        //调用广告事件监听
        initializeEventHandlers ();


    }

    
    // Update is called once per frame
    void Update () {
    }


    //按钮的点击事件
    public void OnClick()
    {
//        广告是否加载完毕
        if (Vungle.isAdvertAvailable ()) {
//          Vungle.setSoundEnabled(false);//设置声音是否开启
            //Event triggered during when an ad is about to be played
            Vungle.onAdStartedEvent += () => {
                DebugLog ("开始播放广告");
            };
//          Vungle.playAdWithOption (options);//带关闭确认的广告打开方式
            Vungle.playAd ();//无弹窗确认的广告打开方式
        } else {
            DebugLog("广告加载中。。。请稍等");
        }



    
    }
    //在onstart方法中调用,有各种广告状态监听
    /* Setup EventHandlers for all available Vungle events */
    void initializeEventHandlers() {
        //Event is triggered when a Vungl ,e ad finished and provides the entire information about this event
        //These can be used to determine how much of the video the user viewed, if they skipped the ad early, etc.
        Vungle.onAdFinishedEvent += (args) => {
            DebugLog ("广告结束时间:" + args.TimeWatched + ", 总时间:" + args.TotalDuration 
                + ", 是否被点击:" + args.WasCallToActionClicked +  ", 是否看完:" 
                + args.IsCompletedView);
        };


        //Event is triggered when the ad's playable state has been changed
        //It can be used to enable certain functionality only accessible when ad plays are available
        Vungle.adPlayableEvent += (adPlayable) => {
            if(adPlayable){
                DebugLog ("Ad广告已经加载完成:");
            }else{
                DebugLog ("Ad广告正在加载中...请稍等:");
            }


        };

    
        //Fired log event from sdk
        // SDK 发送日志事件时促发
        Vungle.onLogEvent += (log) => {
            DebugLog ("Log: " + log);
        };


    }
    //方便打印信息的方法
    /* Common method for ensuring logging messages have the same format */
    void DebugLog(string message) {
        Debug.Log(logTag + System.DateTime.Today +": " + message);
    }


}



官方文档有比这详细的解说,但是这个已经可以加载出视频广告了

温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }