Skip to content

Commit

Permalink
implement rewarded ads
Browse files Browse the repository at this point in the history
  • Loading branch information
Coding-Meet committed Nov 20, 2023
1 parent 35a910d commit f502c1c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 7 deletions.
34 changes: 28 additions & 6 deletions app/src/main/java/com/coding/meet/unityadsapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.unity3d.services.banners.UnityBannerSize

class MainActivity : AppCompatActivity() {
Expand All @@ -17,31 +18,31 @@ class MainActivity : AppCompatActivity() {
// showBannerAds(
// this,
// findViewById(R.id.adBannerCon),
// "bannerads",
// "Banner_Android",
// UnityBannerSize(320,50)
// )

// iabStandard Banner Size
// showBannerAds(
// this,
// findViewById(R.id.adBannerCon),
// "bannerads",
// "Banner_Android",
// UnityBannerSize.iabStandard
// )

//leaderboard Banner Size
// showBannerAds(
// this,
// findViewById(R.id.adBannerCon),
// "bannerads",
// "Banner_Android",
// UnityBannerSize.leaderboard
// )

//standard Banner Size
showBannerAds(
this,
findViewById(R.id.adBannerCon),
"bannerads",
"Banner_Android",
UnityBannerSize.standard
)

Expand All @@ -50,15 +51,36 @@ class MainActivity : AppCompatActivity() {

val myInterstitialAds = MyInterstitialAds(this)
// placementId change in Monetization console
myInterstitialAds.loadInterstitialAds("video")
myInterstitialAds.loadInterstitialAds("Interstitial_Android")

showInterstitialAdsBtn.setOnClickListener {
// placementId change in Monetization console
myInterstitialAds.showInterstitialAds("video") {
myInterstitialAds.showInterstitialAds("Interstitial_Android") {
val afterIntent = Intent(this,AfterInterstitialActivity::class.java)
startActivity(afterIntent)
}
}

val sharedPreferenceManger = SharedPreferenceManger(this)
getRewardedCoin(sharedPreferenceManger.totalRewardedCoin)
val showRewardedAdsBtn = findViewById<Button>(R.id.showRewardedAdsBtn)
val myRewardedAds = MyRewardedAds(this)
// placementId change in Monetization console
myRewardedAds.loadRewardedAds("Rewarded_Android")

showRewardedAdsBtn.setOnClickListener {
// placementId change in Monetization console
myRewardedAds.showRewardedAds("Rewarded_Android"){
val rewardedCoin = 10 // Here Your Rewarded Coin Add
sharedPreferenceManger.totalRewardedCoin += rewardedCoin
getRewardedCoin(sharedPreferenceManger.totalRewardedCoin)
}
}

}

private fun getRewardedCoin(totalRewardedCoin: Int) {
val rewardedCoinTxt = findViewById<TextView>(R.id.rewardedCoinTxt)
rewardedCoinTxt.text = "Total Rewarded Coins: $totalRewardedCoin Coins"
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/coding/meet/unityadsapp/MyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MyApp : Application() {

// gameId change in Monetization console
// testMode if app release change to false
UnityAds.initialize(this,"14851",true,
UnityAds.initialize(this,"5204341",true,
object : IUnityAdsInitializationListener{
override fun onInitializationComplete() {}

Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/com/coding/meet/unityadsapp/MyRewardedAds.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.coding.meet.unityadsapp

import android.app.Activity
import com.unity3d.ads.*

class MyRewardedAds(private val activity: Activity) {

fun loadRewardedAds(placementId : String){
UnityAds.load(placementId, object : IUnityAdsLoadListener{
override fun onUnityAdsAdLoaded(placementId: String?) {
}

override fun onUnityAdsFailedToLoad(
placementId: String?,
error: UnityAds.UnityAdsLoadError?,
message: String?,
) {
}
})
}
fun showRewardedAds(placementId : String,afterCodeRewardCoin: () ->Unit ){
UnityAds.show(activity,placementId, UnityAdsShowOptions(),
object : IUnityAdsShowListener{
override fun onUnityAdsShowFailure(
placementIds: String?,
error: UnityAds.UnityAdsShowError?,
message: String?,
) {
activity.longToastShow("error = $message")
loadRewardedAds(placementId)
}

override fun onUnityAdsShowStart(placementId: String?) {
activity.longToastShow("onUnityAdsShowStart")

}

override fun onUnityAdsShowClick(placementId: String?) {
activity.longToastShow("onUnityAdsShowClick")

}

override fun onUnityAdsShowComplete(
placementIds: String?,
state: UnityAds.UnityAdsShowCompletionState?,
) {
if (state == UnityAds.UnityAdsShowCompletionState.COMPLETED){
// Reward the user for watching the ad to completion
afterCodeRewardCoin()
}else{
//Do not reward the user for skipping the ad
activity.longToastShow("Do not reward the user for skipping the ad")

}
loadRewardedAds(placementId)
}

})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.coding.meet.unityadsapp

import android.content.Context
import androidx.appcompat.app.AppCompatActivity

class SharedPreferenceManger(context: Context) {
private val preference = context.getSharedPreferences(
context.packageName,
AppCompatActivity.MODE_PRIVATE
)
private val editor = preference.edit()


private val keyTotalRewardedCoin = "totalRewardedCoin"

var totalRewardedCoin
get() = preference.getInt(keyTotalRewardedCoin, 0)
set(value) {
editor.putInt(keyTotalRewardedCoin, value)
editor.commit()
}

}
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
android:layout_centerInParent="true"
android:text="Show Interstitial Ads"/>

<Button
android:id="@+id/showRewardedAdsBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/showInterstitialAdsBtn"
android:text="Show Rewarded Ads"/>

<TextView
android:id="@+id/rewardedCoinTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rewarded Amount"
android:layout_centerHorizontal="true"
android:layout_below="@id/showRewardedAdsBtn"
android:textSize="22sp"
android:textStyle="bold"/>

<RelativeLayout
android:id="@+id/adBannerCon"
android:layout_width="wrap_content"
Expand Down

0 comments on commit f502c1c

Please sign in to comment.