c# - 如何在访问广告的任何类或成员之前初始化 Monetization SDK
问题描述
我有视频广告,它连接到游戏场景中的丢失面板,当玩家松开时显示。我还需要在我的场景底部放置横幅广告,它会在播放器播放时始终显示。
因为横幅是广告 API 的一部分,所以我需要将 Monetization 用于视频广告,将广告用于横幅,但出现错误:“已添加具有相同密钥的项目”。因此,正如统一网站告诉我的那样,我在主脚本上的广告 API 之前初始化了 Monetization SDK,但仍然收到此错误。
主脚本:
using UnityEngine.Monetization;
using UnityEngine.Advertisements;
private void Start() {
Monetization.Initialize("1234567", true);
Advertisement.Initialize("1234567");
StartCoroutine(ShowBannerWhenReady());
}
IEnumerator ShowBannerWhenReady() {
while (!Advertisement.IsReady("Banner")) {
yield return new WaitForSeconds(0.5f);
}
Advertisement.Banner.Show("Banner");
}
松散的脚本:
using UnityEngine.SceneManagement;
using UnityEngine.Monetization;
private void Start() {
if (Monetization.isSupported) {
Monetization.Initialize("1234567", true);
}
}
if (gameObject.name == "Free") {
if (cube.GetComponent<NewScpr>().sec_lf == 0) {
if (Monetization.IsReady("rewardedVideo")) {
ShowAdCallbacks options = new ShowAdCallbacks();
options.finishCallback = HandleShowResult;
ShowAdPlacementContent ads = Monetization.GetPlacementContent("rewardedVideo") as ShowAdPlacementContent;
ads.Show(options);
}
}
}
解决方案
我建议您删除UnityEngine.Monetization命名空间。
广告命名空间
• 适用于 4.6 版之后的所有 Unity 版本
• 可以展示视频、展示广告和可播放广告(奖励和非奖励)
• 可以展示 IAP 促销广告(需要 Unity IAP)(在 2.3 版中添加)
• 支持个性化展示位置(需要 Unity IAP)(在 2.3 版中添加)
• 可以显示横幅广告(在 3.0 版中添加)
货币化命名空间
• 适用于版本 5.0.1 之后的所有 Unity 版本
• 可以展示视频、展示广告和可播放广告(奖励和非奖励)
• 可以展示 IAP 促销广告(使用任何 IAP 解决方案)
• 可以显示原生 IAP 促销(使用任何 IAP 解决方案)
• 支持个性化展示位置(使用任何 IAP 解决方案)
• 可以展示 AR 广告
这是我使用UnityEngine.Advertisements添加横幅和视频的方法,效果很好。
[RequireComponent(typeof(Button))]
public class UnityAds : MonoBehaviour, IUnityAdsListener
{
private Button rewardableAdButton;
public const string GameId = "YourGameID";
public const bool TestMode = true; // Change this to false when going live
private readonly string bannerPlacementId = "YourBannerPlacementId";
private readonly string rewardVideoPlacementId = "YourVideoPlacementId";
void Awake()
{
rewardableAdButton = GameObject.FindGameObjectWithTag("RewardableAd").GetComponent<Button>();
}
void Start()
{
if (rewardableAdButton)
{
rewardableAdButton.onClick.AddListener(ShowRewardedVideo);
}
Advertisement.AddListener(this);
if (Advertisement.isSupported && !Advertisement.isInitialized)
{
Advertisement.Initialize(GameId, TestMode);
}
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
StartCoroutine(ShowBannerWhenReady());
}
#region ####### REWARDABLE AD ##########
void ShowRewardedVideo()
{
Advertisement.Show(rewardVideoPlacementId);
}
public void OnUnityAdsReady(string placementId)
{
rewardableAdButton.interactable = Advertisement.IsReady(rewardVideoPlacementId);
}
public void OnUnityAdsDidStart(string placementId)
{
Debug.Log("Ad started");
}
public void OnUnityAdsDidError(string message)
{
Debug.Log("Error while playing Ad");
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
if (showResult == ShowResult.Finished)
{
Debug.Log("Ad is finished, reward the player");
}
else if (showResult == ShowResult.Skipped)
{
Debug.Log("User skipped, do not reward the player");
}
else if (showResult == ShowResult.Failed)
{
Debug.LogWarning("The ad did not finish due to an error.");
}
}
#endregion
#region ########## BANNER ##########
public IEnumerator ShowBannerWhenReady()
{
while (!Advertisement.IsReady(bannerPlacementId))
{
yield return new WaitForSeconds(0.1f);
}
Advertisement.Banner.Show(bannerPlacementId);
}
public void HideBanner()
{
Advertisement.Banner.Hide(false);
}
#endregion
}
推荐阅读
- scala - 如何在 Spark Standalone 集群中提交多个作业?
- r - 制作个人 R 包时如何将函数与 data.table 结合使用而没有错误消息?
- php - 一个 cURL 会执行另一个 cURL
- laravel-dusk - tearDownDuskClass 中的异常
- c# - ExecuteReader 查询,其中有两个 ExecuteNonQuery
- javascript - 如何获取我在此 javascript 函数中单击的 indexof 图像?
- php - 将 PHP 变量传递给模态
- php - 查询laravel表时如何得到相同的结果数组
- python - 即使行列式为零,我也会得到矩阵的逆
- json - 将 nasaAPI 与示例参数一起使用