I have a script running UnityAds. I have verified that placement IDs are correct. It worked the first times, then i applied this script to a prefab to work across many levels and now it no longer works. There is a button that calls ShowRewardedVideo(false). It works if I run the function ShowRewardedVideo(true) first, but not if ShowRewardedVideo(false) is called first, I do not understand why. I first tried with else instead of if isSolutionReset == false, but then the script did not work. Now, it no longer works again. The odd thing is that when ShowRewardedVideo(false) is called, the log doesnt get the Ads listener is working message. Anyone has any tips?
string myPlacementId = "rewardedVideo";
public Text text;
public GameObject solution;
public GameObject AdMenu;
public ResetBlocks resetBlocks;
bool isSolutionNotReset;
private void Start()
{
Advertisement.AddListener(this);
}
// Implement a function for showing a rewarded video ad:
public void ShowRewardedVideo(bool isSolution)
{
Advertisement.Show(myPlacementId);
isSolutionNotReset = isSolution;
Debug.Log("Try play ad");
}
// Implement IUnityAdsListener interface methods:
void IUnityAdsListener.OnUnityAdsReady(string placementId)
{
}
void IUnityAdsListener.OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
Debug.Log("Ads listener is working");
if(placementId != "rewardedVideo")
{
Debug.Log("Ad wasnt rewarded");
return;
}
if (isSolutionNotReset == true)
{
SolutionAd(showResult);
Debug.Log("Played Solution ad");
}
if(isSolutionNotReset == false)
{
ResetBlock(showResult);
Debug.Log("Played reset ad");
}
}
void IUnityAdsListener.OnUnityAdsDidError(string message)
{
// Log the error.
}
void IUnityAdsListener.OnUnityAdsDidStart(string placementId)
{
// Optional actions to take when the end-users triggers an ad.
}
void ResetBlock(ShowResult showRes)
{
if (showRes == ShowResult.Finished)
{
Debug.Log("Ad finished");
resetBlocks.SetTransform();
resetBlocks.SetLevelReset(true);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
else
{
Debug.Log("Ad did not finish");
}
}
void SolutionAd(ShowResult showRes)
{
// Define conditional logic for each ad completion status:
if (showRes == ShowResult.Finished)
{
if (AdMenu == null)
{
text.text = "Error 102: Menu not accessible. Restart game";
return;
}
AdMenu.SetActive(false);
solution.SetActive(true);
}
else if (showRes == ShowResult.Skipped)
{
// Do not reward the user for skipping the ad.
text.text = "The ad was skipped";
}
else if (showRes == ShowResult.Failed)
{
Debug.LogWarning("The ad did not finish due to an error.");
text.text = "The ad did not finish due to an error";
}
}
↧