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