本文讲述了如何在Unity中调用Android中的WebView组件,实现内部浏览器样式的页面切换。首先打开Eclipse创建一个Android的工程:
UnityTestActivity.java 入口Activity ,Unity中会调用这个Activity中的方法从而打开网页。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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 { Context mContext = null ; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); mContext = this ; } //Unity中会调用这个方法,从而开打WebView public void StartWebView(String str) { Intent intent = new Intent(mContext,WebViewActivity. class ); this .startActivity(intent); } } |
WebViewActivity.java Unity中发出通知打开这个Activity 继而打开WebView,没有什么难点大家看看就应当能掌握。
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
37
|
package com.xys; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebView; import android.widget.Button; public class WebViewActivity extends Activity { private WebView webView; private Button close; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.main); webView = (WebView) findViewById(R.id.webView); webView.loadUrl( "http://www.baidu.com/" ); webView.getSettings().setJavaScriptEnabled( true ); webView.setWebViewClient( new WebViewClient()); close = (Button) findViewById(R.id.button); close.setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { WebViewActivity. this .finish(); } }); } private class WebViewClient extends android.webkit.WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //这里实现的目标是在网页中继续点开一个新链接,还是停留在当前程序中 view.loadUrl(url); return super .shouldOverrideUrlLoading(view, url); } } } |
然后是main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:orientation = "vertical" > < WebView android:id = "@+id/webView" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:layout_weight = "1.0" /> < Button android:id = "@+id/button" android:text = "关闭网页" android:layout_width = "wrap_content" android:layout_height = "wrap_content" /> </ LinearLayout > |
最后是AndroidManifest.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
|
<? 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 = "10" /> < application android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" > < activity 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 = ".WebViewActivity" > </ activity > </ application > <!-- 连接互联网的权限 --> < uses-permission android:name = "android.permission.INTERNET" /> </ manifest > |
OK 到这里JAVA代码已经完全写完,然后把所有.JAVA文件打包变成.class文件,具体转换的方法大家可以参照相关的文章,这里就不再重复介绍了。