1

I am facing an issue while integrating the Unity ads. My requirement is that after few free game plays user should go through an ad on next game launch and then he can play the game. But when i check if the placement is ready so that i can show the ad in IUnityAdsListener.onUnityAdsReady(). The UnityAds.isReady(placementId: String) always returns false.

I am trying to initialize the sdk with

UnityAds.initialize(this, "<GameId>", this@MainActivity, false)


My IUnityAdsListener is

override fun onUnityAdsStart(p0: String?) {
    println("Unity ad start")
}

override fun onUnityAdsFinish(p0: String?, p1: UnityAds.FinishState?) {
    println("Unity ad finished")
    loadGame()
}

override fun onUnityAdsError(error: UnityAds.UnityAdsError?, message: String?) {
    println("Unity ad error, $message, ${error?.name}")
}

override fun onUnityAdsReady(placementId: String?) {
    if(UnityAds.isReady(placementId)){
        UnityAds.show(this@MainActivity, placementId)
    }

    println("Unity ad ready, Placement Id: $placementId")
}
Saini Arun
  • 391
  • 3
  • 7

1 Answers1

1

I have found the solution or rather a work around.
I think the issue is that UnityAds requires some time for initialization. So when I created a thread to delay the UnityAds.isReady(String placementId) call for a few seconds, it returns true and the ad is actually ready to be shown and UnityAds.show(Activity activity, String placementId) actually works fine without any issues. As my requirement was to load and show ads on the game launch as early as possible, it seems like Unity sdk had an issue initializing and showing ads that soon.

Below is the implementation change for IUnityAdsListener.onUnityAdsReady(String placementId) callback method that works.

override fun onUnityAdsReady(placementId: String?) {

    if(placementId == "video") {
        val runnable = Runnable {
            Thread.sleep(2000)
            if (UnityAds.isReady("video"))
                UnityAds.show(this@MainActivity, placementId)
        }
        Thread(runnable).start()
    }

    println("Unity ad ready, Placement Id: $placementId")
}
Culainn
  • 19
  • 3
Saini Arun
  • 391
  • 3
  • 7