The Unity3D game engine implements opening an instance of WebView in Android
- 2020-06-01 11:00:22
- OfStack
This article described how to invoke the WebView component in Android from Unity to achieve internal browser-style page switching. First open Eclipse to create an Android project:
UnityTestActivity.java entry Activity, Unity will call this Activity method to open the page.
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 This method is called in the WebView
public void StartWebView(String str)
{
Intent intent = new Intent(mContext,WebViewActivity.class);
this.startActivity(intent);
}
}
WebViewActivity. java Unity issued a notice in the open the WebView Activity then open, no difficulty can look should be mastered.
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) {
// The goal here is to continue clicking on the page 1 New links, or stay in the current program
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
}
And then the main xml
<?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=" Shut down web pages "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Finally the AndroidManifest xml
<?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>
<!-- Access to the Internet -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
The code of OK has been completely written here, and then all the.JAVA files are packaged into.class files. You can refer to the relevant articles for the specific conversion method, and I won't repeat the introduction here.