菜鸟教程小白 发表于 2022-12-13 14:27:25

c# - Unity AdMob 广告未显示


                                            <p><p>我正在 Unity 5.5 上制作游戏,我关注了 <a href="https://firebase.google.com/docs/admob/unity/start" rel="noreferrer noopener nofollow" title="Google&#39;s Official AdMob Getting Started Guide">Google&#39;s Official AdMob Getting Started Guide</a>将 AdMob 集成到我的游戏中(目前仅限 iOS,但当我开始使用此功能时,Android 也会随之而来)。</p>

<ul>
<li>我在 AdMob 控制台上设置了我的游戏,并制作了一个基于奖励的视频广告。</li>
<li>我已将 <code>GoogleMobileAds.framework</code> 添加到我在 Xcode 中构建的项目中。</li>
<li>我已确保我也在链接新添加的框架。</li>
<li>下载了 Unity 包,并根据需要将其集成到我的项目中。</li>
<li>我已经连接到事件处理程序:我的广告加载没有问题,没有失败。</li>
</ul>

<p>当我尝试在 iOS 上展示广告时,<code>ad.IsLoaded();</code> 会返回 false,即使它刚刚加载(我的广告的 <code>OnAdLoaded</code> 事件正在触发)。在 Unity 编辑器中,我看到预期的虚拟方法以正确的顺序被调用(尽管没有显示任何内容,我认为这是 Unity 自己的编辑器的预期行为)。</p>

<p>这是我用于加载广告的代码(当然,我的发布商和广告单元 ID 已编辑):</p>

<pre><code>public class AdManager : MonoBehaviour {

    static RewardBasedVideoAd ad;
    static UIManager uiManager;

    #if UNITY_ANDROID
    static string adUnitId = &#34;ca-app-pub-XXX/YYY&#34;;
    #elif UNITY_IPHONE
    static string adUnitId = &#34;ca-app-pub-XXX/YYY&#34;;
    #else
    static string adUnitId = &#34;unexpected_platform&#34;;
    #endif

    static int headstartPoints;


    // Use this for initialization
    void Start () {
      uiManager = GameObject.Find(&#34;Scripts&#34;).GetComponent&lt;UIManager&gt;();
      ad = RewardBasedVideoAd.Instance;
      ad.OnAdRewarded += Ad_OnAdRewarded;
      ad.OnAdClosed += Ad_OnAdClosed;
      ad.OnAdFailedToLoad += Ad_OnAdFailedToLoad;
      ad.OnAdLoaded += Ad_OnAdLoaded;
      headstartPoints = 0;
      RequestNewAd();
    }

    void Ad_OnAdFailedToLoad (object sender, AdFailedToLoadEventArgs e)
    {
      Debug.Log(&#34;Ad failed to load.&#34;);
    }

    void Ad_OnAdLoaded (object sender, System.EventArgs e)
    {
      Debug.Log(&#34;Ad loaded.&#34;);
    }

    void Ad_OnAdClosed (object sender, System.EventArgs e)
    {
      Debug.Log(&#34;Ad was closed, proceeding to game without rewards...&#34;);
    }

    void Ad_OnAdRewarded (object sender, Reward e)
    {
      Debug.Log(&#34;Ad rewarded.&#34;);
      headstartPoints = (int)e.Amount;
    }

    public static int GetAndConsumeRewards(){
      int points = headstartPoints;
      headstartPoints = 0;
      return points;
    }

    public static void RequestNewAd(){
      Debug.Log(&#34;Requested new ad.&#34;);
      AdRequest request = new AdRequest.Builder().Build();
      ad.LoadAd(request, adUnitId);
    }

    public static void DisplayAdOrProceed(){
      if(ad.IsLoaded()){
            ad.Show();
            Debug.Log(&#34;Ad was loaded, displaying ad...&#34;);
      }else{
            Debug.Log(&#34;Ad wasn&#39;t loaded, skipping ad...&#34;);
            uiManager.ProceedToGame();
      }
    }

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

    }
}
</code></pre>

<p>我从未见过 <code>OnAdFailedToLoad</code> 方法被调用,而且我总是看到 <code>OnAdLoaded</code> 被调用,所以那里似乎没有问题。但是当我检查 <code>ad.IsLoaded()</code> <em>after</em> 广告加载后,由于某种奇怪的原因它是错误的。请注意,<code>RewardBasedVideoAd</code> 是 Google 文档中所述的单例对象。</p>

<p>我做错了什么?</p>

<p><strong>更新:</strong>我在应用程序启动后立即调用 <code>RequestNewAd()</code> 并且在每个“级别”启动时(认为它就像一个级别大约是 ~30秒)和 <code>DisplayAdOrProceed()</code> 死亡(例如关卡结束)。当调用 <code>DisplayAdOrProceed()</code> 时,总是调用广告的加载方法(除非我有连接问题,但这里不是这种情况)。</p>

<p><strong>更新 2:</strong> 刚刚注意到“广告加载”事件中有一个非常可疑的堆栈跟踪:</p>

<pre><code>Ad loaded.
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
AdManager:Ad_OnAdLoaded(Object, EventArgs)
System.EventHandler`1:Invoke(Object, TEventArgs)
GoogleMobileAds.Api.RewardBasedVideoAd:&lt;RewardBasedVideoAd&gt;m__8(Object, AdFailedToLoadEventArgs)
System.EventHandler`1:Invoke(Object, TEventArgs)
GoogleMobileAds.iOS.RewardBasedVideoAdClient:RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback(IntPtr, String)
</code></pre>

<p>我可以看到广告加载事件是从一个为失败处理程序命名的方法触发的(<code>RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback</code>, <code>GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs)</code>)。但是,它调用的是广告加载事件,而不是任何故障处理程序。我不知道发生了什么,也不知道这是否是设计不当的命名约定。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>好吧,在深入研究 Xcode 项目中 IL2CPP 生成的 C++ 代码之后,我意识到 SDK 调用了不正确的方法。广告无法加载并出现错误“没有要显示的广告”,但错误地启动了广告加载方法。我将对我的代码进行额外的更改,以检测它是否被正确加载。</p>

<p>可怕的错误(或设计决策),谷歌。我希望这个问题能尽快得到解决。</p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - Unity AdMob 广告未显示,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40974054/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40974054/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - Unity AdMob 广告未显示