您的位置:首页 > 其它

FaceBook原生广告接入——最白话,手把手教你做系列。

2017-12-19 16:40 489 查看
接入FaceBook原生广告(Native广告)申请广告不做赘述。

接入原生广告三个步骤。

第一步,导入SDK文件。下载地址

找到下载好的文件中的audience-network-unity-sdk-4.22.0.unitypackage文件,导入项目中。

会出现下面的文件。



Samples是各种广告的事例。每种广告都有对应的场景和代码。

第一步结束。

Banner,插屏和视频广告和Admob的广告基本相似。

这里讲一下没讲过的NativeAd(原生广告)。

第二步,代码部分。

using UnityEngine;
using UnityEngine.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using AudienceNetwork;
using UnityEngine.SceneManagement;

[RequireComponent (typeof(CanvasRenderer))]
[RequireComponent (typeof(RectTransform))]
public class NativeAdTest : MonoBehaviour
{
private NativeAd nativeAd;

// UI elements in scene
//以下部分为UI对应的一些组件
[Header("Text:")]
//广告标题名
public Text title;
//广告描述文字
public Text socialContext;
[Header("Images:")]
//广告大图
public Image coverImage;
//广告的Icon
public Image iconImage;
[Header("Buttons:")]
//跳转按钮对应文字
public Text callToAction;
//广告跳
4000
转按钮
public Button callToActionButton;

void Awake ()
{
this.Log ("Native ad ready to load.");
}
void Start ()
{
LoadAd ();
}

void OnGUI ()
{
// Update GUI from native ad
if (nativeAd != null && nativeAd.CoverImage != null) {
coverImage.sprite = nativeAd.CoverImage;
}
if (nativeAd != null && nativeAd.IconImage != null) {
iconImage.sprite = nativeAd.IconImage;
}
}

void OnDestroy ()
{
// Dispose of native ad when the scene is destroyed
if (this.nativeAd) {
this.nativeAd.Dispose ();
}
Debug.Log ("NativeAdTest was destroyed!");
}

// Load Ad button即为展示广告方法
public void LoadAd ()
{
// Create a native ad request with a unique placement ID (generate your own on the Facebook app settings).
// Use different ID for each ad placement in your app.这里填写你的原生广告ID
NativeAd nativeAd = new AudienceNetwork.NativeAd ("YOUR_PLACEMENT_ID");
this.nativeAd = nativeAd;

// Wire up GameObject with the native ad; the specified buttons will be clickable.
nativeAd.RegisterGameObjectForImpression (gameObject, new Button[] { callToActionButton });

// Set delegates to get notified on changes or when the user interacts with the ad.
nativeAd.NativeAdDidLoad = (delegate() {
this.Log ("Native ad loaded.");
Debug.Log ("Loading images...");
// Use helper methods to load images from native ad URLs
StartCoroutine (nativeAd.LoadIconImage (nativeAd.IconImageURL));
StartCoroutine (nativeAd.LoadCoverImage (nativeAd.CoverImageURL));
Debug.Log ("Images loaded.");
title.text = nativeAd.Title;
socialContext.text = nativeAd.SocialContext;
callToAction.text = nativeAd.CallToAction;
});
//广告对应事件
nativeAd.NativeAdDidFailWithError = (delegate(string error) {
this.Log ("Native ad failed to load with error: " + error);
//加载失败,这里可以执行请求失败的逻辑。比如你可以把图片 文字 和 链接替换成自己需求的。
});
nativeAd.NativeAdWillLogImpression = (delegate() {
this.Log ("Native ad logged impression.");
});
nativeAd.NativeAdDidClick = (delegate() {
this.Log ("Native ad clicked.");
});

// Initiate a request to load an ad.
nativeAd.LoadAd ();

this.Log ("Native ad loading...");
}

private void Log(string s)
{
Debug.Log (s);
}
}


原生广告主要的部分在于原生广告可以作为一个页面显示也可以作为UI的一部分显示。可以在显示该页面或者包含该广告的页面的时候调用LoadAd ();我这里直接在Start方法里调用了。

第二步结束。

第三步,页面布局和代码挂载。



广告页面所需元素如图所示。代码只需要的挂在对应的panel上面即可



最后,将对应的物体拖到代码对应位置即可。

所以UI元素位置和大小都可以按自己的需求变动。

以上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: