The YaftaMobile Unity SDK enables developers to quickly integrate YaftaMobile ads into their Unity applications. The SDK supports android 2.3 or higher.
In the Unity Editor
For YaftaMobile to work, you need to make the following changes in the AndroidManifest.xml file.
INTERNET
, ACCESS_NETWORK_STATE
InterstitialActivity
Your AndroidManifest.xml file should have the following:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<meta-data android:name="com.yaftamobile.sdk.app.id"
android:value="<YOUR-APPID>" />
<activity android:configChanges="keyboard|keyboardHidden|orientation"
android:name="com.yaftamobile.sdk.ads.interstitial.InterstitialActivity" />
<!-- For Google Play Services (required by YaftaMobile) -->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
To display banner ads in your app use the showBanner(adSize:AdSize, bannerPosition:BannerPosition, refreshRate:int) method where:
FLEXIBLE
, S300X50
, S320X50
, S468X60
, S728X90
TOP_CENTER
, TOP_LEFT
, TOP_RIGHT
, BOTTOM_CENTER
, BOTTOM_LEFT
, BOTTOM_RIGHT
Before you can use YaftaMobile SDK in your code, you need to add the following namespace declaration:
using YaftaMobileUnity;
add the following code to show banner:
#if UNITY_ANDROID
YaftaMobile.showBanner(AdSize.FLEXIBLE,
BannerPosition.BOTTOM_CENTER , 60);
#endif
Use the following to hide/re-show the banner:
#if UNITY_ANDROID
YaftaMobile.pauseBanner(); // hide banner if visible
YaftaMobile.resumeBanner(); // show banner if not visible
#endif
To show an interstitial ad you need to load it first using the loadInterstitial()
method, then if loaded successfuly, call the method showInterstitial()
to show the ad on the screen.
You can detect if an interstitial has been loaded and is ready to be displayed by listening to the onInterstitialLoadedEvent
event.
Here is sample code showing the two-step process for showing the interstitial ad:
...
void Start () {
#if UNITY_ANDROID
//subscribe for onInterstitialLoadedEvent event
YaftaMobileManager.onInterstitialLoadedEvent += onInterstitialLoadedEvent;
#endif
}
...
// request an interstitial ad
#if UNITY_ANDROID
YaftaMobile.loadInterstitial();
#endif
.....
// show the interstitial ad
void onInterstitialLoadedEvent(){
YaftaMobile.showInterstitial ();
}
The YaftaMobile SDK emits events that let you know what happens during the ad life-cycle. To listen to ad events, you need to subscribe to them using += operator
The available ad events to listen to are listed below:
//Fired when an ad is successfully loaded.
public static event Action onAdLoadedEvent;
//Fired when an ad is clicked.
public static event Action onAdClickedEvent;
//Fired when a failure occurred during ad loading.
public static event Action< int , string> onAdErrorEvent;
//Fired when an interstitial ad is loaded.
public static event Action onInterstitialLoadedEvent;
//Fired when an interstitial ad is clicked.
public static event Action onInterstitialClickedEvent;
//Fired when a failure occurred during interstitial ad loading.
public static event Action< int , string> onInterstitialErrorEvent;
//Fired when an interstitial ad is closed.
public static event Action onInterstitialDismissedEvent;
The following code shows how to subscribe to onInterstitialErrorEvent
event
void Start () {
....
#if UNITY_ANDROID
YaftaMobileManager.onInterstitialErrorEvent += onInterstitialErrorEvent;
#endif
.....
}
void onInterstitialErrorEvent(int code,string message){
Debug.Log ("onInterstitialErrorEvent fired with code: " + code+ " ,and message: "+ message); // print error code and message
}
The SDK Integration can be tested by enabling test mode to verify that the set up SDK side has been completed correctly. You can enable test mode using the setTestMode(boolean testMode)
method
Add the following to enable test mode:
#if UNITY_ANDROID
YaftaMobile.setTestMode(true);
#endif