Unity 自带广告接入方式:
1.打开unity services 页面 (Window→Services)
2.打开Ads开关
3.选则Enable test mode 选项
4.看下Advanced选项,注意gameid后面脚本要用到
5.写脚本挂在任意一个物体上(相机或者空物体都行)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements; //如果提示这个命名空间找不到,注意切换到Android或者IOS平台即可找到,因为ADS只支持Android和IOS平台
public class testAds : MonoBehaviour {
// Use this for initialization
void Start () {
Advertisement.Initialize("1446785", true);
}
public void ShowAd()
{
print(Advertisement.IsReady());
if (Advertisement.IsReady())
{
Advertisement.Show();
}
}
// Update is called once per frame
void Update () {
if (Advertisement.IsReady() && !Advertisement.isShowing)
{
ShowRewardedAd();
}
}
public void ShowRewardedAd()
{
if (Advertisement.IsReady("rewardedVideo"))
{
var option=new ShowOptions{resultCallback=HandleShowResult};
Advertisement.Show("rewardedVideo", option);
}
}
private void HandleShowResult(ShowResult result)
{
switch (result)
{
case ShowResult.Finished:
print("the ad was successfully shown");
break;
case ShowResult.Skipped:
print("the ad was skipped before reaching the end");
break;
case ShowResult.Failed:
print("the ad failed to be shown");
break;
}
}
}
6.运行测试即可
不明白的留言交流哦。