12
14

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.

【Webview】unity-webview / UniWebView3

Posted at

unity-webview

シンプルに表示

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SimpleWebView : MonoBehaviour {

    private string url = "https://www.google.co.jp/";
    WebViewObject webViewObject;

	void Start () {
        webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
        webViewObject.Init((msg) => {
            Debug.Log(msg);
        });
        webViewObject.LoadURL(url);
        // 中央に配置
        webViewObject.SetMargins(Screen.width / 4, Screen.height / 4, Screen.width / 4, Screen.height / 4);
        webViewObject.SetVisibility(true);
	}
}

ローカルファイルを表示

Assets/StreamingAssets/以下にHTMLなど必要ファイルを配置

・ファイルの読み込みはwebViewObject.LoadURL("file://${filePath}");に変更する。

string filePath = System.IO.Path.Combine(Application.persistentDataPath, "sample.html");
webViewObject.LoadURL("file://" + filePath.Replace(" ", "%20"));

参考
Unity上でWebViewを開く unity-webview
Unity3Dのgree webviewでローカルHTMLを表示し、JSからUnityのコードを実行してファイル読み書きする

UniWebView3

https://www.assetstore.unity3d.com/jp/?stay#!/content/92605
http://docs.uniwebview.com/api/

シンプルに表示

空のオブジェクトにUniWebView.csをアタッチするだけ。

スクリーンショット 2018-05-08 11.52.54.png

ローカルファイルを表示

・任意のオブジェクトに、デモの中にあるLocalHTMLSceneManager.csをアタッチ。

・fileNameにAssets/StreamingAssets/以下に配置したファイルのパスを記入。

・任意のタイミングでLoadFromFileメソッドを叩く。

LocalHTMLSceneManager.cs
using UnityEngine;

public class LocalHTMLSceneManager : MonoBehaviour {
    public string fileName;
    public string htmlText;
#if UNITY_IOS || UNITY_ANDROID || UNITY_WP8
    public void LoadFromFile() {

        var webView = CreateWebView();
        webView.url = UniWebViewHelper.streamingAssetURLForPath(fileName);
        webView.Load();
        webView.Show();
    }
    
    public void LoadFromText() {
        var webView = CreateWebView();

        webView.LoadHTMLString(htmlText, null);
        webView.Show();
    }

    UniWebView CreateWebView() {
        var webViewGameObject = GameObject.Find("WebView");
        if (webViewGameObject == null) {
            webViewGameObject = new GameObject("WebView");
        }

        var webView = webViewGameObject.AddComponent<UniWebView>();

        webView.toolBarShow = true;
        return webView;
    }

#else //End of #if UNITY_IOS || UNITY_ANDROID || UNITY_WP8
    void Start() {
        Debug.LogWarning("UniWebView only works on iOS/Android/WP8. Please switch to these platforms in Build Settings.");
    }
#endif
}

参考
UNITYでのWEBVIEW(埋め込みブラウザ)について

12
14
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
12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?