9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

How to using UniWebView2 (UniWebView2の使用方法)

Last updated at Posted at 2015-07-22

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";
9
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?