Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Loading Scene After Watching Ad

Discussion in 'Unity Ads & User Acquisition' started by Will_at_BridgeWorxGames, Feb 20, 2020.

  1. Will_at_BridgeWorxGames

    Will_at_BridgeWorxGames

    Joined:
    Feb 20, 2020
    Posts:
    12
    Hello!

    I realized I put this in the wrong spot, so I'm re-writing this in the Unity Ads section and deleting the old I posted in General.

    I'm getting really mixed up in the information I'm needing. I'm making a toddler game where I'm hoping to have a parent/guardian load up the game, watch the ad, and then pass the device to their child.
    1. The parent/guardian loads the game.
    2. The parent/guardian reads the instructions (essentially what you're reading now)
    3. The parent/guardian presses the [Watch ad & Start Game!] button.
    4. The parent/guardian watches the full-screen interstitial ad.
    5. The parent/guardian locks their device to the game only (which is outside the scope of Unity).
    6. The parent/guardian passes the device to their child.
    7. Ads do not show up again at all.
    I currently am using this code below which is really close to what I'm wanting, but the ad is apparently playing in the foreground while the game is playable in the background. Here is a video link of this taking place:



    I recognize I could set this up into a new scene just for the ads, which is fine, that's not the major concern, the major concerns are:
    • I'm getting the debug log error that I set up which states:
      • The ad did not finish due to an error.
        UnityEngine.Debug:LogWarning(Object)
        StartButton:OnUnityAdsDidFinish(String, ShowResult) (at Assets/Scripts/MenuButtons/StartButton.cs:99)
        StartButton:OnUnityAdsReady(String) (at Assets/Scripts/MenuButtons/StartButton.cs:72)
        StartButton:MyAction() (at Assets/Scripts/MenuButtons/StartButton.cs:63)
        UnityEngine.Events.UnityEvent:Invoke()
        StartButton:Update() (at Assets/Scripts/MenuButtons/StartButton.cs:56)
    • I'm getting this error to use a specific way of doing things, but only when I manually write the line 'Advertisement.Show();' :
      • Please consider upgrading to the Packman Distribution of the Unity Ads SDK. The Asset Store distribution will not longer be supported after Unity 2018.3
        UnityEngine.Advertisements.Advertisement:Initialize(String, Boolean)
        StartButton:Awake() (at Assets/Scripts/MenuButtons/StartButton.cs:26)
    • The ad simply doesn't go away, even when the 'skip' button is pressed.

    Here are how my scenes break down by color and you'll see in the video link above that these transitions take place, even in the unintentional direction of going back to the parental instruction scene while the ad is playing by clicking the "skip" button:
    1. Purple (main menu)
    2. Blue (parental instructions)
    3. Green (child's game)
    Here is the code:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine;
    6. using UnityEngine.Advertisements;
    7. using UnityEngine.Events;
    8.  
    9. public class StartButton : MonoBehaviour
    10. {
    11.     public interface IUnityAdsListener
    12.     {
    13.         void OnUnityAdsReady (string AppleGameID);
    14.         void OnUnityAdsDidError (string message);
    15.         void OnUnityAdsDidStart (string placementId);
    16.         void OnUnityAdsDidFinish (string placementId, ShowResult showResult);
    17.     }
    18.  
    19.     string AppleGameID = "3475968";
    20.     //string AndroidGameID = "3475969";
    21.     UnityEvent m_MyEvent = new UnityEvent();
    22.     bool testMode = true, pushAllowed = true;
    23.     ShowResult showResult;
    24.  
    25.     void Awake()
    26.     {
    27.         Advertisement.Initialize (AppleGameID, testMode);
    28.         //Advertisement.Initialize (AndroidGameID, testMode);
    29.     }
    30.  
    31.     void Start()
    32.     {
    33.         //Add a listener to the new Event. Calls MyAction method when invoked
    34.         m_MyEvent.AddListener(MyAction);
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         // Press Q to close the Listener
    40.         if (Input.GetKeyDown("q") && m_MyEvent != null)
    41.         {
    42.             Debug.Log("Quitting");
    43.             m_MyEvent.RemoveListener(MyAction);
    44.  
    45.             #if UNITY_EDITOR
    46.             UnityEditor.EditorApplication.isPlaying = false;
    47.             #endif
    48.  
    49.             Application.Quit();
    50.         }
    51.  
    52.         //Press any other key to begin the action if the Event exists
    53.         if (pushAllowed && Input.anyKeyDown && m_MyEvent != null)
    54.         {
    55.             pushAllowed = false;
    56.             m_MyEvent.Invoke();
    57.         }
    58.     }
    59.  
    60.     void MyAction()
    61.     {
    62.         OnUnityAdsReady(AppleGameID);
    63.     }
    64.  
    65.     void OnUnityAdsReady (string AppleGameID)
    66.     {
    67.         Advertisement.Show();
    68.         OnUnityAdsDidFinish(AppleGameID,showResult);
    69.     }
    70.  
    71.     void OnUnityAdsDidError (string errorMessage)
    72.     {
    73.         SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    74.     }
    75.  
    76.     void OnUnityAdsDidStart (string placementId)
    77.     {
    78.         Debug.Log("The ad started playing.");
    79.     }
    80.  
    81.     void OnUnityAdsDidFinish (string placementId, ShowResult showResult)
    82.     {
    83.         if (showResult == ShowResult.Finished)
    84.         {
    85.             Debug.LogWarning ("The ad finished.");
    86.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    87.         }
    88.         else if (showResult == ShowResult.Skipped)
    89.         {
    90.             Debug.LogWarning ("The ad was skipped.");
    91.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    92.         }
    93.         else if (showResult == ShowResult.Failed)
    94.         {
    95.             Debug.LogWarning ("The ad did not finish due to an error.");
    96.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    97.         }
    98.     }
    99. }
    100.  
    Here has been my journey:
    1. As you can tell this is all sort of pieced together from the following forums and Unity documentation:
    2. This forum is essentially a copy of that, which lead me to the code above:
    3. This forum mentioned a ResultCallBack which is obsolete
    4. This forum mentions what to do with the ResultCallBack error, which helped me with the code above

    Thank you for your time and effort!
     
  2. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    Some suggestions:

    1) Your redefining the IUnityAdsListener, which is not something you should be doing...this is probably a bad idea, and you should just use the implementation that comes with our SDK.

    2) you seem to pass a method into the AddListener callback. You should be passing in an object that implements the IUnityAdsListener callback. Try having your StartButton class extend IUnityAdsListener and implement its callbacks. You also add the listener much later in a start method after init was called in awake. You could miss callbacks between when you init and when you add the listener. you should be doing those actions around the same time so you capture all callbacks. In fact you can add the listener first then call init which will make sure you get all callbacks.

    3) You call OnUnityAdsReady() yourself inside the MyAction function. You should NEVER invoke the callback methods yourself. We invoke those methods from the sdk so that you can take action when appropriate. Also, the callback method expects a placementId as the parameter not your appId, so the value your calling it with was wrong as well.

    If your still seeing issues after fixing these issues please include the unity version you using, the sdk version, and the specific error with stacktrace if possible. If there is not actual error/stacktrace you just not getting ads, then make sure you include the debug logs (adb logcat or xcode debug logs) so we can see what is happening.
     
  3. Will_at_BridgeWorxGames

    Will_at_BridgeWorxGames

    Joined:
    Feb 20, 2020
    Posts:
    12
    Hey @sbankhead Thank you for your reply. I do not know how to do what you're suggesting. If you have the time, I'd really appreciate you holding my hand.
    Here's what I think you're saying:
    1a) Are you suggesting that I remove lines 11-17 from the code above?
    1b) I don't know what "just use the implementation that comes with our SDK" means. Can you help me better understand how to implement those...thingies?
    2a) I don't know what "Try having your StartButton class extend IUnityAdsListener and implement its callbacks" means. Do you mean change line 9 as the following?:
    Code (CSharp):
    1.  
    2. public class StartButton : MonoBehaviour, IUnityAdsLinstener
    3.  
    When I do this, I get the following error message and cannot compile:
    "Assets/Scripts/MenuButtons/StartButton.cs(8,43): error CS0737: 'StartButton' does not implement interface member 'IUnityAdsListener.OnUnityAdsReady(string)'. 'StartButton.OnUnityAdsReady(string)' cannot implement an interface member because it is not public."
    2b) Are you suggesting to swap lines 27 and 34?
    3a) Are you suggesting to remove line 62? What would I then put in the MyAction() method? I got this code from this Unity Documentation but not sure how to "glue together" what that document states and your suggestions.
    https://docs.unity3d.com/ScriptReference/Events.UnityEvent.AddListener.html
    Can you please help me better understand what I should do with this MyAction function?
    3b) I just figured out what the placement ID is. After following the steps of this documentation makes it
    https://unityads.unity3d.com/help/monetization/placements, I thought that the Game IDs were the placement IDs, because they're right front-and-center, but I figured out I needed to scroll down slightly to see the add button...

    But okay I got my functions, but now where do I put them? In the arguments of the functions prototypes? Since I have no where I'm actually calling these functions, I'm not sure where to place the PlacementID since I assume they're being called by value somwhere else. Is this in the Inspector somewhere?
    Here's what I have so far with your suggestions (Or at least what I think your suggestions are)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5. using UnityEngine.Advertisements;
    6. using UnityEngine.Events;
    7.  
    8. public class StartButton : MonoBehaviour, IUnityAdsListener
    9. {
    10.  
    11.     string AppleGameID = "3475968";
    12.     //string AndroidGameID = "3475969";
    13.     UnityEvent m_MyEvent = new UnityEvent();
    14.     bool testMode = true, pushAllowed = true;
    15.     ShowResult showResult;
    16.  
    17.     void Awake()
    18.     {
    19.         //Add a listener to the new Event. Calls MyAction method when invoked
    20.         m_MyEvent.AddListener(MyAction);
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.         Advertisement.Initialize (AppleGameID, testMode);
    26.         //Advertisement.Initialize (AndroidGameID, testMode);
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         // Press Q to close the Listener
    32.         if (Input.GetKeyDown("q") && m_MyEvent != null)
    33.         {
    34.             Debug.Log("Quitting");
    35.             m_MyEvent.RemoveListener(MyAction);
    36.  
    37.             #if UNITY_EDITOR
    38.             UnityEditor.EditorApplication.isPlaying = false;
    39.             #endif
    40.  
    41.             Application.Quit();
    42.         }
    43.  
    44.         //Press any other key to begin the action if the Event exists
    45.         if (pushAllowed && Input.anyKeyDown && m_MyEvent != null)
    46.         {
    47.             pushAllowed = false;
    48.             m_MyEvent.Invoke();
    49.         }
    50.     }
    51.  
    52.     void MyAction()
    53.     {
    54.         //Not sure what to put here
    55.     }
    56.  
    57.     void OnUnityAdsReady (string AppleGameID)
    58.     {
    59.         Advertisement.Show();
    60.         //OnUnityAdsDidFinish(AppleGameID,showResult);
    61.     }
    62.  
    63.     void OnUnityAdsDidError (string errorMessage)
    64.     {
    65.         SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    66.     }
    67.  
    68.     void OnUnityAdsDidStart (string LevelIntro)
    69.     {
    70.         Debug.Log("The ad started playing.");
    71.     }
    72.  
    73.     void OnUnityAdsDidFinish (string LevelIntro, ShowResult showResult)
    74.     {
    75.         if (showResult == ShowResult.Finished)
    76.         {
    77.             Debug.LogWarning ("The ad finished.");
    78.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    79.         }
    80.         else if (showResult == ShowResult.Skipped)
    81.         {
    82.             Debug.LogWarning ("The ad was skipped.");
    83.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    84.         }
    85.         else if (showResult == ShowResult.Failed)
    86.         {
    87.             Debug.LogWarning ("The ad did not finish due to an error.");
    88.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    89.         }
    90.     }
    91. }
    92.  
    I'm on Unity Version 2019.3.0f6 Personal. I just downloaded the SDK from the asset store yesterday. I'm not sure how to find the SDK version, nor did Google help me find that. Can you please give me directions on where to find it? I've been copying and pasting the errors from the console. What are the stacktrace and how do I get you those?

    Thank you again for your time!
     
  4. Will_at_BridgeWorxGames

    Will_at_BridgeWorxGames

    Joined:
    Feb 20, 2020
    Posts:
    12
    Just to close the loop, I got it working, and I was obviously overthinking everything. I copied the code from the section titled "Rewarded video example" in the Integration Guide, even though I'm not providing any In App Purchases or game rewards for watching, it simply allows the user to play the game.

    https://unityads.unity3d.com/help/unity/integration-guide-unity#rewarded-video-ads

    I grabbed the Placement IDs from the Unity Dashboard > Operate > Projects > Clicked on the desired Project > Dropped down Monetization. On the right, the words "video" and "rewardedVideo" are the actual placement IDs, and those are the two that come with Unity SDK (not to be confused if your game IDs on the left of this same page).

    I then setup a UI button that shows the ad, only if the adlistener states that everything is ready to go.

    Also, for some interesting reason, I know with the new code cannot unintentionally click buttons behind the ad while it is up.

    Here is the code I used:

    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Button.html#events

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5. using UnityEngine.Advertisements;
    6.  
    7. public class RewardedAdsScript : MonoBehaviour, IUnityAdsListener {
    8.  
    9.     string gameId = "3475968";
    10.     string myPlacementId = "LevelIntro";
    11.     bool testMode = true;
    12.     bool ready = false;
    13.  
    14.     // Initialize the Ads listener and service:
    15.     void Start () {
    16.         Advertisement.AddListener (this);
    17.         Advertisement.Initialize (gameId, testMode);
    18.     }
    19.  
    20.     // Implement IUnityAdsListener interface methods:
    21.     public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
    22.         // Define conditional logic for each ad completion status:
    23.         if (showResult == ShowResult.Finished) {
    24.             // Reward the user for watching the ad to completion.
    25.             Debug.LogWarning ("The ad was finished.");
    26.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    27.         } else if (showResult == ShowResult.Skipped) {
    28.             // Do not reward the user for skipping the ad.
    29.             Debug.LogWarning ("The ad was skipped.");
    30.             SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
    31.         } else if (showResult == ShowResult.Failed) {
    32.             Debug.LogWarning ("The ad did not finish due to an error.");
    33.         }
    34.     }
    35.  
    36.     public void OnUnityAdsReady (string placementId) {
    37.         // If the ready Placement is rewarded, show the ad:
    38.         if (placementId == myPlacementId)
    39.         {
    40.             ready = true;
    41.         }
    42.     }
    43.  
    44.     public void OnUnityAdsDidError (string message) {
    45.         // Log the error.
    46.     }
    47.  
    48.     public void OnUnityAdsDidStart (string placementId) {
    49.         // Optional actions to take when the end-users triggers an ad.
    50.     }
    51.  
    52.     public void OnClick()
    53.     {
    54.         if (ready)
    55.         {
    56.             Advertisement.Show (myPlacementId);
    57.         }
    58.     }
    59. }
    60.  
    I hope this helps! Thank you @sbankhead for your help!
    -Will