Skip to content

Commit

Permalink
fixed SampleWebView.cs and related files to run the app also on android.
Browse files Browse the repository at this point in the history
  • Loading branch information
KojiNakamaru committed Sep 1, 2015
1 parent 54f729c commit 87b6763
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 292 deletions.
58 changes: 32 additions & 26 deletions sample/Assets/Scripts/SampleWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* 3. This notice may not be removed or altered from any source distribution.
*/

using System.Collections;
using UnityEngine;

public class SampleWebView : MonoBehaviour
Expand All @@ -27,56 +28,61 @@ public class SampleWebView : MonoBehaviour
public GUIText status;
WebViewObject webViewObject;

void Start()
IEnumerator Start()
{
webViewObject =
(new GameObject("WebViewObject")).AddComponent<WebViewObject>();
webViewObject.Init((msg)=>{
Debug.Log(string.Format("CallFromJS[{0}]", msg));
status.text = msg;
status.animation.Play();
status.GetComponent<Animation>().Play();
});

webViewObject.SetMargins(5, 5, 5, 40);
webViewObject.SetMargins(5, 5, 5, Screen.height / 4);
webViewObject.SetVisibility(true);

switch (Application.platform) {
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
case RuntimePlatform.IPhonePlayer:
webViewObject.LoadURL("file://" + Application.dataPath + "/WebPlayerTemplates/unity-webview/" + Url);
webViewObject.EvaluateJS(
"window.addEventListener('load', function() {" +
" window.Unity = {" +
" call:function(msg) {" +
" var iframe = document.createElement('IFRAME');" +
" iframe.setAttribute('src', 'unity:' + msg);" +
" document.documentElement.appendChild(iframe);" +
" iframe.parentNode.removeChild(iframe);" +
" iframe = null;" +
" }" +
" }" +
"}, false);");
webViewObject.EvaluateJS(
"window.addEventListener('load', function() {" +
" window.addEventListener('click', function() {" +
" Unity.call('clicked');" +
" }, false);" +
"}, false);");
case RuntimePlatform.Android:
var src = System.IO.Path.Combine(Application.streamingAssetsPath, Url);
var dst = System.IO.Path.Combine(Application.persistentDataPath, Url);
var result = "";
if (src.Contains("://")) {
var www = new WWW(src);
yield return www;
result = www.text;
} else {
result = System.IO.File.ReadAllText(src);
}
System.IO.File.WriteAllText(dst, result);
webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
if (Application.platform != RuntimePlatform.Android) {
webViewObject.EvaluateJS(
"window.addEventListener('load', function() {" +
" window.Unity = {" +
" call:function(msg) {" +
" var iframe = document.createElement('IFRAME');" +
" iframe.setAttribute('src', 'unity:' + msg;" +
" document.documentElement.appendChild(iframe);" +
" iframe.parentNode.removeChild(iframe);" +
" iframe = null;" +
" }" +
" }" +
"}, false);");
}
break;
case RuntimePlatform.OSXWebPlayer:
case RuntimePlatform.WindowsWebPlayer:
webViewObject.LoadURL(Url);
webViewObject.LoadURL(Url.Replace(" ", "%20"));
webViewObject.EvaluateJS(
"parent.$(function() {" +
" window.Unity = {" +
" call:function(msg) {" +
" parent.unityWebView.sendMessage('WebViewObject', msg)" +
" }" +
" };" +
" parent.$(window).click(function() {" +
" window.Unity.call('clicked');" +
" });" +
"});");
break;
}
Expand Down
9 changes: 9 additions & 0 deletions sample/Assets/StreamingAssets.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions sample/Assets/StreamingAssets/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Unity WebView</title>
</head>
<body>
<h1>Hello Unity WebView!</h1>
<hr/>
<h2><a href="javascript:void(0)" onclick="Unity.call('anchor');">[Send Message!]</a><h2>
<hr/>
<form method="get" action="unity:form">
<input id="input1" type="text" value="hoge" name="msg"/>
<input type="submit" value="Send Message!" onclick="Unity.call('form?msg=' + document.getElementById('input1').value); return false;"/>
</form>
</body>
</html>
2 changes: 0 additions & 2 deletions sample/Assets/WebPlayerTemplates.meta

This file was deleted.

2 changes: 0 additions & 2 deletions sample/Assets/WebPlayerTemplates/unity-webview.meta

This file was deleted.

136 changes: 0 additions & 136 deletions sample/Assets/WebPlayerTemplates/unity-webview/index.html

This file was deleted.

This file was deleted.

15 changes: 0 additions & 15 deletions sample/Assets/WebPlayerTemplates/unity-webview/sample.html

This file was deleted.

Binary file not shown.

This file was deleted.

99 changes: 0 additions & 99 deletions sample/Assets/WebPlayerTemplates/unity-webview/unity-webview.js

This file was deleted.

This file was deleted.

0 comments on commit 87b6763

Please sign in to comment.