• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by rahat090255 · Apr 08, 2020 at 07:35 AM · unity ads

Problem Implementing unity adslisteners

I am facing a problem integrating the listeners for unity ads. i have gone through the tutorial of [this][1]. here is my codes

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Advertisements;
 public interface IUnityAdsListener
 {
     void OnUnityAdsReady(string placementId);
     void OnUnityAdsDidError(string message);
     void OnUnityAdsDidStart(string placementId);
     void OnUnityAdsDidFinish(string placementId, ShowResult showResult);
 
 }

and my unityAds code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using UnityEngine.UI;
 using UnityEngine.Advertisements;
 public class UnityAds : MonoBehaviour, IUnityAdsListener
 {
     AddManager am;
     string gameId = "1234567";
     string myPlacementId = "rewardedVideo";
     bool testMode = true;
 
     // Initialize the Ads listener and service:
     void Start()
     {
         Advertisement.AddListener(this);
         Advertisement.Initialize(gameId, testMode);
     }
 
     // Implement IUnityAdsListener interface methods:
     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
     {
         // Define conditional logic for each ad completion status:
         if (showResult == ShowResult.Finished)
         {
             // Reward the user for watching the ad to completion.
         }
         else if (showResult == ShowResult.Skipped)
         {
             // Do not reward the user for skipping the ad.
         }
         else if (showResult == ShowResult.Failed)
         {
             Debug.LogWarning("The ad did not finish due to an error.");
         }
     }
 
     public void OnUnityAdsReady(string placementId)
     {
         // If the ready Placement is rewarded, show the ad:
         if (placementId == myPlacementId)
         {
             Advertisement.Show(myPlacementId);
         }
     }
 
     public void OnUnityAdsDidError(string message)
     {
         // Log the error.
     }
 
     public void OnUnityAdsDidStart(string placementId)
     {
         // Optional actions to take when the end-users triggers an ad.
     }
 }
 

i am getting error on

 Advertisement.AddListener(this);

please help me solving this error. My ads were working perfectly without the listeners. but without listeners i cant track when users are rewarded or not. my sdk is 3.4.2 [1]: https://unityads.unity3d.com/help/unity/integration-guide-unity?_ga=2.102650945.1596887153.1585837089-1692565306.1572278406

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tokusunei · Aug 26, 2020 at 06:55 AM

I don't know if this will work, but consider making a private static object of your class, and then assign it to the listener. Here's my code right here. It definitely does work.

Also, you want to be sure that your advertisement is ready to be shown, so use something like a while loop until the advertisement is ready. That's mostly the reason why it won't show I believe using System.Collections; using UnityEngine; using UnityEngine.Advertisements;

 public class GameOverScreen : MonoBehaviour, IUnityAdsListener
 {
     private static IUnityAdsListener Instance;
 
     private void Awake()
     {
         Instance = this;  
     }
 
     private void Start()
     {
         Advertisement.Initialize("3789069");
     }
     public void OnTryAgain()
     {
 
     }
 
     /// <summary>
     /// Player gets extra live, but they have to watch an ad
     /// </summary>
     public void OnContinue()
     {
         ShowAd();
     }
 
     void ShowAd(string zone = "rewardedVideoZone")
     {
         Advertisement.AddListener(Instance);
 
         while (!Advertisement.IsReady())
             continue;
 
         OnUnityAdsReady(zone);
     }
 
 
     public void OnUnityAdsReady(string placementId)
     {
         Advertisement.Show(placementId);
     }
 
     public void OnUnityAdsDidError(string message)
     {
         Debug.Log(message);
     }
 
     public void OnUnityAdsDidStart(string placementId)
     {
         Debug.Log("Ad has started");
     }
 
     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
     {
         switch (showResult)
         {
             case ShowResult.Failed:
                 OnUnityAdsDidError("Ad came across an error.");
                 break;
             case ShowResult.Skipped:
                 Debug.Log("Ad was skipped.");
                 //Remove Listener
                 break;
             case ShowResult.Finished:
                 Debug.Log("Yay! Ad is now finished!!!");
                 Advertisement.RemoveListener(Instance);
                 break;
             default:
                 break;
         }
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tokusunei · Aug 26, 2020 at 06:58 AM 0
Share

You should note that this is my first time working with this, and so this code isn't all that finished yet. But as for the adding listeners, my ads shows up the way it should. You could also set the ip to focus on your android, and debug log your things co$$anonymous$$g from the phone. That way you know what's working and what's not working.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

199 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scripting errors in Unity Ads Import 1 Answer

How many invoices per month using Unity Ads 0 Answers

Unity test ads not Working 0 Answers

Android game export, duplicate files not found 0 Answers

Unity Ads Callback Problem,Unity ads callback problem 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges