Unity3d Android Project to join the latest with rice ads

Source: Internet
Author: User

The reason for this is that I made a little game with Unity3d and the boss asked me to add ads. But I do not Java, but also do not write Android, and finally toss for a few days, check out a lot of messy posts, either the use of the old Rice SDK, or is said not detailed, fraught. Finally I used this method, finally successfully added the banner. In my approach, it's no problem even if Android is not developed. Interested can look at. </span>

Get ready:

1, the first need to have an Android development environment: Eclipse+android Sdk+java, this will not need me to say, online has a lot of detailed posts;

2, the second must have Unity3d software;

3, have the meter SDK:

With the above environment, the following can be formally started:

1, open Unity3d, a new project, in order to demonstrate the simple, I built a scene, add a script file,


2, Building: Notice the red part


Then export to the English path. For example mine is: D:\test\testYouMi\try

OK, now we use Unity3d to do the Android project has been completed, there is no unity3d things, the following began to the project to add a meter advertising;

3, open Eclipse,import We just the project, and then finishing

4. Add the SDK with the meter, import the jar, drag Youmisdk_v5.00_2015-01-08.jar directly to the Lib folder, copy files->ok; and build Path->add to Path

5, modify the Unityplayernativeactivity.java;

First import the package: Add the following code

Import Net.youmi.android.admanager;import Net.youmi.android.banner.adsize;import Net.youmi.android.banner.AdView; Import Net.youmi.android.spot.spotmanager;import Android.app.activity;import Android.os.bundle;import Android.view.gravity;import Android.view.menu;import Android.view.menuitem;import android.widget.FrameLayout;
Then add the code:

Admanager.getinstance (This). Init ("Da4217c86b4902f1", "1f1d3f0007343c71", false);//Load Interstitial resource spotmanager.getinstance ( This). Loadspotads ();//Interstitial animation effect, 0:anim_none for no animation, 1:anim_simple for simple animation effect, 2:anim_ ADVANCE is a high-level animation effect Spotmanager.getinstance (this). Setanimationtype (spotmanager.anim_advance);//Set the interstitial animation of the portrait and the display mode, If you set up a horizontal screen, you will prefer to use a horizontal screen in the event of an ad resource. Spotmanager.getinstance (This). Setspotorientation (spotmanager.orientation_portrait); Framelayout.layoutparams layoutparams = new Framelayout.layoutparams (FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT)//Set the hover position of the ad bar layoutparams.gravity = Gravity.bottom | Gravity.end; Here is an example of the lower right corner//instantiation of the banner AdView AdView = new AdView (this, adsize.fit_screen);// Call the activity's Addcontentview function This.addcontentview (AdView, layoutparams);

The final Unityplayernativeactivity.java code is as follows, and we've only added two parts:

Package Com.chuji.test;import Com.unity3d.player.*;import Android.app.nativeactivity;import Android.content.res.configuration;import Android.graphics.pixelformat;import Android.os.Bundle;import Android.view.keyevent;import Android.view.motionevent;import Android.view.view;import Android.view.Window;import Android.view.windowmanager;import Net.youmi.android.admanager;import Net.youmi.android.banner.adsize;import Net.youmi.android.banner.adview;import Net.youmi.android.spot.spotmanager;import Android.app.Activity;import Android.os.bundle;import Android.view.gravity;import Android.view.menu;import Android.view.MenuItem;import Android.widget.framelayout;public class Unityplayernativeactivity extends nativeactivity{protected UnityPlayer munityplayer;//don ' t change the name of this variable; Referenced from native code//Setup activity layout@override protected void OnCreate (Bundle savedinstancestate) {REQUESTW Indowfeature (Window.feature_no_title); super.oncreate (savedinstancestate); GetwindoW (). Takesurface (null); SetTheme (Android. R.style.theme_notitlebar_fullscreen); GetWindow (). SetFormat (pixelformat.rgb_565); munityplayer = new UnityPlayer ( This), if (Munityplayer.getsettings (). Getboolean ("Hide_status_bar", True)) GetWindow (). SetFlags ( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); SETCONTENTVI EW (Munityplayer); Munityplayer.requestfocus (); Admanager.getinstance (This). Init ("Da4217c86b4902f1", "1f1d3f0007343c71", false);//Load Interstitial resource spotmanager.getinstance ( This). Loadspotads ();//Interstitial animation effect, 0:anim_none for no animation, 1:anim_simple for simple animation effect, 2:anim_ ADVANCE is a high-level animation effect Spotmanager.getinstance (this). Setanimationtype (spotmanager.anim_advance);//Set the interstitial animation of the portrait and the display mode, If you set up a horizontal screen, you will prefer to use a horizontal screen in the event of an ad resource. Spotmanager.getinstance (This). Setspotorientation (spotmanager.orientation_portrait); Framelayout.layoutparams layoutparams = new Framelayout.layoutparams (FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);//Set the hover position of the ad bar layoutparams.gravity = Gravity.bottom | Gravity.end; Here is an example of the lower right corner//instantiation of the banner AdView AdView = new AdView (this, adsize.fit_screen);// Call activity's Addcontentview function This.addcontentview (AdView, layoutparams);} Quit unity@override protected void OnDestroy () {munityplayer.quit (); Super.ondestroy ();} Pause unity@override protected void OnPause () {super.onpause (); Munityplayer.pause ();} Resume unity@override protected void Onresume () {super.onresume (); Munityplayer.resume ();} This ensures the layout is correct @Override public void onconfigurationchanged (Configuration newconfig) {Super.on Configurationchanged (Newconfig); munityplayer.configurationchanged (newconfig);} Notify Unity of the focus change. @Override public void Onwindowfocuschanged (Boolean hasfocus) { Super.onwindowfocuschanged (Hasfocus); munityplayer.windowfocuschanged (Hasfocus);} For some reason the multiple keyevent type are not supported by the NDK.//force event injection by overriding Dispatchk Eyevent (). @Override public boolean dispatchkeyevent (KeyeVent event) {if (event.getaction () = = Keyevent.action_multiple) return munityplayer.injectevent (event); return Super.dispatchkeyevent (event);} Pass any events not handled by (unfocused) views straight to Unityplayer@override public boolean onKeyUp (int keycode, K Eyevent event) {return munityplayer.injectevent (event);} @Override public boolean onKeyDown (int keycode, keyevent event) {return munityplayer.injectevent (event);} @Override public boolean ontouchevent (Motionevent event) {return munityplayer.injectevent (event);} /*api12*/public boolean ongenericmotionevent (Motionevent event) {return munityplayer.injectevent (event);}}
6. Modify the Androidmainfest.xml file: Remember

<application android:icon= "@drawable/app_icon" android:label= "@string/app_name" android:debuggable= "false" >

This sentence is not allowed to modify the code, so we have to delete the android:debuggable= "false" to become this:

<application android:icon= "@drawable/app_icon" android:label= "@string/app_name" >

Add code:

<!--Configure permissions--    <uses-permission android:name= "Android.permission.INTERNET"/>    < Uses-permission android:name= "Android.permission.READ_PHONE_STATE"/>    <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>    <uses-permission android:name=" android.permission.ACCESS_ Wifi_state "/>    <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>    < Uses-permission android:name= "Android.permission.GET_TASKS"/>    <uses-permission android:name= " Com.android.launcher.permission.INSTALL_SHORTCUT "/>    <uses-permission android:name=" Android.permission.CHANGE_CONFIGURATION "/>

Then continue adding code:

<!--add Required components--        <service android:name= "Net.youmi.android.AdService" >        </service>        <activity            android:name= "Net.youmi.android.AdBrowser"            android:theme= "@android: Style/theme.notitlebar ">        </activity>        <receiver android:name=" Net.youmi.android.AdReceiver ">            < intent-filter>                <action android:name= "Android.intent.action.PACKAGE_ADDED"/>                <action android: Name= "Android.intent.action.PACKAGE_INSTALL"/>                <data android:scheme= "package"/>            </ Intent-filter>        </receiver>

The final Androidmainfest.xml code is as follows

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.chuji.test "Android:theme=" @android: Style/theme.notitlebar "android:versionname=" 1.0 " Android:versioncode= "1" android:installlocation= "preferexternal" > <supports-screens android:smallScreens= " True "android:normalscreens=" true "android:largescreens=" true "android:xlargescreens=" true "android:anydensity=" True "/> <!--Configure permissions--<uses-permission android:name=" Android.permission.INTERNET "/> <uses-perm Ission android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/> <uses-permission android:name=" android.permission.ACCESS_WIFI_  State "/> <uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/> <uses-permission Android:name= "Android.permission.GET_TASKS"/> <uses-permission android:name= "Com.android.lAuncher.permission.INSTALL_SHORTCUT "/> <uses-permission android:name=" android.permission.CHANGE_ CONFIGURATION "/> <application android:icon=" @drawable/app_icon "android:label=" @string/app_name "> <act Ivity android:label= "@string/app_name" android:screenorientation= "Fullsensor" android:launchmode= "SingleTask" Android:configchanges= "mcc|mnc|locale|touchscreen|keyboard|keyboardhidden|navigation|orientation|screenlayout| Uimode|screensize|smallestscreensize|fontscale "android:name=" com.chuji.test.UnityPlayerNativeActivity "> < intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android Oid.intent.category.LAUNCHER "/> </intent-filter> <meta-data android:name=" Unityplayer. Unityactivity "android:value=" true "/> <meta-data android:name=" Unityplayer. Forwardnativeeventstodalvik "android:value=" false "/> </activity> <!--add Required components--&LT;service android:name= "Net.youmi.android.AdService" > </service> <activity android:n         Ame= "Net.youmi.android.AdBrowser" Android:theme= "@android: Style/theme.notitlebar" > </activity> <receiver android:name= "Net.youmi.android.AdReceiver" > <intent-filter> < Action android:name= "Android.intent.action.PACKAGE_ADDED"/> <action android:name= "android.intent.act Ion. Package_install "/> <data android:scheme=" package "/> </intent-filter> &lt ;/receiver> </application> <uses-sdk android:minsdkversion= "9" android:targetsdkversion= "/>" < Uses-feature android:glesversion= "0x00020000"/></manifest>

7:, take the real machine test as follows:


Unity3d Android Project to join the latest with rice ads

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.