Skip to content

Instantly share code, notes, and snippets.

@fangj99
Forked from TomoakiNagahara/file0.cs
Created May 22, 2018 05:39
Show Gist options
  • Save fangj99/e6e3339dac12b68ce40c421de32a8e2f to your computer and use it in GitHub Desktop.
Save fangj99/e6e3339dac12b68ce40c421de32a8e2f to your computer and use it in GitHub Desktop.
How to using UniWebView2 (UniWebView2の使用方法) ref: http://qiita.com/TomoakiNagahara/items/2785c85c7b416f88e08f
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 () {
}
}
<a href="uniwebview://command?foo=bar">button</a>
location.href = "uniwebview://command?foo=bar";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment