Search Unity

Does not implement interface member 'IUNityAdsListener.---'

Discussion in 'Unity Ads' started by i9fasc7, Aug 12, 2019.

  1. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    Hello everyone,

    I want to use rewarded ads in my game and am following the advertisement getting started guide from Unity.
    My first problem was that the game did not know the IUnityAdsListener and I was able to solve this problem by upgrading the Unity Ads Package to 3.2.0. But now I get the following error messages if I write IUnityAdsListener behind the class name:

    public class WideCrossPuzzleColourNumber : MonoBehaviour, IUnityAdsListener

    Errors:
    - 'WideCrossPuzzleColourNumber' does not implement interface member 'IUnityAdsListener.OnUnityAdsReady(string)'
    - 'WideCrossPuzzleColourNumber' does not implement interface member 'IUnityAdsListener.OnUnityAdsDidError(string)'
    - 'WideCrossPuzzleColourNumber' does not implement interface member 'IUnityAdsListener.OnUnityAdsDidStart(string)'
    - 'WideCrossPuzzleColourNumber' does not implement interface member 'IUnityAdsListener.OnUnityAdsDidFinish(string, ShowResult)'

    Anyone with the same problem or a solution?


    Thanks in advance.
     
  2. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    You're inheriting from the interface which defines these interface members. In visual studio you can click the warning and automatically make method stubs of these missing interfaces. Just add these functions to the script that implements the IUnityAdsListener and the errors are gone.
     
  3. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    Thank you for the answer.

    I now implemeted the interface with pressing alt+enter in the error and the following functions were included:

    Code (CSharp):
    1. public void OnUnityAdsReady(string placementId)
    2.     {
    3.         throw new System.NotImplementedException();
    4.     }
    5.  
    6.     public void OnUnityAdsDidError(string message)
    7.     {
    8.         throw new System.NotImplementedException();
    9.     }
    10.  
    11.     public void OnUnityAdsDidStart(string placementId)
    12.     {
    13.         throw new System.NotImplementedException();
    14.     }
    15.  
    16.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    17.     {
    18.         throw new System.NotImplementedException();
    19.     }
    What throws me off is the Exception() method. Are the public void methods working if I delete the "code" inside the method and ad my own?

    I think I have to use the ONUnityAdsDidFinish to reward the player. Because this method only works if internet is on and the ad/game was not interrupted. Am I on the right track?
     
  4. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    My code so far as I need it is as following:
    Code (CSharp):
    1. public void OnUnityAdsReady(string placementId)
    2.     {
    3.         if(placementId == myplacementId)
    4.         {
    5.             Advertisement.Show(myplacementId);
    6.         }
    7.     }
    Code (CSharp):
    1.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    2.     {
    3.         if(showResult == ShowResult.Finished)
    4.         {
    5.             PlayerPrefs.SetInt("keysamount", PlayerPrefs.GetInt("keysamount") + 5);
    6.         }
    7.     }
    8.  
    But I need to keep the ohter public void methods inside my code or the IUnityAdsListener throws errors again.

    I wanted to connect these methods onto a button, but I am only able to select all public methods except the OnUnityAdsdidFinish. But I need this one or else the player can just earn keys by disconnecting from the internet and spam the video key without actually watching it.

    I thought about putting the OnUnityAdsDidFinish into the OnUnityAdsReady method, but am unable to do so wihtout getting a bunch of error messages.

    Anyone with a solution?


    Thanks in advance.
     
  5. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    Hi,

    I recommend not using the OnUnityAdsReady implementation you're using - it shows an ad right when one becomes available, which means another ad will show shortly after seeing the first one.

    The OnUnityAdsDidFinish is the correct approach to rewarding the players for watching the ads, but what kind of errors are you getting?
     
  6. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    I don't know whether it qualifies as an error, but I am unable to connect the two methods (probably due to my poor coding skills.

    My idea is to have a button and when you press it a cetain method "DisplayAd()" is executed. So far I always rewarded the player with points afterwards, but he could simply get the points by disabling the his internet to prevent the advertisement video from showing.
    I thought about using this OnUnityAdsDidFinish to check whether the advertisement was even shown and if it was successfully shown.
    So far I got the DisplayAd() method working on the button press (I am using a button an put the method onto its OnClick()-list). But I cannot add the OnUnityAdsDidFinish() method, even though it is public. And by trying to copy the code into the DisplayAd() method I get notifications like these:
    Invalid expression; does not exist; ...

    Here is the pseudocode for I thought how it could work, but aparently it does not. At the moment I simply show the advertisement and then reward the player, but this is abusable by using the game in offline mode.
    Code (CSharp):
    1.     public void DisplayAd()
    2.     {
    3.         Advertisement.Show();
    4.         OnUnityAdsDidFinish(string placementId, ShowResult showresult)
    5.         {
    6.             PlayerPrefs.SetInt("keysamount", PlayerPrefs.GetInt("keysamount") + 5);
    7.         }
    8.     }
    9.  
     
  7. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    Simple: Check if the ShowResult was Finished.

    From Unity Ads knowledge base: https://unityads.unity3d.com/help/unity/integration-guide-unity

    Code (CSharp):
    1.  
    2. public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
    3.         // Define conditional logic for each ad completion status:
    4.         if (showResult == ShowResult.Finished) {
    5.             // Reward the user for watching the ad to completion.
    6.         } else if (showResult == ShowResult.Skipped) {
    7.             // Do not reward the user for skipping the ad.
    8.         } else if (showResult == ShowResult.Failed) {
    9.             Debug.LogWarning (“The ad did not finish due to an error.);
    10.         }
    11.     }
     
  8. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    I was following this guide before I posted my problems here.
    My code is as following:

    Code (CSharp):
    1. public class AdShower : MonoBehaviour, IUnityAdsListener
    2. {
    3.     string gameId = "3237240";
    4.     bool testMode = false;
    5.  
    6.  
    7.     void Start()
    8.     {
    9.         Advertisement.AddListener(this);
    10.         Advertisement.Initialize(gameId, testMode);
    11.     }
    12.  
    13.     public void DisplayAd()
    14.     {
    15.         Advertisement.Show();
    16.         //PlayerPrefs.SetInt("keysamount", PlayerPrefs.GetInt("keysamount") + 5);
    17.     }
    18.  
    19.     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    20.     {
    21.         // Define conditional logic for each ad completion status:
    22.         if (showResult == ShowResult.Finished)
    23.         {
    24.             PlayerPrefs.SetInt("keysamount", PlayerPrefs.GetInt("keysamount") + 5);
    25.         }
    26.     }
    27.  
    28.     public void OnUnityAdsReady(string placementId)
    29.     {
    30.         throw new System.NotImplementedException();
    31.     }
    32.  
    33.     public void OnUnityAdsDidError(string message)
    34.     {
    35.         throw new System.NotImplementedException();
    36.     }
    37.  
    38.     public void OnUnityAdsDidStart(string placementId)
    39.     {
    40.         throw new System.NotImplementedException();
    41.     }
    42. }
    43.  
    I have a public void DisplayAd() to display the ad (obviously) and then I have a public void OnUnityAdsFinish that should reward the player with keys if the ad was shown succesfully.
    But how do I combine these two methods?
    First I thought about putting the OnUnityAdsFinish method alsy in the OnClick() list of the button, but this method is not selectable as seen in the attached picture.
    And I am unable to include the OnUnityAdsFinish method into the DisplayAd() method. Maybe I am not familiar enough with the coding rules. But I get the error as seen in the second attached picture: Unbenannt.png Error_OnUnityAdsDidFinish().png
    I as well think that the Unity Guide about rewarded videos is how I should proceed, but sadly it does not work out for me/I do not understand the procedure on how to work with the guide's information.
     
  9. mikaisomaa

    mikaisomaa

    Unity Technologies

    Joined:
    Sep 14, 2015
    Posts:
    365
    OnUnityAdsFinish is a callback that gets called automatically in the IUnityAdsListener -interface when the ad is completed. No need to call this function yourself. Also you're missing necessary parameters in the function call (calling it without parameters when it asks for two), but never mind that. Remove OnUnityAdsFinish(); and it should work. :)
     
  10. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    Sadly not working either. Only new error messages relating to not implemented methods and buttons that throw errors.
     
  11. ceceomer

    ceceomer

    Joined:
    Sep 1, 2014
    Posts:
    19
    Can you paste your script here? Problem is about your coding skill.
     
  12. i9fasc7

    i9fasc7

    Joined:
    Jun 7, 2018
    Posts:
    47
    Code (CSharp):
    1.     public class AdShower : MonoBehaviour, IUnityAdsListener
    2.     {
    3.         string gameId = "3237240";
    4.         bool testMode = false;
    5.    
    6.    
    7.         void Start()
    8.         {
    9.             Advertisement.AddListener(this);
    10.             Advertisement.Initialize(gameId, testMode);
    11.         }
    12.    
    13.         public void DisplayAd()
    14.         {
    15.             Advertisement.Show();
    16.             //PlayerPrefs.SetInt("keysamount", PlayerPrefs.GetInt("keysamount") + 5);
    17.         }
    18.    
    19.         public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    20.         {
    21.             // Define conditional logic for each ad completion status:
    22.             if (showResult == ShowResult.Finished)
    23.             {
    24.                 PlayerPrefs.SetInt("keysamount", PlayerPrefs.GetInt("keysamount") + 5);
    25.             }
    26.         }
    27.    
    28.         public void OnUnityAdsReady(string placementId)
    29.         {
    30.             throw new System.NotImplementedException();
    31.         }
    32.    
    33.         public void OnUnityAdsDidError(string message)
    34.         {
    35.             throw new System.NotImplementedException();
    36.         }
    37.    
    38.         public void OnUnityAdsDidStart(string placementId)
    39.         {
    40.             throw new System.NotImplementedException();
    41.         }
    42.     }
    43.    
    44.  
    That is the code I wanted to work with. The DisplayAd() method should be called if a button is clicked. Without the IUnityAdsListener I am able to display an advertisement this way. But with the IUnityAdsListener it is not working anymore. Also "mikaisomaa" wrote that the OnUnityAdsDidFinish is automatically called, but it does not appear to be that easy.
     
  13. ceceomer

    ceceomer

    Joined:
    Sep 1, 2014
    Posts:
    19
    Delete them "throw new System.NotImplementedException();" in your code because they brokes your game.

    Code (CSharp):
    1. public void OnUnityAdsReady(string placementId)
    2.         {
    3.          
    4.         }
    5.  
    6.         public void OnUnityAdsDidError(string message)
    7.         {
    8.            
    9.         }
    10.  
    11.         public void OnUnityAdsDidStart(string placementId)
    12.         {
    13.            
    14.         }
    Do it like that and try.