Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Showcase Cool way to test video ads in the Unity Editor

Discussion in 'Unity Ads & User Acquisition' started by krupps, Nov 10, 2022.

  1. krupps

    krupps

    Joined:
    Oct 17, 2017
    Posts:
    159
    I needed a way to test ads in the editor without being on the mobile device. I thought I would share, because I was getting a lockup on the mobile with no real logs for why.

    The video ad is always on the default layer. And if you have more than 1 button on the default layer, then probably move it off. Although most buttons will be on the UI Layer.

    Use this code block and it will close the video window and complete the video



    Code (CSharp):
    1.        
    2. Advertisement.Show(placementId, this);
    3.  
    4. #if DEBUG
    5.         StartCoroutine(ExecuteAfterDelay(3, () =>
    6.         {
    7.             var objects = FindObjectsOfType<Button>();
    8.             foreach (Button go in objects)
    9.             {
    10.                 if (go.name == "Close" && go.gameObject.layer == 0 ) // Default layer is 0
    11.                 {
    12.                     go.onClick.Invoke();
    13.                     OnUnityAdsShowComplete(_adUnitId, UnityAdsShowCompletionState.COMPLETED);
    14.                 }
    15.             }
    16.         }));
    17. #endif
    18.  
    19.   public IEnumerator ExecuteAfterDelay(float seconds, Action action)
    20.     {
    21.         yield return new WaitForSeconds(seconds);
    22.  
    23.         action();
    24.     }
    25.