Unity3d game engine implements an instance of WebView open in Android _android

Source: Internet
Author: User

This article describes how to invoke the WebView component of Android in unity to implement an internal browser-style page switch. First open eclipse to create an Android project:
Unitytestactivity.java entry activity, unity will invoke the method in this activity to open the Web 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 will call this method to fight WebView public  
   void Startwebview (String str)  
   {  
       Intent Intent = new Intent (mcontext , webviewactivity.class);  
       This.startactivity (intent);  
   }  


Webviewactivity.java Unity in the notice to open the activity and then open WebView, there is no difficulty everyone look at should be able to master.

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.on  
    Create (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) {WebView  
      Activity.this.finish ();  
  }  
    }); Private class Webviewclient extends ANDROID.WEbkit. Webviewclient {@Override public boolean shouldoverrideurlloading (webview view, String URL) {//the mesh implemented here  
      Whether to continue to open a new link in the Web page, 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:o" rientation= "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= "close Web page"
      android:layout_width= "wrap_content"
      android:layout _height= "Wrap_content"
      />   
</LinearLayout>

And finally, Androidmanifest.xml.

 <?xml version= "1.0" encoding= "Utf-8"?> <manifest "xmlns:android=" Schemas.android.com/apk/res/android "package=" Com.xys "android:versioncode=" 1 "android:versionname=" 1.0 "> &L T;USES-SDK android:minsdkversion= "Ten"/> <application android:icon= "@drawable/ic_launcher" Android:lab El= "@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> <!--the right to connect to the Internet--> <uses-permission Android:name= "Android.permission.INTERNET"/> </manifest> 

OK to here the Java code has been completely written out and then put all. Java file packaging into a. class file, specific conversion methods you can refer to the relevant articles, here will not repeat the introduction.

Related Article

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.