Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Trigger unity ads after certain time

Discussion in 'Unity Ads & User Acquisition' started by Jointuletz, Dec 23, 2021.

  1. Jointuletz

    Jointuletz

    Joined:
    Feb 9, 2019
    Posts:
    18
    Hi,

    I just update the ads from 3.7.5 to 4.0.0 and had a lot of errors :) But manage to fix them with some help..
    What i want know is to trigger ads after some time... so if you play the game for about 3 minutes let`s say i want to trigger the ads when you finish the game....
    Previously i had this line what was doing this ..

    Code (CSharp):
    1.  void Update()
    2.     {
    3.         timer -= Time.deltaTime;
    4.         ShowAd();
    5.     }
    6.  
    7.     public void ShowAd()
    8.     {
    9.         //Is the time divisible by 90 with no remainder? Has at least 180 seconds passed since the scene was loaded?
    10.         if (Advertisement.IsReady() && Time.time % 120f <= 0.01f && Time.timeSinceLevelLoad > 2)
    11.         {
    12.             Advertisement.Show(myPlacementId);
    13.         }
    14.     }
    Now since IsReady is deprecate i can`t use it anymore... so any help will be verry good.. I just want to trigger the ads after 3 minutes and if he is playing wait till finish and trigger when he dies....
    PS: I`m newbie at coding as well..
     
  2. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Hi @Jointuletz,

    therefore you better use a coroutine. Update is not very performant and not as handy like coroutines for stuff like this. Here is a small coroutine example:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         StartMySimpleTimer();
    4.     }
    5.  
    6.     /// <summary>
    7.     /// warapper function to call the coroutine
    8.     /// </summary>
    9.     void StartMySimpleTimer()
    10.     {
    11.         float secondsToWait = 180;
    12.         StartCoroutine(MySimpleTimer(secondsToWait));
    13.     }
    14.  
    15.     /// <summary>
    16.     /// Fire the OnTimeOut event after the time out duration has passed.
    17.     /// </summary>
    18.     IEnumerator MySimpleTimer(float seconds)
    19.     {
    20.         yield return new WaitForSeconds(seconds);
    21.         // below here the code you want to execute after seconds.
    22.     }
    Always remember: A coroutine is executet only when you call it as argument of StartCoroutine which is a MonoBehaviour function. If you just call MySimpleTimer(secondsToWait) nothing will happen.

    Greetings David
     
    Jointuletz likes this.
  3. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    @Jointuletz If you want you also can use my timer class for it:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Events;
    4.  
    5. public class Timer
    6. {
    7.     float duration;
    8.     IEnumerator coroutine;
    9.     UnityEvent onTimePassed = new UnityEvent();
    10.     WaitForSeconds waitForSeconds;
    11.  
    12.     /// <summary>
    13.     /// The timer event.
    14.     /// </summary>
    15.     public UnityEvent OnTimePassed => onTimePassed;
    16.  
    17.     /// <summary>
    18.     /// A MonoBehaviour reference.
    19.     /// </summary>
    20.     MonoBehaviour MonoBehaviour { get; set; }
    21.  
    22.     /// <summary>
    23.     /// Has the time out started and not allready finnished or stopped?
    24.     /// </summary>
    25.     public bool IsRunning { get; private set; }
    26.  
    27.     /// <summary>
    28.     /// The duration of the timer. A running timer is stopped on set.
    29.     /// </summary>
    30.     public float Duration
    31.     {
    32.         get => duration;
    33.         set
    34.         {
    35.             if (IsRunning)
    36.             {
    37.                 StopTimer();
    38.             }
    39.             waitForSeconds = new WaitForSeconds(value);
    40.             duration = value;
    41.         }
    42.     }
    43.  
    44.     /// <summary>
    45.     /// A timer needs a defined duration to wait and a monoBehaviour reference to perform a coroutine to wait the duration.
    46.     /// </summary>
    47.     /// <param name="duration"></param>
    48.     /// <param name="monoBehaviour"></param>
    49.     public Timer(float duration, MonoBehaviour monoBehaviour)
    50.     {
    51.         MonoBehaviour = monoBehaviour;
    52.         Duration = duration;
    53.         coroutine = DoTrackTime();
    54.     }
    55.  
    56.     /// <summary>
    57.     /// Start the timer and wait the duration, then the OnTimePassed event will fire.
    58.     /// </summary>
    59.     public void StartTimer()
    60.     {
    61.         Debug.Assert(!IsRunning, "The timer is tried to be startet but is running already, try to prevent this.");
    62.         if (!IsRunning)
    63.         {
    64.             IsRunning = true;
    65.             MonoBehaviour.StartCoroutine(coroutine);
    66.         }
    67.     }
    68.  
    69.     /// <summary>
    70.     /// Stop the timer.
    71.     /// </summary>
    72.     public void StopTimer()
    73.     {
    74.         Debug.Assert(IsRunning, "The timer is tried to be stopped but wasn't running anymore, try to prevent this.");
    75.         if (IsRunning)
    76.         {
    77.             IsRunning = false;
    78.             MonoBehaviour.StopCoroutine(coroutine);
    79.         }
    80.     }
    81.  
    82.     /// <summary>
    83.     /// Fire the OnTimeOut event after the time out duration has passed.
    84.     /// </summary>
    85.     IEnumerator DoTrackTime()
    86.     {
    87.         yield return waitForSeconds;
    88.         OnTimePassed.Invoke();
    89.         IsRunning = false;
    90.     }
    91. }
    Inside a monobehaviour derived class you can use it like this

    Code (CSharp):
    1.         Timer timer = new Timer(10, this);
    2.         timer.OnTimePassed.AddListener(() =>
    3.         {
    4.             // do the stuff you want to do after timer ends here.
    5.         });
    6.         timer.StartTimer();
    And in a normal class you need a reference to a monobehaviour instead of using this for the timer constructor. Therefore you can create a script called MonoBehaviourRef like below and put it on an active GameObject in your scene. After that you can call MonoBehaviourRef.Instance as a reference to a MonoBehaviour.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MonoBehaviourRef : MonoBehaviour
    4. {
    5.     public static MonoBehaviourRef Instance;
    6.  
    7.     private void Awake()
    8.     {
    9.         Instance = this;
    10.     }
    11. }
    Greetings.

    p.s. you can delete the Debug.Assert statements, I just wanted to have those conditions but you don't need them.
     
    Last edited: Dec 24, 2021
    Jointuletz likes this.
  4. Jointuletz

    Jointuletz

    Joined:
    Feb 9, 2019
    Posts:
    18
    Hi David,

    Thanks for the reply, tested and is ok. But in the meantime i changed it to show ads every 4 time the scene is loaded :)
    I think is better, and thanks for the timer class !
     
    D12294 likes this.
  5. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Your welcome. I try it with a combination of every third lost game but at least 90 seconds between two ads, for the case that games are lost too fast. A game in my app lasts about 2-60 seconds :) But I'm not an expert for good advertising practice so it's just what I thought what could work for my app and what I've seen in other apps.
     
    Jointuletz likes this.
  6. Jointuletz

    Jointuletz

    Joined:
    Feb 9, 2019
    Posts:
    18
    Hehe, sound like a very good idea to implement as well..
    Don`t` worry, I just start coding and doing games like 1year ago...
    I'm still " useless " and very nob at coding...
    And about Advertising... pfff probably I don`t know anything :)) just implement the ads and see what will happen :))
    I just implement Mediation now from unity, just replace normal ads...see how it goes ..
     
    D12294 likes this.