Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zaidm3031 · Jul 20, 2020 at 10:03 AM · androidgameadsadmob

can you please write rewarded ad script

in my game there ios a button called retry last shoot or undo last shoot (ad) the first problem is nutton showing in editor after game over but not showing mobile and the second problem is i dont know how to write rewarded ad script to show after clicking undo last shoot and show ad then retry last place where the player didnt shooted and game over occured

my script

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; using SgLib;

if EASY_MOBILE

sing EasyMobile; #endif

public class GameplayUI : MonoBehaviour { [HideInInspector] public bool stopCountTime; [HideInInspector] public bool isCompleteWithStar;

 [Header("Object References")]
 public GameManager gameManager;
 public GameObject gameButtons;
 public Button btnShoot;
 public Button btnRetry;
 public Button btnNext;
 public Button btnRetryForStar;
 public Button btnRetryLastShoot;
 public Button btnShare;
 public Button btnBack;
 public Image imgStar;
 public Image imgCrown;
 public Text txtGetStarTime;
 public Text txtBestScore;
 public Text txtScore;
 public Text txtLevel;
 public Text txtFinalScoreLabel;
 public Text txtFinalScoreValue;
 public Text txtLevelComplete;
 public Text txtRandomText;

 [Header("Sharing-Specific")]
 public GameObject shareUI;
 public Image sharedImage;

 private float getStarTime;
 private bool stopCheck;

 #if EASY_MOBILE
 void OnEnable()
 {
     Advertising.RewardedAdCompleted += Advertising_RewardedAdCompleted;
 }

 void OnDisable()
 {
     Advertising.RewardedAdCompleted -= Advertising_RewardedAdCompleted;
 }
 #endif

 // Use this for initialization
 void Start()
 {
     if (gameManager.gameEnvironment == GameEnvironment.LEVEL_EDITOR)
     {
         StartCoroutine(CountDownGetStarTime());
         btnRetry.gameObject.SetActive(false);
         return;
     }
        
     if (GameManager.gameMode == GameMode.Quest) //On level mode
     {
         //Hide crown image, best score text and score text
         imgCrown.gameObject.SetActive(false);
         txtBestScore.gameObject.SetActive(false);
         txtScore.gameObject.SetActive(false);

         imgStar.gameObject.SetActive(true);
         txtGetStarTime.gameObject.SetActive(true);
         txtLevel.gameObject.SetActive(true);

         //Show level and start count down 
         txtLevel.text = GameManager.levelLoaded.ToString();
         StartCoroutine(CountDownGetStarTime());
     }
     else if (GameManager.gameMode == GameMode.Endless) //On survival mode
     {
         //Show crown image, best score tex and score text
         imgCrown.gameObject.SetActive(true);
         txtBestScore.gameObject.SetActive(true);
         txtScore.gameObject.SetActive(true);

         imgStar.gameObject.SetActive(false);
         txtGetStarTime.gameObject.SetActive(false);
         txtLevel.gameObject.SetActive(false);

         //Hide level text, get start time text and star image
         txtLevel.gameObject.SetActive(false);
         txtGetStarTime.gameObject.SetActive(false);
         imgStar.gameObject.SetActive(false);
    
     }

     gameButtons.SetActive(true);

     //Hide the setting panel, button next, button retry, button retry for star,
     //button setting and the text when game over or pass level
     txtFinalScoreLabel.gameObject.SetActive(false);
     txtFinalScoreValue.gameObject.SetActive(false);
     txtLevelComplete.gameObject.SetActive(false);
     txtRandomText.gameObject.SetActive(false);

     btnNext.gameObject.SetActive(false);
     btnRetry.gameObject.SetActive(false);
     btnRetryForStar.gameObject.SetActive(false);
     btnRetryLastShoot.gameObject.SetActive(false);
     btnShare.gameObject.SetActive(false);

     shareUI.SetActive(false);
 }
 
 // Update is called once per frame
 void Update()
 {
     if (gameManager.gameEnvironment == GameEnvironment.LEVEL_EDITOR)
     {
         if (gameManager.gameOver || gameManager.passLevel)
         {
             stopCountTime = true;
             btnShoot.gameObject.SetActive(false);
             btnRetry.gameObject.SetActive(true);
         }
         return;
     }
         
     //Show score, best score and update music, sound
     txtScore.text = ScoreManager.Instance.Score.ToString();
     txtBestScore.text = ScoreManager.Instance.HighScore.ToString();

     if (gameManager.gameOver && !stopCheck) //Game over
     {
         stopCheck = true;
         stopCountTime = true;

         //Show the text
         txtLevel.gameObject.SetActive(false);
         txtScore.gameObject.SetActive(false);

         if (GameManager.gameMode == GameMode.Endless)
         {
             txtRandomText.gameObject.SetActive(false);
             txtLevelComplete.gameObject.SetActive(false);
             txtFinalScoreLabel.gameObject.SetActive(true);
             txtFinalScoreValue.text = ScoreManager.Instance.Score.ToString();
             txtFinalScoreValue.gameObject.SetActive(true);

             // Only show share button at end of survival mode or when pass a level
             if (gameManager.enablePremiumFeatures)
             {
                 btnShare.gameObject.SetActive(true);
             }
         }
         else if (GameManager.gameMode == GameMode.Quest)
         {
             txtFinalScoreLabel.gameObject.SetActive(false);
             txtFinalScoreValue.gameObject.SetActive(false);
             txtRandomText.text = gameManager.gameOverTexts[Random.Range(0, gameManager.gameOverTexts.Length - 1)];
             txtLevelComplete.gameObject.SetActive(false);

             // Won't show share button if fail a level
             btnShare.gameObject.SetActive(false);
         }      

         //Hide buttons
         btnShoot.gameObject.SetActive(false);
         btnNext.gameObject.SetActive(false);

         //Show buttons
         btnRetry.gameObject.SetActive(true);

         #if UNITY_EDITOR
         btnRetryLastShoot.gameObject.SetActive(true);
         #elif EASY_MOBILE
         if (AdDisplayer.Instance.CanShowRewardedAd())
         {
             btnRetryLastShoot.gameObject.SetActive(true);
         }
         #endif

     }
     else if (gameManager.passLevel && !stopCheck) //Pass level
     {
         stopCheck = true;

         txtLevel.gameObject.SetActive(false);
         txtScore.gameObject.SetActive(false);

         //Random text
         if (isCompleteWithStar)
             txtRandomText.text = gameManager.passLevelWithStarTexts[Random.Range(0, gameManager.passLevelWithStarTexts.Length - 1)];
         else
             txtRandomText.text = gameManager.passLevelTexts[Random.Range(0, gameManager.passLevelTexts.Length - 1)];

         txtLevelComplete.text = "Level " + GameManager.levelLoaded.ToString() + " Completed!!!";

         //Show text when level passed
         txtLevelComplete.gameObject.SetActive(true);
         txtRandomText.gameObject.SetActive(true);
         txtFinalScoreLabel.gameObject.SetActive(false);
         txtFinalScoreValue.gameObject.SetActive(false);

         //Hide buttons
         btnShoot.gameObject.SetActive(false);
         btnRetry.gameObject.SetActive(false);
         btnRetryLastShoot.gameObject.SetActive(false);

         //Show buttons
         btnNext.gameObject.SetActive(true);

         if (gameManager.enablePremiumFeatures)
         {
             btnShare.gameObject.SetActive(true);
         }

         //Is the level completed without star -> show the retry for star button
         if (!isCompleteWithStar)
             btnRetryForStar.gameObject.SetActive(true);
     }
 }

 IEnumerator CountDownGetStarTime()
 {
     //Wait until gameManager load get star time
     while (getStarTime == 0)
     {
         getStarTime = gameManager.getStarTime;
         yield return null;
     }
  
     while (getStarTime > 0)
     {
         getStarTime = getStarTime - 1;
         float milisecond;
         float starMilisecond = 99;
         float endMilisecond = 0;
         float t = 0;
         while (t < 1f)
         {
             if (stopCountTime)
                 yield break;

             t += Time.deltaTime;
             float fraction = t / 1f;
             milisecond = (int)Mathf.Lerp(starMilisecond, endMilisecond, fraction);
             txtGetStarTime.text = getStarTime.ToString() + "." + milisecond.ToString(); 
             yield return null;
         }
     }
     stopCountTime = true;
     yield return new WaitForSeconds(0.5f);
     txtGetStarTime.gameObject.SetActive(false);
     imgStar.gameObject.SetActive(false);
 }


 public void HandleBackButton()
 {
     SceneManager.LoadScene("FirstScene"); 
 }


 void RetryLastShoot()
 {
     btnShoot.gameObject.SetActive(true);
     btnRetry.gameObject.SetActive(false);
     btnRetryLastShoot.gameObject.SetActive(false);
     btnShare.gameObject.SetActive(false);

     txtRandomText.gameObject.SetActive(false);
     txtLevelComplete.gameObject.SetActive(false);
     txtFinalScoreLabel.gameObject.SetActive(false);
     txtFinalScoreValue.gameObject.SetActive(false);

     if (GameManager.gameMode == GameMode.Endless)
     {
         txtScore.gameObject.SetActive(true);
         txtLevel.gameObject.SetActive(false);
     }
     else if (GameManager.gameMode == GameMode.Quest)
     {
         txtLevel.gameObject.SetActive(true);
         txtScore.gameObject.SetActive(false);
     }

     gameManager.gameOver = false;
     gameManager.passLevel = false;
     stopCheck = false;
     stopCountTime = false;
     gameManager.stopCheck = false;


     foreach (ClockData a in gameManager.listClocksData)
     {
         gameManager.CreateClockByClockData(a);
     }

     gameManager.getStarTime = Mathf.Ceil(float.Parse(txtGetStarTime.text));

     if (GameManager.gameMode == GameMode.Quest)
         StartCoroutine(CountDownGetStarTime());

     gameManager.listClocksData.Clear();
 }

 public void HandleNextButton()
 {
     GameManager.levelLoaded++;
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }

 public void HandleRetryButton()
 {
     SceneManager.LoadScene(SceneManager.GetActiveScene().name);
 }

 public void ShowShareUI()
 {
     #if EASY_MOBILE
     Texture2D texture = ScreenshotSharer.Instance.GetScreenshotTexture();
     
     if (texture != null)
     {
         Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));
         Transform imgTf = sharedImage.transform;
         Image imgComp = imgTf.GetComponent<Image>();
         float scaleFactor = imgTf.GetComponent<RectTransform>().rect.width / sprite.rect.width;
         imgComp.sprite = sprite;
         imgComp.SetNativeSize();
         imgTf.localScale = imgTf.localScale * scaleFactor;
     
         shareUI.SetActive(true);
     }
     #endif
 }

 public void HideShareUI()
 {
     shareUI.SetActive(false);
 }

 public void ShareScreenshot()
 {
     #if EASY_MOBILE
     shareUI.SetActive(false);
     ScreenshotSharer.Instance.ShareScreenshot();
     #endif
 }

 public void ShowRewardedAd()
 {
     #if UNITY_EDITOR
     RetryLastShoot();   // for testing in editor
     #elif EASY_MOBILE
     if (AdDisplayer.Instance.CanShowRewardedAd())
     {
         // Hide the button
         btnRetryLastShoot.gameObject.SetActive(false);
         AdDisplayer.Instance.ShowRewardedAd();
     }
     #endif
 }

 #if EASY_MOBILE
 void Advertising_RewardedAdCompleted(RewardedAdNetwork arg1, AdLocation arg2)
 {
     RetryLastShoot();
 }
 #endif

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

311 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Admob works on an empty project but not on my actual project 0 Answers

Android Ads Error 2 Answers

Regarding Admob Banner Ads position and game layout? 0 Answers

mobclix plugin issue for windows 1 Answer

JAVA_HOME enviroment preferences error with Embedded JDK in unity preferences/external tools 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges