B4A Library Unity Ads Library

Original library: https://github.com/Unity-Technologies/unity-ads-android
Based on Unity Ads Library v4.4.1 (05 Oct 2022).
Banner, Interstitial and Rewarded Video.

v4.0.x - A lot of events, methods and classes are depecated or changed, full list here. If you use this library for the first time in your project, there is no problem for you. However, if you are upgrading the library from any of the previous versions, be warned that you will need to change your code. Example app is also updated for v4.0.

UnityAds

Author:
Author: Unity - B4a Wrapper: Pendrush
Version: 4.41
  • UnityAdsBanner
    • Events:
      • OnBannerClick
      • OnBannerFailedToLoad (Error As String)
      • OnBannerLeftApplication
      • OnBannerLoaded
    • Functions:
      • BringToFront
      • Initialize (arg1 As String)
      • InitializeBanner (EventName As String, GameID As String, TestMode As Boolean, PlacementID As String)
        EventName - Event name
        GameID - The Unity Game ID for your Project, located in the developer dashboard.
        TestMode - When set to true, only test ads display.
        PlacementID - The Placement ID, located on the developer dashboard.
        UnityAdsBanner.InitializeBanner("BannerAd", "3054608", True, "banner")
        Activity.AddView(UnityAdsBanner, 100%x/2-160dip, 0, 320dip, 50dip)
        UnityAdsBanner.LoadBanner
      • Invalidate
      • Invalidate2 (arg0 As android.graphics.Rect)
      • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • IsInitialized As Boolean
      • LoadBanner
        Load banner Ad.
      • RemoveView
      • RequestFocus As Boolean
      • SendToBack
      • SetBackgroundImage (arg0 As android.graphics.Bitmap) As BitmapDrawable
      • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
      • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
      • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
      • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • Properties:
      • Background As android.graphics.drawable.Drawable
      • Color As Int [write only]
      • Enabled As Boolean
      • Height As Int
      • Left As Int
      • Padding As Int()
      • Parent As Object [read only]
      • Tag As Object
      • Top As Int
      • Visible As Boolean
      • Width As Int
  • UnityAdsInterstitial
    • Events:
      • OnUnityAdsAdLoaded (PlacementID As String)
      • OnUnityAdsFailedToLoad (Error As String)
      • OnUnityAdsShowClick (PlacementID As String)
      • OnUnityAdsShowComplete (PlacementID As String, State As String)
      • OnUnityAdsShowFailure (Error As String)
      • OnUnityAdsShowStart (PlacementID As String)
    • Functions:
      • InitializeInterstitial (EventName As String, GameID As String, TestMode As Boolean, PlacementID As String)
        EventName - Event name
        GameID - The Unity Game ID for your Project, located in the developer dashboard.
        TestMode - When set to true, only test ads display.
        PlacementID - The Placement ID, located on the developer dashboard.
        UnityAdsInterstitial.InitializeInterstitial("InterstitialAds", "3054608", True, "rewardedVideo")
      • IsInitialized As Boolean
      • LoadAd
        Load Interstitial/Rewarded Ad.
      • ShowAd
        Show Ad


For InterstitialAd and for RewardedVideoAd you need to add this in your manifest:
B4X:
AddApplicationText(<activity
            android:name="com.unity3d.services.ads.adunit.AdUnitActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="true" />

        <activity
            android:name="com.unity3d.services.ads.adunit.AdUnitTransparentActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="true" />

        <activity
            android:name="com.unity3d.services.ads.adunit.AdUnitTransparentSoftwareActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="false" />

        <activity
            android:name="com.unity3d.services.ads.adunit.AdUnitSoftwareActivity"
            android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:hardwareAccelerated="false" />)

Download library from:
 

Attachments

  • UnityAdsExample.zip
    11.8 KB · Views: 202
Last edited:

Schakalaka

Active Member
Licensed User
hello, thank for your reply-.

This is my configuration,

1686135481386.png


1686135582915.png


1686135737502.png



the app start,
but also return this error and crash.
this happend when use the last unity sdk aar file, so i have think that this can cause the problem



B4X:
Logger connesso a: RZCT917L7TV
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
** Activity (main) Resume **
*** Service (firebasemessaging) Create ***
** Service (firebasemessaging) Start **
*** Receiver (httputils2service) Receive (first time) ***
1
** Activity (main) Pause, UserClosed = false **
** Activity (act_home) Create (first time) **
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/CoroutineExceptionHandler;
    at com.unity3d.services.core.di.ServiceProvider$initialize$1.invoke(ServiceProvider.kt:149)
    at com.unity3d.services.core.di.ServiceProvider$initialize$1.invoke(ServiceProvider.kt:40)
    at com.unity3d.services.core.di.ServicesRegistryKt.registry(ServicesRegistry.kt:107)
    at com.unity3d.services.core.di.ServiceProvider.initialize(ServiceProvider.kt:40)
    at com.unity3d.services.core.di.ServiceProvider.<clinit>(ServiceProvider.kt:35)
    at com.unity3d.services.core.misc.Utilities.getService(Utilities.java:212)
    at com.unity3d.services.core.misc.Utilities.getService(Utilities.java:216)
    at com.unity3d.services.ads.UnityAdsImplementation.<init>(UnityAdsImplementation.java:39)
    at com.unity3d.services.ads.UnityAdsImplementation.getInstance(UnityAdsImplementation.java:45)
    at com.unity3d.ads.UnityAds.isInitialized(UnityAds.java:169)
    at com.pendrush.unityads.wrapper.UnityAdsWrap$UnityAdsInterstitialWrap.InitializeInterstitial2(UnityAdsWrap.java:137)
    at com.pendrush.unityads.wrapper.UnityAdsWrap$UnityAdsInterstitialWrap.InitializeInterstitial(UnityAdsWrap.java:132)
    at com.clickandclaimbitcoin.android.act_home$ResumableSub_Activity_Create.resume(act_home.java:604)
    at com.clickandclaimbitcoin.android.act_home._activity_create(act_home.java:504)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at com.clickandclaimbitcoin.android.act_home.afterFirstLayout(act_home.java:105)
    at com.clickandclaimbitcoin.android.act_home.access$000(act_home.java:17)
    at com.clickandclaimbitcoin.android.act_home$WaitForLayout.run(act_home.java:83)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:226)
    at android.os.Looper.loop(Looper.java:313)
    at android.app.ActivityThread.main(ActivityThread.java:8757)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.ClassNotFoundException: kotlinx.coroutines.CoroutineExceptionHandler
    ... 31 more
** Activity (act_home) Resume **
** Activity (act_home) Pause, UserClosed = true **

thank you
 

Pendrush

Well-Known Member
Licensed User
Longtime User
From version 4.5.0 UnityAds use Kotlin and Kotlin Corutines... Kotlin coroutines containing more then 10 dependecies...
Like: Kotlinx Coroutines Core, Kotlinx Coroutines Android, Kotlinx Coroutines JDK 8, Kotlinx Coroutines Reactive, Kotlinx Coroutines Reactor...
It's a nightmare to get all to work properly in B4A. Also UnityAds depend on OkHTTP and Okio libraries, and these also use Kotlin... I will try to make it to work, but without any promises.
 
Top