Giter Club home page Giter Club logo

unity-webview's Introduction

unity-webview

unity-webview is a plugin for Unity 5 that overlays WebView components on Unity view. It works on Android, iOS, Unity Web Player, and Mac (Windows is not supported for now).

unity-webview is derived from keijiro-san's https://github.com/keijiro/unity-webview-integration .

Sample Project

It is placed under sample/. You can open it and import the plugin as below:

  1. Open sample/Assets/Sample.unity.
  2. Open dist/unity-webview.unitypackage and import all files. It might be easier to extract dist/unity-webview.zip instead if you've imported unity-webview before.

Platform Specific Notes

Mac (Editor)

Auto Graphics API/Metal Editor Support

The current implementation reiles on several OpenGL APIs so you need to disable "Auto graphics API" and specify OpenGLCore as below.

auto-graphics-api-setting-for-mac.png

If you work only in (recent) Unity Editor, you may just disable "Metal Editor Support" (cf. gree#383 ).

metal-editor-support-setting-for-mac.png

App Transport Security

Since Unity 5.3.0, Unity.app is built with ATS (App Transport Security) enabled and non-secured connection (HTTP) is not permitted. If you want to open http://foo/bar.html with this plugin on Unity Mac Editor, you need to open /Applications/Unity5.3.4p3/Unity.app/Contents/Info.plist with a text editor and add the following,

--- Info.plist~	2016-04-11 18:29:25.000000000 +0900
+++ Info.plist	2016-04-15 16:17:28.000000000 +0900
@@ -57,5 +57,10 @@
 	<string>EditorApplicationPrincipalClass</string>
 	<key>UnityBuildNumber</key>
 	<string>b902ad490cea</string>
+	<key>NSAppTransportSecurity</key>
+	<dict>
+		<key>NSAllowsArbitraryLoads</key>
+		<true/>
+	</dict>
 </dict>
 </plist>

or invoke the following from your terminal,

/usr/libexec/PlistBuddy -c "Add NSAppTransportSecurity:NSAllowsArbitraryLoads bool true" /Applications/Unity/Unity.app/Contents/Info.plist
References

WebViewSeparated.bundle

WebViewSeparated.bundle is a variation of WebView.bundle. It is based on gree#161 . As noted in the pull-request, it shows a separate window and allows a developer to utilize the Safari debugger. For enabling it, please define WEBVIEW_SEPARATED.

iOS

The implementation now supports WKWebView but it is disabled by default. For enabling it, please set enableWKWebView as below:

        webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
        webViewObject.Init(
            ...
            enableWKWebView: true);

(cf. https://github.com/gree/unity-webview/blob/de9a25c0ab0622b15c15ecbc0c7cd85858aa7745/sample/Assets/Scripts/SampleWebView.cs#L94)

Please also note that this flag have no effect on platforms without WKWebView (such as iOS7 and Android).

Android

hardwareAccelerated

The main activity should have android:hardwareAccelerated="true", otherwise a webview won't run smoothly. Depending on unity versions, we need to set it as below (basically this will be done by post-process build scripts).

Unity 2018.1 or newer

Based on the technique discussed in https://forum.unity.com/threads/android-hardwareaccelerated-is-forced-false-in-all-activities.532786/ and https://github.com/Over17/UnityAndroidManifestCallback, Assets/Plugins/Editor/UnityWebViewPostprocessBuild.cs edit the manifest to set android:hardwareAccelerated="true". Please note this works with the gradle (not internal) build setting.

Unity 2017.x - 2018.0

Unity forcibly set android:hardwareAccelerated="false" regardless of its setting in Plugins/Android/AndroidManifest.xml, as discussed in gree#382 (see also gree#342 and https://forum.unity.com/threads/android-hardwareaccelerated-is-forced-false-in-all-activities.532786/ ), and there is no solution for automatically correcting this setting. Please export the project and manually correct AndroidManifest.xml.

Unity 5.x or older

After the initial build, Assets/Plugins/Editor/UnityWebViewPostprocessBuild.cs will copy sample/Temp/StatingArea/AndroidManifest-main.xml to sample/Assets/Plugins/Android/AndroidManifest.xml, edit the latter to add android:hardwareAccelerated="true" to <activity android:name="com.unity3d.player.UnityPlayerActivity" .... Then you need to build the app again to reflect this change.

NOTE: Unity 5.6.1p4 or newer (including 2017 1.0) seems to fix the following issue. cf. gree#212 (comment)

For Unity 5.6.0 and 5.6.1 (except 5.6.1p4), you also need to modify android:name from com.unity3d.player.UnityPlayerActivity to net.gree.unitywebview.CUnityPlayerActivity. This custom activity implementation will adjust Unity's SurfaceView z order. Please refer plugins/Android/src/net/gree/unitywebview/CUnityPlayerActivity.java and plugins/Android/src/net/gree/unitywebview/CUnityPlayer.java if you already have your own activity implementation.

How to build WebViewPlugin.jar

The following steps are for Mac but you can follow similar ones for Windows.

  1. Install Android Studio.
  2. Open Android Studio and select "Configure/SDK Manager", select the followings with "Show Package Details", and click OK.
  • SDK Platforms
    • Android 6.0 (Marshmallow)
      • Android SDK Platform 23
  • SDK Tools
    • Android SDK Build Tools
      • 28.0.2
  1. Open Terminal.app and perform the followings. You should find unity-webview/build/Packager/Assets/Plugins/Android/WebViewPlugin.jar if successful.
$ export ANDROID_HOME=~/Library/Android/sdk
$ export PATH=$PATH:~/Library/Android/sdk/platform-tools/bin:~/Library/Android/sdk/tools:~/Library/Android/sdk/tools/bin
$ cd unity-webview/plugins/Android
$ ./install.sh

Web Player

The implementation utilizes IFRAME so please put both "an_unityplayer_page.html" and "a_page_loaded_in_webview.html" should be placed on the same domain for avoiding cross-domain requests.

unity-webview's People

Contributors

kojinakamaru avatar splhack avatar dolow avatar n-yoda avatar okayasu avatar astrubel avatar drc-genki-kokubun avatar jake1256 avatar bala-kyawhtin-tecotec avatar shinobushiva avatar sumidatx avatar shinriyo avatar hiroyuki0115 avatar codec-abc avatar appirits-nakamatsu avatar rskorski-gsn avatar imkira avatar hallucinogen avatar jaredhays avatar aki-null avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.