Unity: Display AdMob banner in an existing Android game scene

Source: Internet
Author: User
Tags admob

about how to display AdMob banners on an existing Android project site, using a simple Android jar file in an Android project doesn't seem like a simple thing. I finally managed to get AdMob to unify the Android game at the current job. For this example, I used the unified Windows version 3.5.2 and the latest eclipse for Android plugin.

Prerequisite
I assume you have an eclipse plugin that works on your PC to install Android. If not, follow this tutorial to prepare the workspace: Https://github.com/unity-plugins/Unity-Admob.

Part of the Eclipse/android
• If you have not yet done so, download the AdMob jar file and register an account.

• Create a new Android project to ensure that the namespace (package name) is the same. You can set the namespace by building a uniform, Settings > Player Settings >android tab > Other Settings > "Bundle Identifiers". In my example, I use My.android.game.

• Copy the AdMob jar file/libs to the project's large folder.

• Search for the Classes.jar file under your installation directory file and copy it to the Libsfolder Eclipse project file.

• To test realistic ads, we first create a regular Android class called Admobactivity.javaa. As shown below:
<!--base Permission--
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

<!--base permission for location-->
<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>

<!--base permission required by Chartboost and baidu-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

<!--permission required by Mmedia--
<uses-permission android:name= "Android.permission.RECORD_AUDIO"/>
<uses-feature android:name= "Android.hardware.microphone" android:required= "false"/>

• Update the Androidmanifest.xml file for your eclipse project. As shown below:
<activity android:name= "Com.jirbo.adcolony.AdColonyOverlay" android:configchanges= "keyboardhidden|orientation |screensize "

Android:theme= "@android: Style/theme.translucent.notitlebar.fullscreen"/>

<activity android:name= "Com.jirbo.adcolony.AdColonyFullscreen" android:configchanges= "keyboardhidden| Orientation|screensize "

Android:theme= "@android: Style/theme.black.notitlebar.fullscreen"/>

<activity android:name= "Com.jirbo.adcolony.AdColonyBrowser" android:configchanges= "keyboardhidden|orientation |screensize "

Android:theme= "@android: Style/theme.black.notitlebar.fullscreen"/>
<!--Guang Dian Tong--
<service android:name= "Com.qq.e.comm.downloadservice" android:exported= "false"/>
<activity android:name= "com.qq.e.ads.adactivity" android:configchanges= "keyboard|keyboardhidden|orientation| ScreenSize "/>
Be sure to replace the your_publisher_id AdMob publisher ID with the actual.
• Build and run the app on your Android phone and you'll see the AdMob banner displayed. If you don't see the AdMob tutorial on how to implement banners for Android apps, this tutorial will still be implemented.

• To create a class called Admobunityactivity.java, you can use the Admobactivity.java class you created earlier as the basis. The class is like this:
Activity android:name= "Com.amazon.device.ads.AdActivity"

android:configchanges= "Keyboardhidden|orientation|screensize"/>

<!--InMobi--
<activity android:name= "Com.inmobi.androidsdk.IMBrowserActivity"
Android:configchanges= "Keyboardhidden|orientation|keyboard|smallestscreensize|screensize"
Android:theme= "@android: Style/theme.translucent.notitlebar"
Android:hardwareaccelerated= "true"/>

<!--millennial Media--
<activity android:name= "Com.millennialmedia.android.MMActivity"
Android:theme= "@android: Style/theme.translucent.notitlebar"
Android:configchanges= "Keyboardhidden|orientation|keyboard|screensize" ></activity>

<!--Keymob--
<activity android:name= "com.keymob.sdk.core.KeymobActivity" Android:theme= "@android: Style/theme.dialog"

Android:configchanges= "Keyboard|keyboardhidden|orientation|screenlayout|uimode|screensize|smallestscreensize"
/>

Android:name= "Com.google.android.gms.ads.AdActivity"
Android:configchanges= "Keyboard|keyboardhidden|orientation|screenlayout|uimode|screensize|smallestscreensize"
Android:theme= "@android: Style/theme.translucent"/>

Now that the class extends unityplayeractivity the opposite activity, we create a static function setupadsstatic () That leaves the OnCreate () function almost empty. We also want to include the entire contents of this function in the
Admanager.getinstance (). Initfromkeymobservice (This, "1", New Adeventlistener (), false);
Otherwise we will get an error or crash when calling the function, and some of the content is slightly different in function to make it work with unity.
• Not entirely sure the following steps are necessary, but only to do so: Androidmanifest Add this line, the XML file inside the application tag:
<activity android:name= "Com.amazon.device.ads.AdActivity"
• Export the project to a jar file, right-click the Package Manager in the project, and select Export.
Now that you have finished the part in Eclipse, we have to add plugins to unity 3D.

Unity section
• Copy create jar file into your Unity Android project, insert folder/plugin/Android/
• Simultaneously copy admob jar files to the same file/plugin/Android/
• The complete Androidmanifest.xml file is located on the Unity Android project with the following content
Class Adeventlistener implements Iadeventlistener {
@Override
public void onloadedsuccess (int arg0, Object arg1,
Platformadapter arg2) {
LOG.D (TAG, arg2+ "onloadedsuccess for type" +arg0 + "Withdata" +arg1);
if (arg0==adtypes.interstitial) {
((Iinterstitialplatform) arg2). Showinterstitial ();//When the ad is loaded successfully
}
}

@Override
public void Onloadedfail (int arg0, Object arg1, Platformadapter arg2) {
LOG.D (TAG, arg2+ "Onloadedfail for type" +arg0 + "Withdata" +arg1);
}

@Override
public void onadopened (int arg0, Object arg1, Platformadapter arg2) {
LOG.D (TAG, arg2+ "onadopened for type" +arg0 + "Withdata" +arg1);
}

@Override
public void onadclosed (int arg0, Object arg1, Platformadapter arg2) {
LOG.D (TAG, arg2+ "onadclosed for type" +arg0 + "Withdata" +arg1);
}

@Override
public void onadclicked (int arg0, Object arg1, Platformadapter arg2) {
LOG.D (TAG, arg2+ "onadclicked for type" +arg0 + "Withdata" +arg1);
}

@Override
public void Onotherevent (String eventName, int adtype, Object data,
Platformadapter adapter) {
LOG.D (TAG, adapter+ "onloadedsuccess for type" +adtype + "withevent" +eventname);
}
}
Ensure that the name in the Eclipse project is correct, changing the your_publisher_id value to the actual value of the AdMob Publisher ID. If you have used another Android plugin, it will be different than what is described in this article.

• The final display of the ad banner in the live Unity Android game creates or modifies the following C # script:
<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>

<!--base permission for location-->
<uses-permission android:name= "Android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>

The banner will be displayed at the top or bottom of your phone screen depending on your source code in the Eclipse project. Note that this code is only executed on an actual Android device, and the code is not displayed in the Unity Player! The "setupadsstatic" in your Eclipse project must be the same function name.

Unity: Display AdMob banner in an existing Android game scene

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.