Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by CubePhysics · May 10, 2016 at 07:06 PM · movementgamebooleanadvertisinginteger

Show an ad once every 2 times

An advertisement from admob shows in my android app every time the user loses. However I want it to appear after the loss once every 2 loses. E.g i died and the ad showed the next time i died it didnt show after that it showed and so on. It takes a gap and then shows again after.

      //Setting up the ad
     InterstitialAd interstitial;
         private void RequestInterstitial()
         {
             #if UNITY_ANDROID
             string adUnitId = "ca-app-pub-5920324855307233/4763839506";
   
             #elif UNITY_IPHONE
                 string adUnitId = "ca-app-pub-5920324855307233/4763839506";
                 #else
                 string adUnitId = "unexpected_platform";
                 #endif
                 
                 // Initialize an InterstitialAd.
                 interstitial = new InterstitialAd(adUnitId);
                 // Create an empty ad request.
                 AdRequest request = new AdRequest.Builder().Build();
                 // Load the interstitial with the request.
                 interstitial.LoadAd(request);
             }

     void Start(){
         RequestInterstitial();
     }

 void Update(){
     if (interstitial.IsLoaded()) {
         interstitial.Show();
          }
      }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by pankajb · May 10, 2016 at 08:37 PM

Something like this should solve your issue

 void Start(){
 
   PlayerPrefs.SetInt("ShouldShowAd", 0);
 }

 void ShowAd(){
  if(PlayerPrefs.GetInt("ShouldShowAd") == 0)
   {
        PlayerPrefs.SetInt("ShouldShowAd", 1);
         // do nothing and don't show ad
   }
 else
   {
       PlayerPrefs.SetInt("ShouldShowAd", 0);
       // show ad here
   }
 }

 // call show ad upon player death

Comment
Add comment · Show 3 · Share
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
avatar image Jjules · May 10, 2016 at 08:59 PM 0
Share

@pankajb this is absolutely the standard way to go about it, and it even has the added benefit that it saves whether or not it is time for an ad between play sessions. There is, however, one flaw to consider; PlayerPrefs aren't secure at all. At. All. Particularly on Android. PlayerPref variables are essentially stored in a text file that anyone with the right tools could go in and edit, and it isn't incredibly hard to find those tools, either. If they wanted, they could potentially edit the PlayerPref variable to never see an ad in your game, and although it isn't common by any means, it does happen. If this doesn't bother you, or you're confident in your own form of d.i.y. PlayerPref decryption, then by all means, go for it. It is an awesomely simple and effective way to do this. I just want you to be aware of the drawbacks.

avatar image Munchy2007 Jjules · May 10, 2016 at 11:13 PM 0
Share

Whilst this is potentially true, they would have to edit the playerprefs after every death to avoid seeing ads. It would be less hassle just to close the ad when it shows.

Also, your post was a comment and not an answer so you should convert it to a comment.

avatar image Jjules Munchy2007 · May 11, 2016 at 12:36 AM 0
Share

I'm looking at the broader aspect of this answer. It would be annoying and tedious to do so, but some people don't care. Plus, you have to think about people who see this answer and say something like "i'm gonna use this answer, but i'll do it once every 4 games" or something like that. Then it would be a lot more likely. Or if the person who asked the question uses this, says "PlayerPrefs are great, i'm also gonna implement them here and here as well" not knowing what he's getting into. You have to be aware of how much reach an answer on this site has

Also, I clicked reply, but it still posted as an answer. I'll try to convert it

avatar image
1

Answer by Munchy2007 · May 10, 2016 at 11:08 PM

My suggestion would be to do it like this (the method I use myself).

Player dies

Check interstitial is ready

If it is then show it (but don't request another)

If it isn't then request an interstitial (but don't show it.)

Rinse and repeat

This results in an interstitial being shown every two deaths, with no need for counters, playerprefs saving etc. etc.

Comment
Add comment · Share
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

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

68 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

Related Questions

Changing Booleans Throught Certain Distance 1 Answer

I am trying to make a movement script for my main camera but it isnt working! 2 Answers

How to store a boolean for each variable in a class 1 Answer

Setting boundary for Z axis 1 Answer

How do I do a double Floating joystick? (Direction and movement (one for each))? 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