2016-03-16 3 views
0

Привет, мои объявления единства работают, но не останавливаются, когда я нажимаю кнопку закрытия, она просто загружает другое объявление. Как я могу сделать это, чтобы он прекратил играть после одного объявления и продолжал загружать меню?Объявления Unity Never End. Они Loop

Я ОЧЕНЬ новичок в C# FYI.

using UnityEngine; 
using System.Collections; 
using UnityEngine.Advertisements; 

//GameOver page when the plane is destroyed 
public class GameOverPage : MonoBehaviour { 

public GUISkin skin; //skin for button styles 
public static int start; //static integer indicates to show or hide Start/GameOver page 
public static bool running; //static variable indicates if the plane is destroyed or not 


void OnGUI(){ 
      GUI.skin = skin; 

      //if start is not equal to 0 and running is false then show GameOver page buttons 
      if ((StartPage.start != 0) && !PlaneMovement.running) { 
     if (Advertisement.IsReady()) { 
      Advertisement.Show(); 

     } 



     if (GUI.Button (new Rect (Screen.width/2.378f, Screen.height/1.34f, Screen.width/6f, Screen.height/10.10f), "", skin.GetStyle ("Restart"))) { 
          Application.LoadLevel (1); 
          PlaneMovement.running = true; 
        } 
     if (GUI.Button (new Rect (Screen.width/1.50f, Screen.height/1.34f, Screen.width/6.0f, Screen.height/10.1f), "", skin.GetStyle ("Home"))) { 
          Application.LoadLevel (0); 
          PlaneMovement.running = false; 
          StartPage.start = 0; 
        } 
     if (GUI.Button (new Rect (Screen.width/5.7f, Screen.height/1.34f, Screen.width/6f, Screen.height/10.10f), "", skin.GetStyle ("Website"))) { 
          Application.OpenURL ("http://www.skyboxertech.weebly.com"); 
        } 


     } 
    } 
} 

EDIT: Это то, что я имею. Я не уверен, как реализовать таймер, он снова отображает все объявления в цикле и не ограничивает их каждый пятый раз.

using UnityEngine; 
using System.Collections; 
using UnityEngine.Advertisements; 

//GameOver page when the plane is destroyed 
using UnityEngine.SceneManagement; 


public class GameOverPage : MonoBehaviour { 

public GUISkin skin; //skin for button styles 
public static int start; //static integer indicates to show or hide 
bool showAd = true; 
public static bool running; //static variable indicates if the plane is destroyed or not 
public int gameOverCounter = 0; 

void OnGUI(){ 
    GUI.skin = skin; 

    //if start is not equal to 0 and running is false then show GameOver page buttons 
    if ((StartPage.start != 0) && !PlaneMovement.running) { 
     gameOverCounter++; 

     if (gameOverCounter >= 5) { 
      showAd = true; 
     if(showAd){ //Check if we should show add 
      if (Advertisement.IsReady()) { 
       Advertisement.Show(); 
       showAd = false; 
        gameOverCounter = 0; 
       } 
      } 
     } 

     if (GUI.Button (new Rect (Screen.width/2.378f, Screen.height/1.34f, Screen.width/6f, Screen.height/10.10f), "", skin.GetStyle ("Restart"))) { 
      SceneManager.LoadScene (1); 

      PlaneMovement.running = true; 
     } 
     if (GUI.Button (new Rect (Screen.width/1.50f, Screen.height/1.34f, Screen.width/6.0f, Screen.height/10.1f), "", skin.GetStyle ("Home"))) { 
      SceneManager.LoadScene (0); 
      PlaneMovement.running = false; 
      StartPage.start = 0; 
     } 
     if (GUI.Button (new Rect (Screen.width/5.7f, Screen.height/1.34f, Screen.width/6f, Screen.height/10.10f), "", skin.GetStyle ("Website"))) { 
      Application.OpenURL ("http://www.skyboxertech.weebly.com"); 
     } 


    } 
} 
} 

ответ

0

OnGUI() функция называется несколько раз враме. Пока StartPage.start является не и PlaneMovement.running является неправда надстройка всегда будет показывать. Этими двумя условиями являются met. Вот почему объявления показаны. Я не знаю, что эти переменные и что вы используете для них, но это хорошая идея, чтобы показывать ваше объявление каждые 5 раз игра закончилась вместо каждый раз, когда игра открывается. Это annoying и сделает игроков . Delete ваша игра.

Вы можете создать логическую переменную с именем showAdd, которая определяет, когда показывать объявление. Код ниже покажет объявление один раз.

using UnityEngine; 
using System.Collections; 
using UnityEngine.Advertisements; 

//GameOver page when the plane is destroyed 
public class GameOverPage : MonoBehaviour { 

public GUISkin skin; //skin for button styles 
public static int start; //static integer indicates to show or hide 
bool showAd = true; //Will show ad the first time 
public static bool running; //static variable indicates if the plane is destroyed or not 


void OnGUI(){ 
      GUI.skin = skin; 

      //if start is not equal to 0 and running is false then show GameOver page buttons 
     if ((StartPage.start != 0) && !PlaneMovement.running) { 
      if(showAd){ //Check if we should show add   
      if (Advertisement.IsReady()) { 
      Advertisement.Show(); 
      showAd = false; //Set to false So that we dont show ad again 
      } 
     } 


     if (GUI.Button (new Rect (Screen.width/2.378f, Screen.height/1.34f, Screen.width/6f, Screen.height/10.10f), "", skin.GetStyle ("Restart"))) { 
          Application.LoadLevel (1); 
          PlaneMovement.running = true; 
        } 
     if (GUI.Button (new Rect (Screen.width/1.50f, Screen.height/1.34f, Screen.width/6.0f, Screen.height/10.1f), "", skin.GetStyle ("Home"))) { 
          Application.LoadLevel (0); 
          PlaneMovement.running = false; 
          StartPage.start = 0; 
        } 
     if (GUI.Button (new Rect (Screen.width/5.7f, Screen.height/1.34f, Screen.width/6f, Screen.height/10.10f), "", skin.GetStyle ("Website"))) { 
          Application.OpenURL ("http://www.skyboxertech.weebly.com"); 
        } 


     } 
    } 
} 

Вы можете иметь целой переменной, что подсчитывает хау много раз игра была закончена. если он достигает 5, установите showAdd до true и объявление будет показать еще раз.

+0

как бы реализовать целочисленную переменную –

+0

Вернитесь, когда вы напишете свою первую игру, и я предоставлю это. Сейчас я попытаюсь объяснить. Создайте переменную переменную, называемую gameOverCounter, и инициализируйте ее до 0. Когда игра закончена, увеличьте gameOverCounter на 1, выполнив gameOverCounter ++ или gameOverCounter = gameOverCounter + 1. Затем используйте инструкцию if, чтобы проверить, имеет ли gameOverCounter> = 5. Если это так, перезагрузите gameOverCounter до нуля, а затем включите объявление, установив showAd в true. После отображения объявления установите для showAd значение false, чтобы оно не отображалось снова. Это должно происходить снова и снова. Надеюсь, я объяснил это хорошо. – Programmer

+0

Ive отредактировал мой оригинал с новым кодом –

Смежные вопросы