Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发者,开发者可以在工程的基础上继续添加新的视图,最后由开发者自行打包生成IPA包,发布程序。而Unity for Android打包直接生成APK包,等于说源代码开发者是看不到的,但是Unity的自身确实有些局限,针对Android平台我们需要学习如何在Unity中调用Android的JAVA代码。本章我们的目标是使用Unity的脚本打开Activity。首先我们创建一个普通的Android工程,目录结构如下图所示。
因为项目需要使用Unity提供的接口,所以需要将接口classes.jar引入至当前工程但中。接口包的所在地,打开Finder->应用程序->Unity->点击Unity图标,鼠标右键选择“显示包内容”->Contents->PlaybackEngines->AndroidPlayer->bin->classes.jar 。接口包引入工程后,开始编写JAVA代码。
UnityTestActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package
com
.
xys
;
import
android
.
content
.
Context
;
import
android
.
content
.
Intent
;
import
android
.
os
.
Bundle
;
import
com
.
unity3d
.
player
.
UnityPlayerActivity
;
public
class
UnityTestActivity
extends
UnityPlayerActivity
{
/** Called when the activity is first created. */
Context
mContext
=
null
;
@
Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
mContext
=
this
;
}
public
void
StartActivity0
(
String
name
)
{
Intent
intent
=
new
Intent
(
mContext
,
TestActivity0
.
class
)
;
intent
.
putExtra
(
"name"
,
name
)
;
this
.
startActivity
(
intent
)
;
}
public
void
StartActivity1
(
String
name
)
{
Intent
intent
=
new
Intent
(
mContext
,
TestActivity1
.
class
)
;
intent
.
putExtra
(
"name"
,
name
)
;
this
.
startActivity
(
intent
)
;
}
}
|
UnityTestActivity是主Activity,Unity程序一起动就会调用这个Activity,它是在AndroidManifest.xml中配置的。它需要继承UnityPlayerActivity,然而它就是刚刚我们引入的classes.jar包中提供的接口类。UnityTestActivity对外提供了两个方法接口,StartActivity0(String name) 方法与StartActivity1(String name)方法,这两个方法是在Unity中使用C#脚本调用的,意思是调用后程序将打开一个新的Activity,参数name也是由C#脚本传递过来的,接着将传递的String参数继续传递给新打开的Activity。
TestActivity0.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package
com
.
xys
;
import
android
.
app
.
Activity
;
import
android
.
os
.
Bundle
;
import
android
.
view
.
View
;
import
android
.
view
.
View
.
OnClickListener
;
import
android
.
widget
.
Button
;
import
android
.
widget
.
TextView
;
public
class
TestActivity0
extends
Activity
{
/** Called when the activity is first created. */
@
Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
main
)
;
TextView
text
=
(
TextView
)
this
.
findViewById
(
R
.
id
.
textView1
)
;
text
.
setText
(
this
.
getIntent
(
)
.
getStringExtra
(
"name"
)
)
;
Button
close
=
(
Button
)
this
.
findViewById
(
R
.
id
.
button0
)
;
close
.
setOnClickListener
(
new
OnClickListener
(
)
{
@
Override
public
void
onClick
(
View
v
)
{
TestActivity0
.
this
.
finish
(
)
;
}
}
)
;
}
}
|
TestActivity1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package
com
.
xys
;
import
android
.
app
.
Activity
;
import
android
.
os
.
Bundle
;
import
android
.
view
.
View
;
import
android
.
view
.
View
.
OnClickListener
;
import
android
.
widget
.
Button
;
import
android
.
widget
.
TextView
;
public
class
TestActivity1
extends
Activity
{
/** Called when the activity is first created. */
@
Override
public
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
)
;
setContentView
(
R
.
layout
.
main
)
;
TextView
text
=
(
TextView
)
this
.
findViewById
(
R
.
id
.
textView1
)
;
text
.
setText
(
this
.
getIntent
(
)
.
getStringExtra
(
"name"
)
)
;
Button
close
=
(
Button
)
this
.
findViewById
(
R
.
id
.
button0
)
;
close
.
setOnClickListener
(
new
OnClickListener
(
)
{
@
Override
public
void
onClick
(
View
v
)
{
TestActivity1
.
this
.
finish
(
)
;
}
}
)
;
}
public
void
Start
(
)
{
}
}
|
使用this.getIntent().getStringExtra(“name”)方法,得到上个界面传递过来的字符串,并且显示在屏幕中,用于区分新打开的Activity。TestActivity0与TestActivity1是两个新打开的Activity,它们属于Unity程序的子Activity所以它不需要继承UnityPlayerActivity,直接继承Activity即可,在代码中监听了一个按钮,意思是点击按钮后关闭当前的Activity。
接着是代码中打开的一个布局文件,这个应该没什问题,学过Android开发的朋友应该都能看懂,我就不详细解释了。
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
ScrollView
xmlns
:
android
=
"http://schemas.android.com/apk/res/android"
android
:
id
=
"@+id/screen"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"fill_parent"
android
:
orientation
=
"vertical"
>
<
LinearLayout
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"fill_parent"
android
:
orientation
=
"vertical"
>
<
ImageView
android
:
src
=
"@drawable/jay"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"fill_parent"
/
>
<
TextView
android
:
id
=
"@+id/textView0"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
textColor
=
"#000000"
android
:
textSize
=
"18dip"
android
:
background
=
"#00FF00"
android
:
text
=
"雨松MOMO 带你走进Unity for Android的世界"
android
:
gravity
=
"center_vertical¦center_horizontal"
/
>
<
TextView
android
:
id
=
"@+id/textView1"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
textColor
=
"#FFFFFF"
android
:
textSize
=
"18dip"
android
:
background
=
"#0000FF"
android
:
text
=
"Unity与Android之间的交互"
android
:
gravity
=
"center_vertical¦center_horizontal"
/
>
<
Button
android
:
id
=
"@+id/button0"
android
:
layout_width
=
"fill_parent"
android
:
layout_height
=
"wrap_content"
android
:
text
=
"关闭这个Activity"
/
>
<
/
LinearLayout
>
<
/
ScrollView
>
|
最后是本程序的AndroidManisest.xml,这个很重要,一定要配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
manifest
xmlns
:
android
=
"http://schemas.android.com/apk/res/android"
package
=
"com.xys"
android
:
versionCode
=
"1"
android
:
versionName
=
"1.0"
>
<
uses
-
sdk
android
:
minSdkVersion
=
"7"
/
>
<
application
android
:
icon
=
"@drawable/ic_launcher"
android
:
label
=
"@string/app_name"
>
<
activity
android
:
theme
=
"@android:style/Theme.NoTitleBar.Fullscreen"
android
:
name
=
".UnityTestActivity"
android
:
label
=
"@string/app_name"
>
<
intent
-
filter
>
<
action
android
:
name
=
"android.intent.action.MAIN"
/
>
<
category
android
:
name
=
"android.intent.category.LAUNCHER"
/
>
<
/
intent
-
filter
>
<
/
activity
>
<
activity
android
:
name
=
".TestActivity0"
android
:
theme
=
"@android:style/Theme.NoTitleBar.Fullscreen"
>
<
/
activity
>
<
activity
android
:
name
=
".TestActivity1"
android
:
theme
=
"@android:style/Theme.NoTitleBar.Fullscreen"
>
<
/
activity
>
<
/
application
>
<
/
manifest
>
|
大家请看清楚,这里一共配置了代码中的三个Activity,并且标志UnityTestActivity为主Activity。另外继承了UnityPlayerActivity后在Eclipse是运行不了的,除非拿到Unity中在真机下才行,请大家继续认真阅读本篇博文。
OK,到这里Android的代码已经写完,下面我们学习如何在Unity中去调用它。首先Build一下当前的Eclipse工程,代码所有的.class文件都生成在了Android工程的bin文件夹中,当前工程的路径是UnityTestActivity->bin->classes->com->xys->你的.class文件。下面需要对这些.class文件进行打包,苹果系统的话打开电脑的终端,cd到classes文件夹的目录下,接着执行代码
1
|
jar
-
cvf
class
.
jar *
|
这行代码的意思是把当前目录下的所有.class文件打包成.jar文件,保存文件名称为class.jar。接着class.jar文件就生成在bin->classes->目录中了。如下图所示,请大家仔细看一下解开的包应该与你的Android对应的包名保持一致,我的包名是com.xys,所以文件夹就是class->com->xys->.class代码。
确保无误后,请大家开始创建Unity工程。如下图所示,Unity工程中文件夹的结构如下,Plugins->Android的名称不能修改,必需保持一致。接着把Eclipse中Android的工程文件拷贝至这里,除了Android工程中的src文件夹,将其它文件夹全部拷贝至Plugins->Android文件夹中。最后在Plugins->Android文件夹中创建bin文件夹,然后将刚刚生成的.jar文件拷贝进来,jar的名称可以随便修改,但是jar包里面必须是com->xys->你的class文件,否则运行程序后提示找不到类文件。
最后在Unity工程中创建一个C#脚本,就是上图中的Test.cs,直接将它绑定在摄像机中,它用来通知界面打开Activity。如下图所示,利用GUI在屏幕中创建两个按钮,点击按钮打开不同的Activity。
Test.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
using
UnityEngine
;
using
System
.
Collections
;
public
class
Test
:
MonoBehaviour
{
// Update is called once per frame
void
Update
(
)
{
//当用户按下手机的返回键或home键退出游戏
if
(
Input
.
GetKeyDown
(
KeyCode
.
Escape
)
¦¦
Input
.
GetKeyDown
(
KeyCode
.
Home
)
)
{
Application
.
Quit
(
)
;
}
}
void
OnGUI
(
)
{
if
(
GUILayout
.
Button
(
"OPEN Activity01"
,
GUILayout
.
Height
(
100
)
)
)
{
//注释1
AndroidJavaClass
jc
=
new
AndroidJavaClass
(
"com.unity3d.player.UnityPlayer"
)
;
AndroidJavaObject
jo
=
jc
.
GetStatic
<
AndroidJavaObject
>
(
"currentActivity"
)
;
jo
.
Call
(
"StartActivity0"
,
"第一个Activity"
)
;
}
if
(
GUILayout
.
Button
(
"OPEN Activity02"
,
GUILayout
.
Height
(
100
)
)
)
{
AndroidJavaClass
jc
=
new
AndroidJavaClass
(
"com.unity3d.player.UnityPlayer"
)
;
AndroidJavaObject
jo
=
jc
.
GetStatic
<
AndroidJavaObject
>
(
"currentActivity"
)
;
jo
.
Call
(
"StartActivity1"
,
"第二个Activit"
)
;
}
}
}
|
注释1:先得到AndroidJavaClass,然后得到AndroidjavaObject就是当前Activity的对象,也就是我们在上面创建的主UnityTestActivity.JAVA。拿到它的对象后调用jo.Call()参数1表示调用UnityTestActivity.JAVA类中的方法名称,参数2表示该方法传递过去的参数。如下图所示:“第一个Activity”与“第二个Activit”就是我在C#中传递过去的字符串。
在打开的Activity中点击“关闭这个Activity按钮”,程序将继续回到原来的界面。
最后大家一定要注意Unity中的包名,要和Android工程保持一致,否则无法调用。如下图所示,Bundle Identifier* 当前项目为com.xys 。另外其它的选项也在其中,请大家仔细阅读。