gpt4 book ai didi

c# - 如何停止从 UnityAds 和 Vungle 自动获取/加载广告?

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:39 28 4
gpt4 key购买 nike

我在我的游戏中同时使用了 Unity Ads 和 Vungle。我使用它们的 .unitypackage 安装了它们。

我已经完成了交替显示广告的代码。我现在的问题是如何在关闭他们的第一个广告后阻止他们获取/加载?或者可以选择在获取时停止获取。

这让我的游戏因内存压力而崩溃,因为内存中已经有 2 个广告以及我的游戏资源。

我浏览了源代码,找不到任何 API 来停止获取或卸载等待显示的广告。我查看了他们的文档,但没有详细说明。

这是我当前的代码:

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;


public class AdsMngr : MonoManager<AdsMngr> {
enum AdsEngine{

VUNGLE,
UNITY_ADS,

MAXNUM
};

enum AdsStatus{
IDLE,
WAITING,
SHOWING,

MAXNUM
}

AdsEngine m_curAdsEngine = AdsEngine.UNITY_ADS;
AdsStatus m_curAdsStatus = AdsStatus.IDLE;

#if UNITY_IPHONE || UNITY_ANDROID


protected override void Awake(){
base.Awake ();

m_curAdsEngine = AdsEngine.UNITY_ADS; //VUNGLE;

SetupAdsCallback ();

}

protected override void OnDestroy(){

base.OnDestroy();

UnsetupAdsCallback();

}

void SetNextAdEngine (){
m_curAdsEngine = (AdsEngine)((int)(m_curAdsEngine + 1) % (int)AdsEngine.MAXNUM);

}

void Update(){

if (m_curAdsStatus == AdsStatus.WAITING) {
Debug.Log ("Waiting for ads to load: "+m_curAdsEngine+"\n");

bool bHasReadyAds = false;

switch(m_curAdsEngine){
case AdsEngine.VUNGLE: if( Vungle.isAdvertAvailable()){bHasReadyAds = true;} break;
case AdsEngine.UNITY_ADS: if( Advertisement.isReady() ){bHasReadyAds = true;} break;
}

if (bHasReadyAds) {
LoadingOverlay.Instance.Close();
PromptOverlay.Instance.Close();
ShowAd ();
}
}

}

public void StartAd(){

LoadingOverlay.Instance.Open();

//---start refetching/recaching the ads here
switch(m_curAdsEngine){
case AdsEngine.VUNGLE: {
Vungle.init( "mygame_forAndroid", "mygame_forIOS" );
}break;
case AdsEngine.UNITY_ADS: {
if (Advertisement.isSupported) {
//Advertisement.allowPrecache = true;
Advertisement.Initialize ("12345", true);

Debug.Log("Advertisement Initialized.\n");
} else {
Debug.Log("Platform not supported\n");
}

}break;
}

m_curAdsStatus = AdsStatus.WAITING;

}

public void StopWaitingAds(){
m_curAdsStatus = AdsStatus.IDLE;
//TODO: cancel the loading of ads.

//xxx: test code only
SetNextAdEngine ();


}

public void ShowAd(){
switch(m_curAdsEngine){
case AdsEngine.VUNGLE: {
Debug.Log("*** Vungle Ads is available. Now playing...\n");
Vungle.playAd();
Vungle.setSoundEnabled(false);
}break;
case AdsEngine.UNITY_ADS: {
Debug.Log("*** Unity Ads is available. Now playing...\n");
Advertisement.Show(null, new ShowOptions {
pause = true,
resultCallback = result => {
Debug.Log(result.ToString());
onAdEndedEvent();
}
});
}break;
}

m_curAdsStatus = AdsStatus.SHOWING;
}

#region Optional: Example of Subscribing to All Events

void SetupAdsCallback()
{
Vungle.onAdStartedEvent += onAdStartedEvent;
Vungle.onAdEndedEvent += onAdEndedEvent;
Vungle.onAdViewedEvent += onAdViewedEvent;
Vungle.onCachedAdAvailableEvent += onCachedAdAvailableEvent;
}


void UnsetupAdsCallback()
{
Vungle.onAdStartedEvent -= onAdStartedEvent;
Vungle.onAdEndedEvent -= onAdEndedEvent;
Vungle.onAdViewedEvent -= onAdViewedEvent;
Vungle.onCachedAdAvailableEvent -= onCachedAdAvailableEvent;
}



void onAdStartedEvent()
{
Debug.Log( "onAdStartedEvent" );
}


void onAdEndedEvent()
{
Debug.Log( "onAdEndedEvent" );

EndGameRewardOverlay.Instance.SetMultiplier(2);
EndGameRewardOverlay.Instance.Open();

if (m_curAdsEngine == AdsEngine.VUNGLE) {
Vungle.setSoundEnabled(true);
}

m_curAdsStatus = AdsStatus.IDLE;
SetNextAdEngine();

}


void onAdViewedEvent( double watched, double length )
{
Debug.Log( "onAdViewedEvent. watched: " + watched + ", length: " + length );
}


void onCachedAdAvailableEvent()
{
Debug.Log( "onCachedAdAvailableEvent" );
}

#endregion

#endif

}

最佳答案

Unity Ads 和 Vungle 自行处理广告缓存,无法阻止。这是因为两个网络都显示视频广告,而视频广告如果在显示前没有预先缓存,则需要时间进行缓冲。

关于c# - 如何停止从 UnityAds 和 Vungle 自动获取/加载广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29934654/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com