2014-10-04 82 views
1

我做了一個簡單的2D遊戲,遊戲中沒有多少物體,它在我的Xperia ST上運行得很好。但是,當我整合admobs在屏幕上顯示遊戲橫幅和插頁式廣告遊戲玩法滯後,並且變得緩慢。簡單的Unity遊戲在admob集成後變得緩慢

我對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 
      } 
    } 
} 

下面的代碼是GoogleMobileAdsDemoScript代碼

using System; 
using UnityEngine; 
using GoogleMobileAds; 
using GoogleMobileAds.Api; 

// Example script showing how to invoke the Google Mobile Ads Unity plugin. 
public class GoogleMobileAdsDemoScript : MonoBehaviour 
{ 

private BannerView bannerView; 
private InterstitialAd interstitial; 
public int timesBannerRequested=0,timesInterstitalRequested=0; 

void Awake() { 
    DontDestroyOnLoad (this); 
} 
void Start() 
{ 

} 

public void requestBanner(){ 
    //Requesting BannerView 
    timesBannerRequested = timesBannerRequested + 1; 
    #if UNITY_EDITOR 
    string adUnitId = "ca-app-pub-asdas"; 
    #elif UNITY_ANDROID 
    string adUnitId = "ca-app-pub-asd"; 
    #elif UNITY_IPHONE 
    string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE"; 
    #else 
    string adUnitId = "unexpected_platform"; 
    #endif 
    bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom); 
    AdRequest requestBanner = new AdRequest.Builder().Build(); 
    bannerView.LoadAd(requestBanner); 
    //bannerView.Hide(); 
} 

public void requestInterstitial(){ 
    timesInterstitalRequested = timesInterstitalRequested + 1; 
    //Requesting Interstitial 
    #if UNITY_EDITOR 
    string adUnitIdInterstitial = "ca-app-pub-dfada"; 
    #elif UNITY_ANDROID 
    string adUnitIdInterstitial = "ca-app-pub-asdas"; 
    #elif UNITY_IPHONE 
    string adUnitIdInterstitial = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE"; 
    #else 
    string adUnitIdInterstitial = "unexpected_platform"; 
    #endif 
    interstitial = new InterstitialAd(adUnitIdInterstitial); 
    AdRequest requestInterstitial = new AdRequest.Builder().Build(); 
    interstitial.LoadAd(requestInterstitial); 
} 

public void showBanner() 
{ 
    bannerView.Show(); 
} 
public void destroyBanner() 
{ 
    bannerView.Destroy(); 
    } 

public void hideBanner() 
{ 
    bannerView.Hide(); 
    } 
public void ShowInterstitial() 
{ 
    if (interstitial.IsLoaded()) 
    { 
     interstitial.Show(); 
    } 
    else 
    { 
     print("Interstitial is not ready yet."); 
    } 
} 
} 
+0

從經驗中我發現格式良好的代碼在這裏得到了最好的幫助。 – FunctionR 2014-10-04 22:42:05

+0

@FunctionR我不明白你究竟是什麼意思 – 2014-10-05 00:39:24

回答

0

你的邏輯似乎固體從第一眼,

然而,我會優化OnGUI功能。因爲這是所有的最重要的功能,當考慮到移動硬件時,就是 。

void OnGUI(){ 
     if (gameOver) { 
       if(adScript.bannerShowing == false) { 
        if((adScript.timesInterstitalRequested)%5==0) 
        adScript.ShowInterstitial(); 
        else 
        adScript.showBanner(); 
       } 
     } 
} 

然後在GoogleMobileAdsDemoScript代碼添加bannerShowing布爾值,使其真正在可見 和虛假的旗幟,是時隱時現, 它是一個壞主意,打電話ShowInterstital或showBanner多次在OnGUI,因爲它所有的運行上一個單一的線程。

+0

但是在這裏,你可以看到它的唯一被稱爲當gameover是真實的..但是當遊戲結束時,遊戲並不慢,當場景加載並運行時,遊戲速度很慢只有第一次 – 2014-10-07 15:19:25