Cocos2d-x embedded WebView on ios/android dual platform

Source: Internet
Author: User

Note: The article was first published in a personal blog, now the technical article transferred to this. http://blog.go3k.org/cocos2dx-add-webview/

In-game help and game announcements can be implemented by embedding the WebView scheme, which is handy for editing HTML to modify and update the style and content of the announcement.

iOS platform embedding

Embedded on the iOS platform is smooth, there are ready-made tutorials on the Internet, the key is to do the OC and C + + code in a mixed package.
Refer to the implementation in the following link: Cocos2d-x loading Web pages with UIWebView

Android platform embedding 1. Call Java code from cocos2d-x C + + code through JNI.

This is the prerequisite for Android to implement WebView embedding, so be sure to study it carefully again, or you won't be able to go down. For more information, see the following: JNI usage tutorial

This is a tutorial I've summarized for JNI, which basically covers all the points related to the use of JNI.

2. Implementation of Java code

The standard Android display WebView implementation, the reference code is as follows:

    public void Displaywebview () {This.runonuithread (new Runnable () {public void run () {//actinstance is   Member variable, which is the current activity.                M_webview is a member variable of type WebView linearlayout layout = new LinearLayout (actinstance);                Actinstance.addcontentview (Layout, new Layoutparams (layoutparams.fill_parent,layoutparams.fill_parent));                M_webview = new WebView (actinstance);                Layout.addview (M_webview); Linearlayout.layoutparams linearparams = (linearlayout.layoutparams) m_webview.getlayoutparams ();//selectable WebView position, X,                Y,width,height can be filled in arbitrarily, or as a function parameter passed in.                Linearparams.leftmargin = x;                Linearparams.topmargin = y;                Linearparams.width = width;                Linearparams.height = height;                M_webview.setlayoutparams (linearparams);//optional WebView configuration m_webview.setbackgroundcolor (0);               M_webview.getsettings (). Setcachemode (Websettings.load_no_cache); M_webview.getsettings (). setappcacheenabled (false);    }        }); }

The questions to note here are:

The code called by JNI is not in the main thread stream, the UI display needs to be placed in the main thread. This problem stuck to me for half a day, and finally asked the front brother to understand the need to do so on Android.

Source Sharing

Take the time to build a project on GitHub with the following address:

Https://github.com/go3k/CCXWebview

3. Configure the WebView to jump links in custom webview instead of opening the system browser.

This requires setting the Webviewclient for WebView and implementing the Shouldoverrideurlloading method, as follows:

Change the size and position of the WebView view

Sets the layoutparamaters,setlayoutparams of the WebView.

Reference link: How to add a UIWebView to Cocos2d-x?

Cocos2d-x embedded WebView on ios/android dual platform

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.