首页 文章

Unity Admobs无法在Android设备上运行

提问于
浏览
0

所以我测试了iOS设备上的广告,它的工作非常好!

但是当Android构建并在Android设备上安装时,Admobs广告甚至都没有显示!

我用来整合广告的Adcontroller脚本!

任何人都可以解释造成这个问题的原因 . 不胜感激!

谢谢

公共类AdController:MonoBehaviour {

public static AdController instance;

private BannerView bannerView;
private InterstitialAd interstitial;


void Awake (){
    MakeSingleton ();
}



// Update is called once per frame
void Start () {


    MakeSingleton ();


    RequestBanner ();
    RequestInterstitial ();

    #if UNITY_ANDROID
    string appId = "ca-app-pub-3454534660788114~3231414565";
    #elif UNITY_IPHONE
    string appId = "ca-app-pub-3454534660788114~6021229439";
    #else
    string appId = "unexpected_platform";
    #endif

    MobileAds.SetiOSAppPauseOnBackground(true);

    // Initialize the Google Mobile Ads SDK.
    MobileAds.Initialize(appId);

}

void MakeSingleton() {
    if (instance != null) {
        Destroy (gameObject);
    } else {
        instance = this;
        DontDestroyOnLoad (gameObject);

    }

}





private void RequestBanner()
{
    // These ad units are configured to always serve test ads.
    #if UNITY_EDITOR
    string adUnitId = "unused";
    #elif UNITY_ANDROID
    string adUnitId = "ca-app-pub-3454534660788114/6674059459";
    #elif UNITY_IPHONE
    string adUnitId = "ca-app-pub-3454534660788114/5829657741";
    #else
    string adUnitId = "unexpected_platform";
    #endif

    // Clean up banner ad before creating a new one.
    if (this.bannerView != null)
    {
    this.bannerView.Destroy();
    }

    // Create a 320x50 banner at the top of the screen.
    this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);

    // Register for ad events.
    RegisterDelegateForBanner ();

    // Load a banner ad.
    this.bannerView.LoadAd(this.CreateAdRequest());
    }


    private void RequestInterstitial()
    {
    // These ad units are configured to always serve test ads.
    #if UNITY_EDITOR
    string adUnitId = "unused";
    #elif UNITY_ANDROID
    string adUnitId = "ca-app-pub-3454534660788114/1152046133";
    #elif UNITY_IPHONE
    string adUnitId = "ca-app-pub-3454534660788114/5254942671";
    #else
    string adUnitId = "unexpected_platform";
    #endif

    // Clean up interstitial ad before creating a new one.
    if (this.interstitial != null)
    {
    this.interstitial.Destroy();
    }

    // Create an interstitial.
    this.interstitial = new InterstitialAd(adUnitId);

    // Register for ad events.
    RegisterDelegateForInterstitial ();


    // Load an interstitial ad.
    this.interstitial.LoadAd(this.CreateAdRequest());
    }

    private AdRequest CreateAdRequest()
    {
    return new AdRequest.Builder()
    .AddTestDevice(AdRequest.TestDeviceSimulator)
    .AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
    .AddKeyword("game")
    .SetGender(Gender.Male)
    .SetBirthday(new DateTime(1985, 1, 1))
    .TagForChildDirectedTreatment(false)
    .AddExtra("color_bg", "9B30FF")
    .Build();
    }

    void RegisterDelegateForBanner(){

    this.bannerView.OnAdLoaded += this.HandleAdLoaded;
    this.bannerView.OnAdFailedToLoad += this.HandleAdFailedToLoad;
    this.bannerView.OnAdOpening += this.HandleAdOpened;
    this.bannerView.OnAdClosed += this.HandleAdClosed;
    this.bannerView.OnAdLeavingApplication += this.HandleAdLeftApplication;

    }

    void UnregisterDelegateForBanner(){

    this.bannerView.OnAdLoaded -= this.HandleAdLoaded;
    this.bannerView.OnAdFailedToLoad -= this.HandleAdFailedToLoad;
    this.bannerView.OnAdOpening -= this.HandleAdOpened;
    this.bannerView.OnAdClosed -= this.HandleAdClosed;
    this.bannerView.OnAdLeavingApplication -= this.HandleAdLeftApplication;

    }

    void RegisterDelegateForInterstitial(){

    this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
    this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
    this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
    this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
    this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;

    }


    void UnRegisterDelegateForInterstitial(){

    this.interstitial.OnAdLoaded -= this.HandleInterstitialLoaded;
    this.interstitial.OnAdFailedToLoad -= this.HandleInterstitialFailedToLoad;
    this.interstitial.OnAdOpening -= this.HandleInterstitialOpened;
    this.interstitial.OnAdClosed -= this.HandleInterstitialClosed;
    this.interstitial.OnAdLeavingApplication -= this.HandleInterstitialLeftApplication;

    }
    public void ShowBanner(){
    bannerView.Show ();
    }


    public void ShowInterstitial(){
    if (interstitial.IsLoaded ()) {

        interstitial.Show ();

    } else {
        RequestInterstitial ();
    }

    }



    public void HandleAdLoaded(object sender, EventArgs args)
    {
    ShowBanner ();
    }

    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
    UnregisterDelegateForBanner ();
    RequestBanner ();
    }

    public void HandleAdOpened(object sender, EventArgs args)
    {

    }

    public void HandleAdClosed(object sender, EventArgs args)
    {
    UnregisterDelegateForBanner ();
    RequestBanner ();

    }

    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
    }






    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {

    }

    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
    UnRegisterDelegateForInterstitial ();
    RequestInterstitial ();

    }

    public void HandleInterstitialOpened(object sender, EventArgs args)
    {
    }

    public void HandleInterstitialClosed(object sender, EventArgs args)
    {

    UnregisterDelegateForBanner ();
    RequestBanner ();

    }

    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
    }

}

1 回答

  • 0

    你能告诉我们这个的输出吗?

    还要检查您是否在ADMOB配置文件中填写了结算信息,除非您填写它,否则您似乎无法使其运行 .

    你似乎也没有打电话,ShowInterstitial()?

相关问题