Good day, it is not clear how to implement the following task: there is advertising, it should be called up in the game once a day, the next day, the next day, the advertisement is again available for viewing. That is, they clicked on the "1" button, an advertisement appeared, on the second press there will be other actions, after the new day comes, the advertisement is shown once again and so around, tell me how to implement this, I would appreciate the sample code or sources for this question .

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class Day : MonoBehaviour { public Text textNow; public void CheckDate() { DateTime now = DateTime.Parse ((int)PlayerPrefs.GetString ("Session")); if (now < (int)DateTime.Now.ToString()) { textNow.text = "Можем показывать рекламу"; } else textNow.text = "Превышен лимит на день"; } public void setSaveDate() { PlayerPrefs.SetString ("Session", DateTime.Now.ToString("dd")); CheckDate (); } } 
  • But there is no possibility to save the date of the last show, and if a day has passed from this date, then show it again? - tym32167
  • This is configured at the level of ad networks, and not in the code. - Suvitruf
  • there was an option to simply compare the day, if the day that was memorized is less than the current one, then we allow to show ads .. but I didn’t understand the code very well, I remember the string 'DateTime.Now.ToString ("dd")' and how to convect an integer to compare, I don’t know, added the code to the edits. Advertising network is new, I was told this manually in my code to write .. - GR1995

2 answers 2

Something like this calculates the time span for your task.

  private DateTime timeLeft { get; set; } private void Rec() { TimeSpan timeSpan = timeLeft.AddDays(1).Subtract(DateTime.Now); if (timeSpan > TimeSpan.Zero) { // показать рекламу } } 

where timeLeft is the last time an ad was shown.

you can encrypt it and set it in the session to the user, or save it on the server

  • May be TimeSpan timeSpan = DateTime.Now.Subtract(timeLeft.NowAddDays(1)); ? - Dmitry Polyanin
  • In my opinion, you will have a negative result, so I wrote it. Now it is more than last + 1, you subtract more from smaller and get a negative number. - Dmitry Polyaninin
  • An example of an error is yours, I just changed parts in places. - Dmitry Polyanin
  • my code: TimeSpan timeSpan = timeLeft.AddDays(1).Subtract(DateTime.Now); YOUR CODE: TimeSpan timeSpan = DateTime.Now.Subtract(timeLeft.NowAddDays(1)); - Digital Core
  • Yes, you wrote the subtract correctly, checked it was my mistake. But the code for the test of your example - ideone.com/Z15rtI we see that it shows an advertisement after 5 minutes ago it showed one - Dmitry Polyanin

Maybe it makes sense to store another variable?

 public bool advShown = false; public void CheckDate() { DateTime now = DateTime.Parse ((int)PlayerPrefs.GetString ("Session")); if (now < (int)DateTime.Now.ToString() && advShown == false) { textNow.text = "Можем показывать рекламу"; advShown = true; } else textNow.text = "Превышен лимит на день"; }