Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Ayuda Admob

Iniciado por andresblend7, Abril 30, 2018, 12:10:17 AM

Tema anterior - Siguiente tema
Abril 30, 2018, 12:10:17 AM Ultima modificación: Abril 30, 2018, 12:28:50 AM por BAMF
Hola,
 
Soy nuevo en unity pero ya he Manejado Admob en proyectos nativos para Android.
 
Estoy intentando colocar Admob en Unity pero se me ha hecho imposible saber que pasa, no sé si carga o si no carga si hay error o que pasa, ya incluí el Modulo sdk que hay que descargar y sigo los pasos, este es el código.
 


 
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class Ads : MonoBehaviour {
    private BannerView bannerView;
    int lol = 0;
    // Use this for initialization
    void Start () {
        string appId = "id";
       
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(appId);
       
        this.RequestBanner();
    }
    private void RequestBanner()
    {
     
          string adUnitId = "id";
 
        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Center);
        // Register for ad events.
        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;
        // Create an empty ad request.
        AdRequest request = new AdRequest.Builder().Build();
        // Load the banner with the request.
        bannerView.LoadAd(request);
        bannerView.Show(); /// he intentado con esta y SIN esta linea
        
    }
    #region Banner callback handlers
    public void HandleAdLoaded(object sender, EventArgs args)
    {
        print("HandleAdLoaded");
     //   MonoBehaviour.print("HandleAdLoaded event received");
    }
    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleAdFailedToLoad");
      //  MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.Message);
    }
    public void HandleAdOpened(object sender, EventArgs args)
    {
        print("HandleAdOpened");
       // MonoBehaviour.print("HandleAdOpened event received");
    }
    public void HandleAdClosed(object sender, EventArgs args)
    {
        print("HandleAdClosed");
        //MonoBehaviour.print("HandleAdClosed event received");
    }
    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
     //   MonoBehaviour.print("HandleAdLeftApplication event received");
    }
    #endregion
    // Update is called once per frame
    void Update () {
     
     
    }
}

 
ese codigo lo tengo enlazado  al canvas que viene por defecto ( también probé poniendolo en la camera) pero no pasa nada, nada sale en consola ni sé que sucede.
 
exporté la apk y jamás sale el banner, tampoco se crashea ni nada.
 
 
 
o si no es mucho pedir, alguiien tiene un proyecto con admob para editarlo, ya intenté con uno que supuestamente carga banners e interstitial pero se crashea en los móviles u_u"

Abril 30, 2018, 12:41:21 AM #1 Ultima modificación: Abril 30, 2018, 12:42:46 AM por BAMF
Yo lo tengo así, tu ya puedes editarlo como quieras.
 

using System;
using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.Advertisements;
public class AdsController : MonoBehaviour
{
    public static AdsController Instance { set; get; }
    public string bannerId;
    public string fullPanelId;
    public string nativeId;
    public string IOSBannerId;
    public string IOSFullPanelId;
    public string rewardVideoId;
    private BannerView bannerView;
    private InterstitialAd interstitial;
    private NativeExpressAdView nativeExpressAdView;
    private RewardBasedVideoAd rewardBasedVideo;
    private void Awake()
    {
        Instance = this;
    }
    public void RequestAds()
    {
        RequestInterstitial();
        RequestBanner();
    }
    public void RequestRewardDelay(float timeWait)
    {
        StartCoroutine(StartRequest(timeWait));
    }
    IEnumerator StartRequest(float timeWait)
    {
        yield return new WaitForSeconds(timeWait);
        RequestRewardBasedVideo();
    }
    void PreloadRewardVideo()
    {
        // Get singleton reward based video ad reference.
        rewardBasedVideo = RewardBasedVideoAd.Instance;
        // RewardBasedVideoAd is a singleton, so handlers should only be registered once.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
        rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
        rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
        rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
    }
    public void RequestNativeExpressAdView()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        NativeInsize();
        #endif
    }
    void NativeInsize()
    {
       
        // Create a 320x150 native express ad at the top of the screen.
        this.nativeExpressAdView = new NativeExpressAdView(
            nativeId,
            new AdSize(320, 132),
            AdPosition.Center);
        // Register for ad events.
        this.nativeExpressAdView.OnAdLoaded += this.HandleNativeExpressAdLoaded;
        this.nativeExpressAdView.OnAdFailedToLoad += this.HandleNativeExpresseAdFailedToLoad;
        this.nativeExpressAdView.OnAdOpening += this.HandleNativeExpressAdOpened;
        this.nativeExpressAdView.OnAdClosed += this.HandleNativeExpressAdClosed;
        this.nativeExpressAdView.OnAdLeavingApplication += this.HandleNativeExpressAdLeftApplication;
        // Load a native express ad.
        this.nativeExpressAdView.LoadAd(this.createAdRequest());
    }
    public void ShowNative()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        nativeExpressAdView.Show();
        #endif
    }
    public void HideNative(bool isDestroy)
    {
//        #if UNITY_ANDROID && !UNITY_EDITOR
        if (nativeExpressAdView != null)
        {
            nativeExpressAdView.Hide();
            if (isDestroy)
            {
                nativeExpressAdView.Destroy();
            }
        }
//        #endif
    }
    public void RequestBanner()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        RequestBannerInsize();
        #elif UNITY_IOS
        RequestBannerIOSInsize();
        #endif
    }
    public void ShowBanner()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        ShowBannerInsize();
        #elif UNITY_IOS
        ShowBannerInsize();
        #endif
    }
    public void HideBanner()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        if (bannerView != null)
        {
            bannerView.Hide();
            bannerView.Destroy();
        }
        #elif UNITY_IOS
        #endif
    }
    void ShowBannerInsize()
    {
        RequestBanner();
        bannerView.Show();
    }
    void RequestBannerInsize()
    {
        HideBanner();
        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(bannerId, AdSize.Banner, AdPosition.Bottom);
        // Register for ad events.
        bannerView.OnAdLoaded += HandleAdLoaded;
        bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
        bannerView.OnAdLoaded += HandleAdOpened;
        bannerView.OnAdClosed += HandleAdClosed;
        bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
        // Load a banner ad.
        bannerView.LoadAd(createAdRequest());
    }
    void RequestBannerIOSInsize()
    {
        // Create a 320x50 banner at the top of the screen.
        bannerView = new BannerView(IOSBannerId, AdSize.SmartBanner, AdPosition.Bottom);
        // Register for ad events.
        bannerView.OnAdLoaded += HandleAdLoaded;
        bannerView.OnAdFailedToLoad += HandleAdFailedToLoad;
        bannerView.OnAdLoaded += HandleAdOpened;
        bannerView.OnAdClosed += HandleAdClosed;
        bannerView.OnAdLeavingApplication += HandleAdLeftApplication;
        // Load a banner ad.
        bannerView.LoadAd(createAdRequest());
    }
    public void RequestInterstitial()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        RequestInterstitialInsize();
        #elif UNITY_IOS
        RequestInterstitialIOSInsize();
        #endif
    }
    void RequestInterstitialInsize()
    {
        // Create an interstitial.
        interstitial = new InterstitialAd(fullPanelId);
        // Register for ad events.
        interstitial.OnAdLoaded += HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial.OnAdOpening += HandleInterstitialOpened;
        interstitial.OnAdClosed += HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
        // Load an interstitial ad.
        interstitial.LoadAd(createAdRequest());
    }
    void RequestInterstitialIOSInsize()
    {
        // Create an interstitial.
        interstitial = new InterstitialAd(IOSFullPanelId);
        // Register for ad events.
        interstitial.OnAdLoaded += HandleInterstitialLoaded;
        interstitial.OnAdFailedToLoad += HandleInterstitialFailedToLoad;
        interstitial.OnAdOpening += HandleInterstitialOpened;
        interstitial.OnAdClosed += HandleInterstitialClosed;
        interstitial.OnAdLeavingApplication += HandleInterstitialLeftApplication;
        // Load an interstitial ad.
        interstitial.LoadAd(createAdRequest());
    }
 
    // Returns an ad request with custom ad targeting.
    private AdRequest createAdRequest()
    {
        return new AdRequest.Builder()
                .AddTestDevice(AdRequest.TestDeviceSimulator)
                        .AddTestDevice("") // z4
        //.AddTestDevice("") // tab i8
                .AddKeyword("game")
//                .SetGender(Gender.Male)
                .SetBirthday(new DateTime(1985, 1, 1))
                .TagForChildDirectedTreatment(false)
                .AddExtra("color_bg", "9B30FF")
                .Build();
    }
    public void RequestRewardBasedVideo()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        RequestRewardBasedVideoInsize();
        #endif
    }
    void RequestRewardBasedVideoInsize()
    {
        rewardBasedVideo.LoadAd(createAdRequest(), rewardVideoId);
    }
    public void ShowInterstitial()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
        RequestInterstitial();
        #elif UNITY_IOS
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
        }
        RequestInterstitial();
        #endif
    }
    public void ShowRewardBasedVideo()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        if (rewardBasedVideo.IsLoaded())
        {
            rewardBasedVideo.Show();
            RequestRewardDelay(10);
        }
        #endif
    }
    public bool IsRewardVideoLoaded()
    {
        #if UNITY_ANDROID && !UNITY_EDITOR
        return IsRewardVideoLoadedInsize();
        #endif
        return true;
    }
    bool IsRewardVideoLoadedInsize()
    {
        return rewardBasedVideo.IsLoaded();
        ;
    }
 
    // Unity Ads
    public void ShowRewardedAd(ref bool isReady)
    {
        if (Advertisement.IsReady("rewardedVideo"))
        {
            var options = new ShowOptions { resultCallback = HandleShowResult };
            Advertisement.Show("rewardedVideo", options);
            isReady = true;
        }
        else
        {
            isReady = false;
        }
    }
    public bool IsRewardedAdReady()
    {
        if (Advertisement.IsReady("rewardedVideo"))
        {
            return true;
        }
       
        return false;
    }
    private void HandleShowResult(ShowResult result)
    {
        switch (result)
        {
            case ShowResult.Finished:
                Debug.Log("The ad was successfully shown.");
                //
                // YOUR CODE TO REWARD THE GAMER
                // Give coins etc.
                MainCanvas.Main.inGameScript.ShowRewardFinish();
                break;
            case ShowResult.Skipped:
                Debug.Log("The ad was skipped before reaching the end.");
                break;
            case ShowResult.Failed:
                Debug.LogError("The ad failed to be shown.");
                break;
        }
    }
 
    #region Banner callback handlers
    public void HandleAdLoaded(object sender, EventArgs args)
    {
        print("HandleAdLoaded event received.");
    }
    public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleFailedToReceiveAd event received with message: " + args.Message);
    }
    public void HandleAdOpened(object sender, EventArgs args)
    {
        print("HandleAdOpened event received");
    }
    void HandleAdClosing(object sender, EventArgs args)
    {
        print("HandleAdClosing event received");
    }
    public void HandleAdClosed(object sender, EventArgs args)
    {
        print("HandleAdClosed event received");
    }
    public void HandleAdLeftApplication(object sender, EventArgs args)
    {
        print("HandleAdLeftApplication event received");
    }
 
    #endregion
    #region Interstitial callback handlers
    public void HandleInterstitialLoaded(object sender, EventArgs args)
    {
        print("HandleInterstitialLoaded event received.");
    }
    public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
    }
    public void HandleInterstitialOpened(object sender, EventArgs args)
    {
        print("HandleInterstitialOpened event received");
    }
    void HandleInterstitialClosing(object sender, EventArgs args)
    {
        print("HandleInterstitialClosing event received");
    }
    public void HandleInterstitialClosed(object sender, EventArgs args)
    {
        print("HandleInterstitialClosed event received");
    }
    public void HandleInterstitialLeftApplication(object sender, EventArgs args)
    {
        print("HandleInterstitialLeftApplication event received");
    }
    #endregion
    #region RewardBasedVideo callback handlers
    public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoLoaded event received.");
    }
    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
    }
    public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoOpened event received");
    }
    public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoStarted event received");
    }
    public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoClosed event received");
    }
    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        string type = args.Type;
        double amount = args.Amount;
        print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
            type);
    }
    public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
    {
        print("HandleRewardBasedVideoLeftApplication event received");
    }
    #endregion
    #region Native express ad callback handlers
    public void HandleNativeExpressAdLoaded(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleNativeExpressAdAdLoaded event received");
    }
    public void HandleNativeExpresseAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
    {
        MonoBehaviour.print(
            "HandleNativeExpressAdFailedToReceiveAd event received with message: " + args.Message);
    }
    public void HandleNativeExpressAdOpened(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleNativeExpressAdAdOpened event received");
    }
    public void HandleNativeExpressAdClosed(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleNativeExpressAdAdClosed event received");
    }
    public void HandleNativeExpressAdLeftApplication(object sender, EventArgs args)
    {
        MonoBehaviour.print("HandleNativeExpressAdAdLeftApplication event received");
    }
    #endregion
}

 
Recuerda que este script tiene que ir a un gameobject vacío.
 
Nota: Puedes borrar la línea 326
 
Nota 2: Si no usas UnityAds borra la línea desde 290 hasta 338.
 
Espero haberte ayudado!

estas poniendo los ID de la app y las unidades correctamente? no estoy seguro si los ocultaste en el codigo que has compartido (lo q es buena idea para q no abusen de ellos):
 
 
 

   void Start () {
        string appId = "id";
...
 
    private void RequestBanner()
    {
     
          string adUnitId = "id";
...

 
 
 
algo mas si deshabilitas el objeto donde pusiste ese script nunca va a hacer nada

Cita de: Braltor date=1525047004estas poniendo los ID de la app y las unidades correctamente? no estoy seguro si los ocultaste en el codigo que has compartido (lo q es buena idea para q no abusen de ellos):
   
   
       
   
   

   void Start () {
        string appId = "id";
...
 
    private void RequestBanner()
    {
     
          string adUnitId = "id";
...

   
       
   
   
      algo mas si deshabilitas el objeto donde pusiste ese script nunca va a hacer nada
   


 
 
no señor no lo deshabilito. y los ids son los que da admob de prueba

Cita de: andresblend7 date=1525051195 
   
   
      no señor no lo deshabilito. y los ids son los que da admob de prueba
   


entonces xq tienes la cadena "id" en vez de un identificador apropiado? el del banner deberia ser "ca-app-pub-3940256099942544/6300978111" mira la tabla en:
 
https://developers.google.com/admob/android/test-ads

Lol ,alguien lo editó no yo

Cita de: andresblend7 date=1525051949Lol ,alguien lo editó no yo
   


jajaj ok

pero sigo sin solucionar esto :/

Cita de: andresblend7 date=1525052188pero sigo sin solucionar esto :/
   


ese codigo de @BAMF se ve bien... lo probaste? no muestra nada? has intentado poner lanzar un texto a consola justo cuando inicia el script en Start? no das muchos indicios de q has hecho hasta el momento...

No, las ids las oculté yo, pensaba que eran las originales por eso puse id @andresblend7

Etiquetas: