Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单的Unity游戏在admob集成后变慢_C#_Android_Performance_Unity3d_Admob - Fatal编程技术网

C# 简单的Unity游戏在admob集成后变慢

C# 简单的Unity游戏在admob集成后变慢,c#,android,performance,unity3d,admob,C#,Android,Performance,Unity3d,Admob,我制作了一个简单的2d游戏,对象不多,在我的Xperia ST上运行得很好。但当我集成admobs在屏幕上显示横幅和间隙广告时,游戏会变得滞后和缓慢 下面给出了我的admob代码及其使用方法 玩家脚本 using UnityEngine; using System.Collections; public class PlayerMotion : MonoBehaviour { public GameObject gameOverTag,spawner,camera,adObject; Spa

我制作了一个简单的2d游戏,对象不多,在我的Xperia ST上运行得很好。但当我集成admobs在屏幕上显示横幅和间隙广告时,游戏会变得滞后和缓慢

下面给出了我的admob代码及其使用方法

玩家脚本

using UnityEngine;
using System.Collections;

public class PlayerMotion : MonoBehaviour {

public GameObject gameOverTag,spawner,camera,adObject;
SpawnScript spawnScript;
GoogleMobileAdsDemoScript adScript;
// Use this for initialization
void Awake() {
    adObject=GameObject.Find("Ads");// ads is a game object which was kept from main menu screen
    }

void Start () {
    spawnScript= spawner.GetComponent<SpawnScript> ();
    //adScript=camera.GetComponent<GoogleMobileAdsDemoScript> ();//GoogleMobileAdsDemoScript is         the ad script
    adScript=adObject.GetComponent<GoogleMobileAdsDemoScript> ();
    adScript.hideBanner ();
    adScript.requestInterstitial ();
}
// Update is called once per frame
void Update () {
//some more code
            }
    }
public void Movement()
{
    //some code
}

void OnCollisionEnter2D(Collision2D other){
    //some code
}
void OnGUI(){
            if (gameOver) {
                    if((adScript.timesInterstitalRequested)%5==0)
                    adScript.ShowInterstitial ();
                    else
                    adScript.showBanner ();

                    //some more code
            }
    }
}

乍一看,你的逻辑似乎很可靠

不过,我会优化OnGUI函数。因为这是最重要的功能, 在考虑移动硬件时

void OnGUI(){
        if (gameOver) {
                if(adScript.bannerShowing == false) {
                  if((adScript.timesInterstitalRequested)%5==0)
                    adScript.ShowInterstitial ();
                   else
                    adScript.showBanner ();
                }
        }
}
然后在GoogleMobileAddemoscript代码上添加一个横幅显示布尔值,并在可见时使其为真 当旗帜被隐藏时是假的,
在OnGUI中多次调用ShowInterstatal或showBanner是个坏主意,因为它们都在一个线程上运行。

如果将其集成到Unity project中,Google Admob将使用一些非常大的8MB DLL。 常见的反馈是:应用程序启动较慢,并且应用程序首次使用Admob slow加载场景


您可以尝试在Unity应用程序中实现本机Android插件。基本上,该插件是用Java编写的,运行速度很快。

根据经验,我发现格式良好的代码在这方面得到了最好的帮助。@functor我不理解您的确切意思,但您可以看到它只有在gameover为真时才会被调用。。但当游戏结束时,游戏并不慢,当场景加载并首次运行时,游戏就慢了
void OnGUI(){
        if (gameOver) {
                if(adScript.bannerShowing == false) {
                  if((adScript.timesInterstitalRequested)%5==0)
                    adScript.ShowInterstitial ();
                   else
                    adScript.showBanner ();
                }
        }
}