- http://uniwebview.onevcat.com/manual.html
- http://every-studio.com/wordpress/2015/04/01/unity5%E3%81%A7webview%E3%82%92%E4%BD%BF%E3%81%86%EF%BC%81/
- https://twitter.com/onevcat/status/532176093822722048
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
private UniWebView _webView;
private string _errorMessage;
// Use this for initialization
void Start () {
Debug.Log (this._errorMessage);
_webView = GetComponent<UniWebView>();
if (_webView == null) {
// User agent masquerade.
UniWebView.SetUserAgent ("Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A365 Safari/600.1.4");
_webView = gameObject.AddComponent<UniWebView>();
_webView.OnReceivedMessage += OnReceivedMessage;
_webView.OnLoadComplete += OnLoadComplete;
_webView.OnWebViewShouldClose += OnWebViewShouldClose;
_webView.OnEvalJavaScriptFinished += OnEvalJavaScriptFinished;
_webView.InsetsForScreenOreitation += InsetsForScreenOreitation;
}
_webView.url = "https://uqunie.com/useragent";
_webView.Load();
_webView.Show ();
_errorMessage = null;
}
void OnLoadComplete(UniWebView webView, bool success, string errorMessage) {
if (success) {
webView.Show();
} else {
Debug.Log("Something wrong in webview loading: " + errorMessage);
_errorMessage = errorMessage;
}
}
bool OnWebViewShouldClose(UniWebView webView) {
if (webView == _webView) {
_webView = null;
return true;
}
return false;
}
void OnReceivedMessage(UniWebView webView, UniWebViewMessage message) {
/**
* From WebView's message.
*
* Ex:
* uniwebview://command?foo=true&bar=false&num=1
*/
Debug.Log(message.rawMessage);
Debug.Log(message.path); // command
Debug.Log(message.args["foo"]); // true (string)
Debug.Log(message.args["bar"]); // false (string)
Debug.Log(message.args["num"]); // 1 (string)
}
void OnEvalJavaScriptFinished(UniWebView webView, string result) {
Debug.Log("js result: " + result);
// tipTextMesh.text = "<color=#000000>" + result + "</color>";
}
UniWebViewEdgeInsets InsetsForScreenOreitation(UniWebView webView, UniWebViewOrientation orientation) {
/*
int bottomInset = (int)(UniWebViewHelper.screenHeight * 0.1f);
if (orientation == UniWebViewOrientation.Portrait) {
return new UniWebViewEdgeInsets(5,5,bottomInset,5);
} else {
return new UniWebViewEdgeInsets(5,5,bottomInset,5);
}
*/
return new UniWebViewEdgeInsets(0, 0, 0, 0);
}
// Update is called once per frame
void Update () {
}
}
Call from html.
<a href="uniwebview://command?foo=bar">button</a>
Call from javascript.
location.href = "uniwebview://command?foo=bar";