Skip to content

Android and Unity Banner API v2 documentation#38

Merged
Sunmeng1985 merged 12 commits into4.4.0from
4.4.0-unity
Nov 12, 2021
Merged

Android and Unity Banner API v2 documentation#38
Sunmeng1985 merged 12 commits into4.4.0from
4.4.0-unity

Conversation

@Sunmeng1985
Copy link
Contributor

@Sunmeng1985 Sunmeng1985 commented Nov 10, 2021

Part Banner(V2) Integration of integration-android.md file needs to be reviewed

Part Banner(V2) Integration of integration-unity.md file needs to be reviewed

Update Proguard
-keep class com.yodo1.mas.banner.** { *; }

Android Banner(V2) Integration

1. Add Yodo1MasBannerAdView to the layout

The first step toward displaying a banner is to place Yodo1MasBannerAdView in the layout for the Activity or Fragment in which you'd like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here's an example that shows an activity's Yodo1MasBannerAdView:

...
	<com.yodo1.mas.banner.Yodo1MasBannerAdView 
		xmlns:masads="http://schemas.android.com/apk/res-auto"
		android:id="@+id/yodo1_mas_banner"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center_horizontal|top"
		masads:adSize="Banner" />
...

Note the following required attributes:

  • masads:adSize - Set this to the ad size you'd like to use.

You can alternatively create Yodo1MasBannerAdView programmatically:

For Java

Yodo1MasBannerAdView bannerAdView = new Yodo1MasBannerAdView(this);
bannerAdView.setAdSize(Yodo1MasBannerAdSize.Banner);
// TODO: Add bannerAdView to your view hierarchy.

For Kotlin

val bannerAdView = Yodo1MasBannerAdView(this)
bannerAdView.setAdSize(Yodo1MasBannerAdSize.Banner)
// TODO: Add bannerAdView to your view hierarchy.

Banner sizes

Size in dp Description Availability AdSize constant
320x50 Banner Phones and Tablets Banner
320x100 Large Banner Phones and Tablets LargeBanner
300x250 IAB Medium Rectangle Phones and Tablets IABMediumRectangle
Full screen width x Adaptive height Adaptive banner Phones and Tablets AdaptiveBanner
Screen width x 32/50/90 Smart banner Phones and Tablets SmartBanner

2. Load an ad

Once the Yodo1MasBannerAdView is in place, the next step is to load an ad. That's done with the loadAd() method in the Yodo1MasBannerAdView class.

Here's an example that shows how to load an ad in the onCreate() method of an Activity:

For Java

package ...

import ...
import com.yodo1.mas.Yodo1Mas;
import com.yodo1.mas.banner.Yodo1MasBannerAdView;

public class MainActivity extends AppCompatActivity {
    private Yodo1MasBannerAdView bannerAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Yodo1Mas.getInstance().init(this, "YourAppKey", new Yodo1Mas.InitListener() {
            @Override
            public void onMasInitSuccessful() {
            }

            @Override
            public void onMasInitFailed(@NonNull Yodo1MasError error) {
            }
        });

        bannerAdView = findViewById(R.id.yodo1_mas_banner);
        bannerAdView.loadAd();
    }
}

For Kotlin

package ...

import ...
import com.yodo1.mas.Yodo1Mas;
import com.yodo1.mas.banner.Yodo1MasBannerAdView;

class MainActivity : AppCompatActivity() {

    lateinit var bannerAdView : Yodo1MasBannerAdView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Yodo1Mas.getInstance().init(this, "YourAppKey", object : Yodo1Mas.InitListener {
        	override fun onMasInitSuccessful() {    
        		Toast.makeText(this@MainActivity, "[Yodo1 Mas] Successful initialization", Toast.LENGTH_SHORT).show()
        	} 
        	override fun onMasInitFailed(error: Yodo1MasError) {
        		Toast.makeText(this@MainActivity, error.message, Toast.LENGTH_SHORT).show()  
        	}
        })

        bannerAdView = findViewById(R.id.yodo1_mas_banner)
        bannerAdView.loadAd()
    }
}

That's it! Your app is now ready to display banner ads.

3. Ad events

To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events through the Yodo1MasBannerAdListener class.

For Java

package ...

import ...
import com.yodo1.mas.Yodo1Mas;
import com.yodo1.mas.banner.Yodo1MasBannerAdListener;
import com.yodo1.mas.banner.Yodo1MasBannerAdView;

public class MainActivity extends AppCompatActivity {
    private Yodo1MasBannerAdView bannerAdView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Yodo1Mas.getInstance().init(this, "YourAppKey", new Yodo1Mas.InitListener() {
            @Override
            public void onMasInitSuccessful() {
            }

            @Override
            public void onMasInitFailed(@NonNull Yodo1MasError error) {
            }
        });

        bannerAdView = findViewById(R.id.yodo1_mas_banner);
        bannerAdView.setAdListener(new Yodo1MasBannerAdListener() {
		    @Override
		    public void onBannerAdLoaded(Yodo1MasBannerAdView bannerAdView) {
		        // Code to be executed when an ad finishes loading.
		    }
		
		    @Override
		    public void onBannerAdFailedToLoad(Yodo1MasBannerAdView bannerAdView, @NonNull Yodo1MasError error) {
		        // Code to be executed when an ad request fails.
		    }
		
		    @Override
		    public void onBannerAdOpened(Yodo1MasBannerAdView bannerAdView) {
		        // Code to be executed when an ad opens an overlay that
		        // covers the screen.
		    }
		
		    @Override
		    public void onBannerAdFailedToOpen(Yodo1MasBannerAdView bannerAdView, @NonNull Yodo1MasError error) {
				// Code to be executed when an ad open fails.
		    }
		
		    @Override
		    public void onBannerAdClosed(Yodo1MasBannerAdView bannerAdView) {
		        // Code to be executed when the user is about to return
		        // to the app after tapping on an ad.
		    }
		 });
        bannerAdView.loadAd();
    }
}

For Kotlin

package ...

import ...
import com.yodo1.mas.Yodo1Mas;
import com.yodo1.mas.banner.Yodo1MasBannerAdListener;
import com.yodo1.mas.banner.Yodo1MasBannerAdView;

class MainActivity : AppCompatActivity() {

    lateinit var bannerAdView : Yodo1MasBannerAdView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Yodo1Mas.getInstance().init(this, "YourAppKey", object : Yodo1Mas.InitListener {
        	override fun onMasInitSuccessful() {    
        		Toast.makeText(this@MainActivity, "[Yodo1 Mas] Successful initialization", Toast.LENGTH_SHORT).show()
        	} 
        	override fun onMasInitFailed(error: Yodo1MasError) {
        		Toast.makeText(this@MainActivity, error.message, Toast.LENGTH_SHORT).show()  
        	}
        })

        bannerAdView = findViewById(R.id.yodo1_mas_banner)
        bannerAdView.setAdListener(object : Yodo1MasBannerAdListener {
            override fun onBannerAdLoaded(bannerAdView: Yodo1MasBannerAdView?) {
                // Code to be executed when an ad finishes loading.
            }

            override fun onBannerAdFailedToLoad(
                bannerAdView: Yodo1MasBannerAdView?,
                error: Yodo1MasError
            ) {
                // Code to be executed when an ad request fails.
            }

            override fun onBannerAdOpened(bannerAdView: Yodo1MasBannerAdView?) {
                // Code to be executed when an ad opens an overlay that
		         // covers the screen.
            }

            override fun onBannerAdFailedToOpen(
                bannerAdView: Yodo1MasBannerAdView?,
                error: Yodo1MasError
            ) {
                // Code to be executed when an ad open fails.
            }

            override fun onBannerAdClosed(bannerAdView: Yodo1MasBannerAdView?) {
                // Code to be executed when the user is about to return
		         // to the app after tapping on an ad.

            }

        })
        bannerAdView.loadAd()
    }
}

Unity Banner(V2) Integration

1. Create a Yodo1U3dBannerAdView

The first step toward displaying a banner is to create a Yodo1U3dBannerAdView object in a C# script attached to a GameObject.

using System;
using UnityEngine;
using Yodo1.MAS;
...
public class BannerSampleV2 : MonoBehaviour
{
    private Yodo1U3dBannerAdView bannerAdView;
    ...
    public void Start()
    {
        // Initialize the MAS SDK.
        Yodo1U3dMas.SetInitializeDelegate((bool success, Yodo1U3dAdError error) => { });
        Yodo1U3dMas.InitializeSdk();
		
        this.RequestBanner();
    }

    private void RequestBanner()
    {
        // Create a 320x50 banner at top of the screen
        bannerAdView = new Yodo1U3dBannerAdView(Yodo1U3dBannerAdSize.Banner, Yodo1U3dBannerAdPosition.BannerTop | Yodo1U3dBannerAdPosition.BannerHorizontalCenter);
    }
}

The constructor for a Yodo1U3dBannerAdView has the following parameters:

  • Yodo1U3dBannerAdSize - The MAS ad size you'd like to use.
  • Yodo1U3dBannerAdPosition - The position where the banner ad should be placed. The Yodo1U3dBannerAdPosition enum lists the valid ad position values.

2. (Optional) Custom ad position

For greater control over where a Yodo1U3dBannerAdView is placed on screen than what's offered by Yodo1U3dBannerAdPosition values, use the Yodo1U3dBannerAdView constructor that has x- and y-coordinates as parameters:

// Create a 320x50 banner ad at coordinate (0,50) on screen.
bannerAdView = new Yodo1U3dBannerAdView(Yodo1U3dBannerAdSize.Banner, 0, 50);

The top-left corner of the Yodo1U3dBannerAdView will be positioned at the x and y values passed to the constructor, where the origin is the top-left of the screen.

3. Load an ad

Once the BannerView is instantiated, the next step is to load an ad. That's done with the loadAd() method in the BannerView class.

Here's an example that shows how to load an ad:

...
    private void RequestBanner()
    {
        // Create a 320x50 banner at top of the screen
        bannerAdView = new Yodo1U3dBannerAdView(Yodo1U3dBannerAdSize.Banner, Yodo1U3dBannerAdPosition.BannerTop | Yodo1U3dBannerAdPosition.BannerHorizontalCenter);

        // Load banner ads, the banner ad will be displayed automatically after loaded
        bannerAdView.LoadAd();
    }
...

That's it! Your app is now ready to display banner ads from MAS.

4. Ad events

To further customize the behavior of your ad, you can hook into a number of events in the ad's lifecycle: loading, opening, closing, and so on.

...
using System;
using UnityEngine;
using Yodo1.MAS;
...
public class BannerSampleV2 : MonoBehaviour
{
    private Yodo1U3dBannerAdView bannerAdView;

    public void Start()
    {
        // Initialize the MAS SDK.
        Yodo1U3dMas.SetInitializeDelegate((bool success, Yodo1U3dAdError error) => { });
        Yodo1U3dMas.InitializeSdk();
		
        this.RequestBanner();
    }

    private void RequestBanner()
    {
		  // Clean up banner before reusing
        if (bannerAdView != null)
        {
            bannerAdView.Destroy();
        }

    	 // Create a 320x50 banner at top of the screen
        bannerAdView = new Yodo1U3dBannerAdView(Yodo1U3dBannerAdSize.Banner, Yodo1U3dBannerAdPosition.BannerTop | Yodo1U3dBannerAdPosition.BannerHorizontalCenter);

		 // Ad Events
        bannerAdView.OnAdLoadedEvent += OnBannerAdLoadedEvent;
        bannerAdView.OnAdFailedToLoadEvent += OnBannerAdFailedToLoadEvent;
        bannerAdView.OnAdOpenedEvent += OnBannerAdOpenedEvent;
        bannerAdView.OnAdFailedToOpenEvent += OnBannerAdFailedToOpenEvent;
        bannerAdView.OnAdClosedEvent += OnBannerAdClosedEvent;

        // Load banner ads, the banner ad will be displayed automatically after loaded
        bannerAdView.LoadAd();
    }

    private void OnBannerAdLoadedEvent(Yodo1U3dBannerAdView adView)
    {
        // Banner ad is ready to be shown.
        Debug.Log("[Yodo1 Mas] OnBannerAdLoadedEvent event received");
    }

    private void OnBannerAdFailedToLoadEvent(Yodo1U3dBannerAdView adView, Yodo1U3dAdError adError)
    {
        Debug.Log("[Yodo1 Mas] OnBannerAdFailedToLoadEvent event received with error: " + adError.ToString());
    }

    private void OnBannerAdOpenedEvent(Yodo1U3dBannerAdView adView)
    {
        Debug.Log("[Yodo1 Mas] OnBannerAdOpenedEvent event received");
    }

    private void OnBannerAdFailedToOpenEvent(Yodo1U3dBannerAdView adView, Yodo1U3dAdError adError)
    {
        Debug.Log("[Yodo1 Mas] OnBannerAdFailedToOpenEvent event received with error: " + adError.ToString());
    }

    private void OnBannerAdClosedEvent(Yodo1U3dBannerAdView adView)
    {
        Debug.Log("[Yodo1 Mas] OnBannerAdClosedEvent event received");
    }
}

5. Clean up banner ads

When you are finished with a BannerView, make sure to call the Destroy() method before dropping your reference to it:

bannerAdView.Destroy();

6. Create a Banner Placement

Simply add the placement name as a string in the parentheses.

bannerAdView.SetAdPlacement("Placement_Name")

@Sunmeng1985 Sunmeng1985 changed the title Unity Banner API v2 documentation Android and Unity Banner API v2 documentation Nov 11, 2021
@Sunmeng1985 Sunmeng1985 merged commit c1714ee into 4.4.0 Nov 12, 2021
@Sunmeng1985 Sunmeng1985 deleted the 4.4.0-unity branch November 15, 2021 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants