unity3d 无法在C# Unity中从“string”转换为“System.Action< GoogleMobileAds.Api.InitializationStatus>”

ckx4rj1h  于 2022-12-30  发布在  C#
关注(0)|答案(1)|浏览(357)

这是谷歌admob脚本,但Unity给我错误参数1:无法从“string”转换为“System.Action<GoogleMobileAds.Api.InitializationStatus>”。它在void Start()函数中为appID加下划线,并表示参数1:无法从“string”转换为“System.Action<GoogleMobileAds.Api.InitializationStatus>”请帮助我完成此操作。

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds;
using System;

public class addmanager_script : MonoBehaviour 
{
    public BannerView bannerview;
    public InterstitialAd regularad;
    private string appID="ca-app-pub-5133215721502784~7588202759";
    private string regularID="ca-app-pub-5133215721502784/3265814366";
    // Use this for initialization
    void Start () 
    {
        MobileAds.Initialize (appID);
        //RequestAd ();
        RequestBanner ();
    }

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

    }

    public void OnClickShowBanner()
    {
        this.RequestBanner ();
    }

    public void OnClickShowAd()
    {
        this.RequestAd ();
    }

    private void RequestAd()
    {
        regularad = new InterstitialAd (regularID);
        AdRequest request = new AdRequest.Builder ().Build ();
        regularad.LoadAd (request);
    }

    private void RequestBanner()
    {
        #if UNITY_ANDROID
        string bannerID = "ca-app-pub-5133215721502784/6909545908";
        #elif UNITY_IPHONE
        string adUnitId = bannerID;
        #else
        string bannerID  ="ca-app-pub-5133215721502784/6909545908";
        #endif

        bannerview = new BannerView (bannerID, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder ().Build ();
        bannerview.LoadAd (request);
        bannerview.Show ();
        //bannerview.OnAdLoaded += HandleOnAdLoaded;

    }

    void HandleOnAdLoaded(object a, EventArgs args)
    {
        print ("loaded");
        bannerview.Show ();
    }

    public void Show()
    {
        if (regularad.IsLoaded ()) 
        {
           regularad.Show ();
         }

    }

}`

我不明白这个问题的解决方案是什么,因为我是新的编码。

krcsximq

krcsximq1#

docs

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(initStatus => { });
    }
}

所以问题是你不需要传递任何字符串参数给MobileAds.Initialize,而是需要传递一个delegate/action/callback,它通过参数接收一个GoogleMobileAds.Api.InitializationStatus
因此,您可以像上面的示例那样执行此操作,或者类似于:

...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
    public void Start()
    {
        // Initialize the Google Mobile Ads SDK.
        MobileAds.Initialize(OnMobileAdsInitialized);
    }

    private void OnMobileAdsInitialized(InitializationStatus initStatus)
    {
        //Do something with that status
    }
}

相关问题