コード例 #1
0
    private IEnumerator Start()
    {
        webViewObject.Init(
            msg =>
        {
            Debug.Log($"CallFromJS[{msg}]");
        },
            err: (msg) =>
        {
            Debug.Log($"CallOnError[{msg}]");
        },
            started: (msg) =>
        {
            Debug.Log($"CallOnStarted[{msg}]");
        },
            ld: (msg) =>
        {
            Debug.Log($"CallOnLoaded[{msg}]");
        },
            enableWKWebView: true);

        webViewObject.SetRectTransformMargin(rectTransform);
        if (url.Contains("http"))
        {
            webViewObject.LoadURL(url);
        }
        else
        {
            var    src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
            var    dst = System.IO.Path.Combine(Application.persistentDataPath, url);
            byte[] result;

            if (src.Contains("://"))    // for Android
            {
                var www = UnityWebRequest.Get(url);
                yield return(www.SendWebRequest());

                result = www.downloadHandler.data;
            }
            else
            {
                result = System.IO.File.ReadAllBytes(src);
            }

            System.IO.File.WriteAllBytes(dst, result);
            webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
        }



        webViewObject.SetVisibility(true);
    }
コード例 #2
0
    private IEnumerator Start()
    {
        webViewObject.Init(
            msg =>
        {
            Debug.Log($"CallFromJS[{msg}]");
            message.text = msg;
        },
            err: (msg) =>
        {
            Debug.Log($"CallOnError[{msg}]");
            message.text = $"ERROR: {msg}";
        },
            started: (msg) =>
        {
            Debug.Log($"CallOnStarted[{msg}]");
        },
            ld: (msg) =>
        {
            Debug.Log(string.Format("CallOnLoaded[{0}]", msg));

#if UNITY_EDITOR_OSX
            webViewObject.EvaluateJS(@"
                  if (window && window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.unityControl) {
                    window.Unity = {
                      call: function(msg) {
                        window.webkit.messageHandlers.unityControl.postMessage(msg);
                      }
                    }
                  } else {
                    window.Unity = {
                      call: function(msg) {
                        window.location = 'unity:' + msg;
                      }
                    }
                  }
                ");
            webViewObject.bitmapRefreshCycle = 1;
#endif

            webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
        },
            enableWKWebView: true);


        webViewObject.SetRectTransformMargin(rectTransform);

        var    src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
        var    dst = System.IO.Path.Combine(Application.persistentDataPath, url);
        byte[] result;

        if (src.Contains("://"))  // for Android
        {
            var www = UnityWebRequest.Get(src);
            yield return(www.SendWebRequest());

            result = www.downloadHandler.data;
        }
        else
        {
            result = System.IO.File.ReadAllBytes(src);
        }

        System.IO.File.WriteAllBytes(dst, result);
        webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
        webViewObject.SetVisibility(true);
    }