Last active
August 29, 2015 14:09
-
-
Save yuya/607bc0b5abb091aee62b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Unity WebView Plugin</title> | |
<meta http-equiv="pragma" content="no-cache"> | |
<meta http-equiv="cache-control" content="no-cache"> | |
<meta http-equiv="expires" content="-1"> | |
<meta name="format-detection" content="telephone=no"> | |
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no"> | |
</head> | |
<body> | |
<div id="container"> | |
<div class="btn uni-open-url" data-url="http://yahoo.co.jp/">open url</div> | |
<div class="btn uni-open-url" data-url="http://ahoo.co.jp/">open url</div> | |
</div> | |
<script src="webview.js"></script> | |
<script> | |
(function (global, document) { | |
var Webview = global.WebViewMediator, | |
target = document.getElementsByClassName("uni-open-url"), | |
el, url; | |
function openUrl(url) { | |
if (!url) { | |
return; | |
} | |
console.log(url); | |
Webview.Call("/open_url", { | |
"url" : url | |
}); | |
} | |
for (var i = 0, l = target.length; i < l; ++i) { | |
el = target[i]; | |
el.addEventListener("click", function (event) { | |
url = event.currentTarget.getAttribute("data-url"); | |
openUrl(url); | |
}, false); | |
} | |
})(this, this.document); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class TestInterface : MonoBehaviour { | |
public string Url; | |
private string note; | |
public GUISkin guiSkin; | |
public GameObject redBoxPrefab; | |
public GameObject blueBoxPrefab; | |
WebViewObject webViewObject; | |
void Start() { | |
webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); | |
webViewObject.Init(name, "CustomUserAgent/1.0.0"); | |
} | |
void OnGUI() { | |
if (GUILayout.Button("TAP HERE", GUILayout.MinWidth(200), GUILayout.MinHeight(100))) { | |
ShowWebView(); | |
} | |
} | |
private void ShowWebView() { | |
webViewObject.LoadURL(Url); | |
webViewObject.SetMargins(12, Screen.height / 2 + 12, 12, 12); | |
webViewObject.Show(); | |
} | |
private void HideWebView() { | |
webViewObject.LoadURL("about:blank"); | |
webViewObject.Hide(); | |
} | |
private IEnumerator StartActivityIndicator() { | |
#if UNITY_IPHONE | |
Handheld.SetActivityIndicatorStyle(iOSActivityIndicatorStyle.White); | |
#elif UNITY_ANDROID | |
Handheld.SetActivityIndicatorStyle(AndroidActivityIndicatorStyle.Small); | |
#endif | |
Handheld.StartActivityIndicator(); | |
yield return new WaitForSeconds(0); | |
} | |
private void ShowIndicator() { | |
StartCoroutine(StartActivityIndicator()); | |
} | |
private void HideIndicator() { | |
Handheld.StopActivityIndicator(); | |
} | |
public void LoadURLWithIndicator(string url) { | |
if (string.IsNullOrEmpty(url)) { | |
return; | |
} | |
StartCoroutine(StartActivityIndicator()); | |
webViewObject.LoadURL(url); | |
} | |
private void Spawn(Hashtable args) { | |
GameObject prefab; | |
GameObject box; | |
if (args.ContainsKey("color")) { | |
prefab = (args["color"] as string == "red") ? redBoxPrefab : blueBoxPrefab; | |
} | |
else { | |
prefab = Random.value < 0.5 ? redBoxPrefab : blueBoxPrefab; | |
} | |
box = Instantiate(prefab, redBoxPrefab.transform.position, Random.rotation) as GameObject; | |
if (args.ContainsKey("scale")) { | |
box.transform.localScale = Vector3.one * float.Parse(args["scale"] as string); | |
} | |
} | |
private void WindowOnLoad() { | |
Debug.Log("WindowOnLoad"); | |
} | |
private void OpenUrl(Hashtable args) { | |
string url = args["url"] as string; | |
if (string.IsNullOrEmpty(url)) { | |
return; | |
} | |
Application.OpenURL(url); | |
} | |
private void onError() { | |
Debug.Log("OnErorr"); | |
} | |
public void CallMessage(WebViewObjectMessage message) { | |
switch (message.path) { | |
case "/spawn": | |
Spawn(message.args); | |
break; | |
case "/close": | |
HideWebView(); | |
break; | |
case "/show_indicator": | |
ShowIndicator(); | |
break; | |
case "/hide_indicator": | |
HideIndicator(); | |
break; | |
case "/load": | |
WindowOnLoad(); | |
break; | |
case "/open_url": | |
OpenUrl(message.args); | |
break; | |
case "/on_error": | |
onError(); | |
break; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment