sobota, 09 august 2014 19:28 Written by 5257 times
Rate this item
(4 votes)

Unity3d - Admob android plugin s Google Play Services - Interstitial Ads

Pred časom, Google vydal nový Google Play Services plugin. Tento plugin je potrebný na zobrazenie reklamy v naších hrách. Ukážeme si ako spraviť wrappovaciu dll v java aby sme mohli tento plugin použiť v unity3d.

Interstitial Ads je reklama, ktorá sa zobrazi na celej obrazovke. Užívateľ na ňu buď klikne, alebo ju zatvorí a vráti sa k hre. Ak sa chcete dozvedieť viac o Interstitials kliknite sem.

Podmienky :

1.Krok - Vytvorenie projektu

Otvoríme Eclipse a vytvoríme nový project. File -> New -> Project...

obr1

...vyberieme Android application project.

obr2

Okienko vyplníme podľa nasledujúceho obr:

obr3

Necháme zaškrtnuté iba Create Project in Workspace

obr4

2.Krok - Pridanie google-play-services.jar a UnityPlayer.jar

V menu vyberieme položku Project -> Properties  a preklikneme sa do záložky Java Build Path

obr5

Kliknutim na tlačidlo Add External JARs pridáme

  • C:\Program Files\Unity\Editor\Data\PlaybackEngines\androidplayer\development\bin\classes.jar
  • C:\adt-bundle-windows-x86\sdk\extras\google\google_play_services\libproject\google-play-services_lib\libs\google-play-services.jar

Následne klikneme na OK. Do projektu pridáme súbor class do ktorého napíšeme nasledujúci kód (inšpirujeme sa podľa vzorového príkladu od googlu):

Kód v jave :

 

package com.projectikeu.admobplugin;
import com.google.android.gms.ads.*;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherInterstitialAd;
import com.unity3d.player.UnityPlayer;
import android.app.Activity;
public class Interstitial {
	
    private Activity activity; 
    private PublisherInterstitialAd interstitial;
    private boolean isLoaded = false;
    
    public void DisplayInterstitial() {
		activity.runOnUiThread(new Runnable() {
			public void run(){
				if (interstitial.isLoaded()) {
					interstitial.show();
				}
			}
		});
	  }
	
	public boolean IsInterstitialLoaded() {
		return isLoaded;
	}
	
	private void loadNewInterstitial(String TestDeviceID)
	{
		PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
		.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		.addTestDevice(TestDeviceID)
		.build();
		
		interstitial.loadAd(adRequest);
	}
    
    public Interstitial (final String PublisherID,final String TestDeviceID) {
    	
    	activity = UnityPlayer.currentActivity;
         
	    activity.runOnUiThread(new Runnable() {
	        public void run(){
	            interstitial = new PublisherInterstitialAd(activity);
			    interstitial.setAdUnitId(PublisherID);
			    
			    interstitial.setAdListener(new AdListener() {
					public void onAdClosed() {
						PublisherAdRequest adRequest = new PublisherAdRequest.Builder()
						.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
						.addTestDevice(TestDeviceID)
						.build();
						
						interstitial.loadAd(adRequest);
						isLoaded = false;
					}
					
					public void onAdLoaded() {
						isLoaded = true;
					}
				});
			    
			    loadNewInterstitial(TestDeviceID);
	        }
	    });
    }
}

 

3.Krok - Export wrapovacej dll

V menu vyberieme možnosť  File -> Export a vyberieme Java -> JAR file .

obr6

 

4.Krok - Použitie v Unity3d

Kód v c# :

using UnityEngine;
using System;
public class ProjectikeuAdmob : MonoBehaviour {
    private string msg="nic";
    public string PublisherID = "YOUR_AD_UNIT_ID";
    public string TestDeviceID = "";
    private static AndroidJavaObject jo;
    // Dont destroy on load and prevent duplicate
    private static bool created = false;
    void Awake()
    {
        try
        {
            if (!created)
            {
                DontDestroyOnLoad(this.gameObject);
                created = true;
                initializeInterstitial();
            }
            else
            {
                Destroy(this.gameObject);
            }
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
    }
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 100, 100), msg);
    }
    void initializeInterstitial()
    {
        #if UNITY_ANDROID       
        jo = new AndroidJavaObject("com.projectikeu.admobplugin.Interstitial", PublisherID, TestDeviceID);
        #endif
    }
    /// <summary>
    /// Load and show the interstitial.
    /// </summary>
    public static void ShowInterstitial()
    {
#if UNITY_ANDROID       
        jo.Call("DisplayInterstitial");
#endif
    }
}

 

Súbor AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>
    <application
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
		<meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>
        <activity android:name="com.unity3d.player.UnityPlayerProxyActivity"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
        </activity>
        <activity android:name="com.unity3d.player.UnityPlayerNativeActivity"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
            <meta-data android:name="android.app.lib_name" android:value="unity" />
            <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
        </activity>
        <activity android:name="com.unity3d.player.VideoPlayer"
                  android:label="@string/app_name"
                  android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
        </activity>
        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

Umiestenie v unity 3d:

  • Assets\plugins\android\com.projectikeu.admob.jar
  • Assets\plugins\android\google-play-services.jar
  • Assets\plugins\android\AndroidManifest.xml

Version:

ver 2.0 google_play_services_version 7095000

Last modified on sobota, 23 máj 2015 17:11
Ing.Jaroslav Vadel

Som zakladateľom www.projectik.eu.

Hrám sa na programátora, ktorý ovláda:

c#,cpp,java,unity3d,php,html,NI testand,NI Vision Builder,Cognex In-Sight,NI LabView

"Naprogramovať program, ktorý funguje vie každy. Ale to, že program funguje ešte neznamena, že je napísany správne "

Website: www.projectik.eu