Hi
I want to hide admob ads during gameplay.
but bannerView.Hide(); is not working in unity.
Heres is my code.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using GoogleMobileAds.Api;
public class GameController : MonoBehaviour {
public GameObject newRecord;
public Text bestScoreObj;
public Text bestScoreOldObj;
public GameObject bestScoreJpg;
public GameObject quitButton;
public GameObject howtoplay;
public GameObject Timer;
public GameObject score;
public Camera cam;
public GameObject[] apples;
public float timeLeft;
public Text timeText;
public GameObject gameOver;
public GameObject restart;
public GameObject menuSplash;
public GameObject play;
public HeroController heroController;
private float maxWidth;
private bool playing;
// Use this for initialization
void Start () {
if (cam == null) {
cam = Camera.main;
}
playing = false;
Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
float appleWidth = apples[0].GetComponent().bounds.extents.x;
maxWidth = targetWidth.x - appleWidth;
UpdateText ();
RequestBanner();
}
void FixedUpdate (){
if (playing) {
timeLeft -= Time.deltaTime;
if (timeLeft < 0) {
timeLeft = 0;
}
UpdateText ();
}
}
public void PlayGame(){
menuSplash.SetActive (false);
play.SetActive (false);
StartCoroutine (Spawn ());
heroController.ToggleControl (true);
Timer.SetActive (true);
score.SetActive (true);
howtoplay.SetActive (false);
quitButton.SetActive (false);
Screen.sleepTimeout = SleepTimeout.NeverSleep;
bannerView.Hide();
}
IEnumerator Spawn () {
yield return new WaitForSeconds (2.0f);
playing = true;
while (timeLeft > 0) {
GameObject apple = apples[Random.Range (0,apples.Length)];
Vector3 spawnPosition = new Vector3 (
Random.Range (-maxWidth, maxWidth),
transform.position.y,
0.0f
);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (apple, spawnPosition, spawnRotation);
yield return new WaitForSeconds (Random.Range (1.0f,2.0f));
}
yield return new WaitForSeconds (0.0f);
gameOverNow();
}
public void UpdateText (){
timeText.text = "" + Mathf.RoundToInt (timeLeft);
}
void getResultScore(){
int score = PlayerPrefs.GetInt("currentScore");
int highScoreold = PlayerPrefs.GetInt("bestScore");
newRecord.SetActive(false);
if(score > highScoreold){
newRecord.SetActive(true);
}
bestScoreObj.text = "" + score;
bestScoreOldObj.text = "" + highScoreold;
}
void gameOverNow(){
heroController.ToggleControl(false);
bestScoreJpg.SetActive (true);
getResultScore();
Timer.SetActive (false);
score.SetActive (false);
}
private void RequestBanner()
{
#if UNITY_ANDROID
string adUnitId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
AdSize adSize = new AdSize(480,32);
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.Bottom);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
bannerView.LoadAd(request);
}
}
↧