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
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;
}
}
}
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
Follow this Question
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