[英]How to place a ad banner in unity using Unity ads? No plugins download
我想弄清楚如何统一放置广告横幅,但所有主题都与统一广告横幅有关。 我想知道如何使用 Unity 广告横幅。
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Advertisements;
using UnityEngine;
public class UnityAdManager : MonoBehaviour
{
public string gameId = "gameid";
public string placementId = "Adbanner";
public bool testMode = true;
public static UnityAdManager instance;
void Awake()
{
DontDestroyOnLoad(this.gameObject);
if (instance == null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
Advertisement.Initialize(gameId, testMode);
StartCoroutine(ShowBannerWhenReady());
}
// Update is called once per frame
void Update()
{
}
public void ShowAd()
{
if (PlayerPrefs.HasKey("Adcount"))
{
//number of ads
if (PlayerPrefs.GetInt("Adcount") == 2)
{
if (Advertisement.IsReady("video"))
{
Advertisement.Show("video");
}
PlayerPrefs.SetInt("Adcount", 0);
}
else
{
PlayerPrefs.SetInt("Adcount", PlayerPrefs.GetInt("Adcount") + 1);
}
}
else
{
PlayerPrefs.SetInt("Adcount", 0);
}
}
IEnumerator ShowBannerWhenReady()
{
while (!Advertisement.IsReady(placementId))
{
yield return new WaitForSeconds(0.5f);
}
Advertisement.Banner.Show(placementId);
}
}
**我不断收到错误
错误 CS0117“广告”不包含“横幅”的定义 ** 编辑:我让它在测试模式下工作,但是当我在手机上尝试时没有任何弹出。 我检查了一下,这个问题已经发生在其他人身上。 我住在美国,所以这个功能应该可以工作。 其他人建议使用另一种类型的广告。 但是统一广告很方便。
Unity Docs 中的横幅广告示例:
using System.Collections; using UnityEngine; using UnityEngine.Advertisements; public class BannerAdScript : MonoBehaviour { public string gameId = "1234567"; public string placementId = "bannerPlacement"; public bool testMode = true; void Start () { Advertisement.Initialize (gameId, testMode); StartCoroutine (ShowBannerWhenReady ()); } IEnumerator ShowBannerWhenReady () { while (!Advertisement.IsReady (placementId)) { yield return new WaitForSeconds (0.5f); } Advertisement.Banner.Show (placementId); } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.