I am using google Admob Native Ad to target specific locations in a game. While following the steps, I encountered the following error:
AdMobManager.cs(37,30): error CS1503: Argument 1: cannot convert from 'string' to 'System.Action<GoogleMobileAds.Api.InitializationStatus>'
The following is where the error is occurring on Mobile.Initialize:
private string idApp, idNative;
// Start is called before the first frame update
void Start(){
idApp = "ca-app-pub-3940256099942544~3347511713";
idNative = "ca-app-pub-3940256099942544/2247696110";
MobileAds.Initialize(idApp);
RequestNativeAd();
}
UPDATE: I did the following which resolved the issue. However, I am unable to view the native ad. I am getting a null reference error:
NullReferenceException: Object reference not set to an instance of an object
It is referring to the following lines that is in the update method:
Texture2D iconTexture = this.adNative.GetIconTexture();
Texture2D iconAdChoices = this.adNative.GetAdChoicesLogoTexture();
string headline = this.adNative.GetHeadlineText();
string cta = this.adNative.GetCallToActionText();
string advertiser = this.adNative.GetAdvertiserText();
adIcon.texture = iconTexture;
adChoices.texture = iconAdChoices;
adHeadline.text = headline;
adAdvertiser.text = advertiser;
adCallToAction.text = cta;
The following images are what I did in Unity:
The following is the code where I am render the Native Ads:
using System.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using System.Threading.Tasks;
using Debug = UnityEngine.Debug;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
public class AdMobManager : MonoBehaviour
{
private NativeAd adNative;
private bool nativeLoaded = false;
private string idApp, idNative;
[SerializeField] GameObject adNativePanel;
[SerializeField] RawImage adIcon;
[SerializeField] RawImage adChoices;
[SerializeField] Text adHeadline;
[SerializeField] Text adCallToAction;
[SerializeField] Text adAdvertiser;
// Start is called before the first frame update
void Start(){
idApp = "ca-app-pub-3940256099942544/6300978111";
idNative = "ca-app-pub-3940256099942544/2247696110";
List<String> deviceIds = new List<String>() { AdRequest.TestDeviceSimulator };
// Add some test device IDs (replace with your own device IDs).
#if UNITY_ANDROID
deviceIds.Add(idApp);
#endif
MobileAds.Initialize(HandleInitCompleteAction);
}
void Update(){
Texture2D iconTexture = this.adNative.GetIconTexture();
Texture2D iconAdChoices = this.adNative.GetAdChoicesLogoTexture();
string headline = this.adNative.GetHeadlineText();
string cta = this.adNative.GetCallToActionText();
string advertiser = this.adNative.GetAdvertiserText();
adIcon.texture = iconTexture;
adChoices.texture = iconAdChoices;
adHeadline.text = headline;
adAdvertiser.text = advertiser;
adCallToAction.text = cta;
//register gameobjects
adNative.RegisterIconImageGameObject(adIcon.gameObject);
adNative.RegisterAdChoicesLogoGameObject(adChoices.gameObject);
adNative.RegisterHeadlineTextGameObject(adHeadline.gameObject);
adNative.RegisterCallToActionGameObject(adCallToAction.gameObject);
adNative.RegisterAdvertiserTextGameObject(adAdvertiser.gameObject);
adNativePanel.SetActive(true); //show ad panel
}
private void HandleInitCompleteAction(InitializationStatus initstatus) {
Debug.Log("Initialization complete.");
// Callbacks from GoogleMobileAds are not guaranteed to be called on
// the main thread.
// In this example we use MobileAdsEventExecutor to schedule these calls on
// the next Update() loop.
MobileAdsEventExecutor.ExecuteInUpdate(() => {
RequestNativeAd();
});
}
private void RequestNativeAd(){
AdLoader adLoader = new AdLoader.Builder (idNative).ForNativeAd().Build();
adLoader.OnNativeAdLoaded += this.HandleOnUnifiedNativeAdLoaded;
adLoader.LoadAd(AdRequestBuild());
}
//events
private void HandleOnUnifiedNativeAdLoaded (object sender, NativeAdEventArgs args){
this.adNative = args.nativeAd;
}
AdRequest AdRequestBuild(){
return new AdRequest.Builder().Build();
}
}