Unity教程之-Unity3d在android工程加入广告Advertise

 

本篇unity3d教程我们来学习下如何在unity3d中加入广告advertise,最近用unity3d 做了一个小游戏,老板让加上广告。但是我本人不会java,也不会android,最后折腾了好几天,查了好多帖子,要么用的是旧版本的有米SDK,要么是说的不详细。最后我用以下方法成功加入了广告条。

准备:

1、首先需要有android开发环境:eclipse+android sdk+java,这个就不用我说了,网上有很多详细的帖子;

2、其次要有unity3d 软件;

3、有米SDK:Unity3d在android工程加入广告Advertise

有了上面的环境,下面就可以正式开始:

1、打开unity3d,新建一个工程,为了演示简单,我建了一个场景,加了一个脚本文件,Unity3d在android工程加入广告Advertise

Unity3d在android工程加入广告Advertise

2、building:注意下图中红色部分

Unity3d在android工程加入广告Advertise

然后export到英文路径下。比如我的是:D:\test\testYouMi\try

好了,现在我们用unity3d 做的android工程已经完成了,已经没有unity3d 的事情了,下面开始给这个工程加有米广告;

3、打开eclipse,import我们刚才的工程,然后finishingUnity3d在android工程加入广告Advertise

4、加入有米的SDK,导入jar,直接将YoumiSdk_v5.00_2015-01-08.jar拖到lib文件夹下,copy files ->OK;然后build path->add to path

5、修改UnityPlayerNativeActivity.java;

首先导入包:添加以下代码


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;

然后添加代码:


AdManager.getInstance(this).init("da4217c86b4902f1", "1f1d3f0007343c71", false);
// 加载插播资源
SpotManager.getInstance(this).loadSpotAds();
// 插屏出现动画效果,0:ANIM_NONE为无动画,1:ANIM_SIMPLE为简单动画效果,2:ANIM_ADVANCE为高级动画效果
SpotManager.getInstance(this).setAnimationType(SpotManager.ANIM_ADVANCE);
// 设置插屏动画的横竖屏展示方式,如果设置了横屏,则在有广告资源的情况下会是优先使用横屏图。
SpotManager.getInstance(this).setSpotOrientation(
SpotManager.ORIENTATION_PORTRAIT);

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// 设置广告条的悬浮位置
layoutParams.gravity = Gravity.BOTTOM | Gravity.END; // 这里示例为右下角
// 实例化广告条
AdView adView = new AdView(this, AdSize.FIT_SCREEN);
// 调用Activity的addContentView函数

this.addContentView(adView, layoutParams);

最终UnityPlayerNativeActivity.java代码如下,我们只添加了两个部分:


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)
{
requestWindowFeature(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);

setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();

AdManager.getInstance(this).init("da4217c86b4902f1", "1f1d3f0007343c71", false);
// 加载插播资源
SpotManager.getInstance(this).loadSpotAds();
// 插屏出现动画效果,0:ANIM_NONE为无动画,1:ANIM_SIMPLE为简单动画效果,2:ANIM_ADVANCE为高级动画效果
SpotManager.getInstance(this).setAnimationType(SpotManager.ANIM_ADVANCE);
// 设置插屏动画的横竖屏展示方式,如果设置了横屏,则在有广告资源的情况下会是优先使用横屏图。
SpotManager.getInstance(this).setSpotOrientation(
SpotManager.ORIENTATION_PORTRAIT);

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// 设置广告条的悬浮位置
layoutParams.gravity = Gravity.BOTTOM | Gravity.END; // 这里示例为右下角
// 实例化广告条
AdView adView = new AdView(this, AdSize.FIT_SCREEN);
// 调用Activity的addContentView函数

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 will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(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 is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@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, KeyEvent 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、修改AndroidMainfest.xml文件:记住

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

这句话不允许修改代码,所以我们要删掉android:debuggable=”false”,成为这样:

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

加入代码:


<!-- 配置权限 -->
<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" />

再继续加入代码:


<!-- 添加必须的组件 -->
<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>

最后AndroidMainfest.xml代码如下


<?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" />

<!-- 配置权限 -->
<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" />

<application android:icon="@drawable/app_icon" android:label="@string/app_name" >
<activity 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.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" />
</activity>

<!-- 添加必须的组件 -->
<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>

</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="20" />
<uses-feature android:glEsVersion="0x00020000" />
</manifest>

好了,大功告成,发布后可以利用真机测试下!本篇unity3d教程到此结束,下篇我们再会!