Exemplo n.º 1
0
    IEnumerator Start()
    {
        BetterStreamingAssets.Initialize();
        webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>();
        webViewObject.Init(
            cb: (msg) =>
        {
            Debug.Log(string.Format("CallFromJS[{0}]", msg));
            HandleIncomingMessage(msg);
        },
            err: (msg) =>
        {
            Debug.Log(string.Format("CallOnError[{0}]", msg));
        },
            started: (msg) =>
        {
            Debug.Log(string.Format("CallOnStarted[{0}]", msg));
        },
            hooked: (msg) =>
        {
            Debug.Log(string.Format("CallOnHooked[{0}]", msg));
        },
            ld: (msg) =>
        {
            Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
#if UNITY_EDITOR_OSX || (!UNITY_ANDROID && !UNITY_WEBPLAYER && !UNITY_WEBGL)
            // NOTE: depending on the situation, you might prefer
            // the 'iframe' approach.
            // cf. https://github.com/gree/unity-webview/issues/189
#if true
            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;
                      }
                    }
                  }
                ");
#else
            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) {
                        var iframe = document.createElement('IFRAME');
                        iframe.setAttribute('src', 'unity:' + msg);
                        document.documentElement.appendChild(iframe);
                        iframe.parentNode.removeChild(iframe);
                        iframe = null;
                      }
                    }
                  }
                ");
#endif
#elif UNITY_WEBPLAYER || UNITY_WEBGL
            webViewObject.EvaluateJS(
                "window.Unity = {" +
                "   call:function(msg) {" +
                "       parent.unityWebView.sendMessage('WebViewObject', msg)" +
                "   }" +
                "};");
#endif
            webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)");
        },
            //transparent: false,
            //zoom: true,
            //ua: "custom user agent string",
#if UNITY_EDITOR
            separated: false,
#endif
            enableWKWebView: true,
            wkContentMode: 0);  // 0: recommended, 1: mobile, 2: desktop
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
        webViewObject.bitmapRefreshCycle = 1;
#endif
        // cf. https://github.com/gree/unity-webview/pull/512
        // Added alertDialogEnabled flag to enable/disable alert/confirm/prompt dialogs. by KojiNakamaru · Pull Request #512 · gree/unity-webview
        //webViewObject.SetAlertDialogEnabled(false);

        // cf. https://github.com/gree/unity-webview/pull/550
        // introduced SetURLPattern(..., hookPattern). by KojiNakamaru · Pull Request #550 · gree/unity-webview
        //webViewObject.SetURLPattern("", "^https://.*youtube.com", "^https://.*google.com");

        // cf. https://github.com/gree/unity-webview/pull/570
        // Add BASIC authentication feature (Android and iOS with WKWebView only) by takeh1k0 · Pull Request #570 · gree/unity-webview
        //webViewObject.SetBasicAuthInfo("id", "password");

        // webViewObject.SetMargins(5, 100, 5, Screen.height / 4);
        webViewObject.SetVisibility(true);


        //HandleIncomingMessage("{\"action\": \"Initialize\", \"payload\": \"{\\\"name\\\": \\\"afonso\\\", \\\"player\\\": \\\"maduro\\\"}\"}");

#if UNITY_IPHONE || UNITY_STANDALONE_OSX
        webViewObject.SetScrollBounceEnabled(false);
#endif

#if !UNITY_WEBPLAYER && !UNITY_WEBGL
        if (Url.StartsWith("http"))
        {
            webViewObject.LoadURL(Url.Replace(" ", "%20"));
        }
        else
        {
            //var sourcePath = System.IO.Path.Combine(Application.streamingAssetsPath, Url);
            var sourceBasePath = Application.streamingAssetsPath;
            //var sourceBasePath = "jar:file:/" + Application.dataPath + "!/assets/";
            var      destinationPath = Application.persistentDataPath;
            var      index           = System.IO.Path.Combine(destinationPath, "index.html");
            string[] files           = BetterStreamingAssets.GetFiles(System.IO.Path.Combine(Url == "" ? "/" : Url), "*", System.IO.SearchOption.AllDirectories);
            //string[] directories = System.IO.Directory.GetDirectories(sourcePath);
            string[] directories = GetPathArrayDirectories(files);

            foreach (string dirPath in directories)
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(destinationPath, dirPath));
            }

            foreach (string newPath in files)
            {
                string fromPath = System.IO.Path.Combine(sourceBasePath, newPath);
                string toPath   = System.IO.Path.Combine(destinationPath, newPath);

                if (fromPath.Contains("://"))
                {
                    // Android
                    UnityWebRequest unityWebRequest = UnityWebRequest.Get(fromPath);
                    yield return(unityWebRequest.SendWebRequest());

                    byte[] result = unityWebRequest.downloadHandler.data;
                    System.IO.File.WriteAllBytes(toPath, result);
                }
                else
                {
                    System.IO.File.Copy(fromPath, toPath, true);
                }
            }

            webViewObject.LoadURL("file://" + index);
        }
#else
        if (Url.StartsWith("http"))
        {
            webViewObject.LoadURL(Url.Replace(" ", "%20"));
        }
        else
        {
            webViewObject.LoadURL("StreamingAssets/" + Url + "index.html");
        }
#endif
        yield break;
    }
Exemplo n.º 2
0
    protected override void Awake()
    {
        base.Awake();

        if (GameObject.FindGameObjectsWithTag("WebView").Length > 1)
        {
            Destroy(gameObject);
            return;
        }

        DontDestroyOnLoad(gameObject);

        webView.Init(
            cb: (msg) =>
        {
            Debug.Log(string.Format("CallFromJS[{0}]", msg));
        },
            err: (msg) =>
        {
            Debug.Log(string.Format("CallOnError[{0}]", msg));
        },
            started: (msg) =>
        {
            Debug.Log(string.Format("CallOnStarted[{0}]", msg));
        },
            hooked: (msg) =>
        {
            Debug.Log(string.Format("CallOnHooked[{0}]", msg));
            try
            {
                var uri = new Uri(msg);
                Application.OpenURL(msg);
            }
            catch
            {
                // Ignored
            }
        },
            ld: (msg) =>
        {
            Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
#if UNITY_EDITOR_OSX || !UNITY_ANDROID
            webView.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;
                      }
                    }
                  }
                ");
#endif
            webView.EvaluateJS(@"document.body.style.background = 'none';");
            OnWebViewLoaded.Invoke(msg);
        },
            //ua: "custom user agent string",
            enableWKWebView: true,
            transparent: true
            );
        webView.SetMargins(0, 0, (int)((48 + 96 + 48) / 1920f * UnityEngine.Screen.width), 0);
        webView.SetScrollBounceEnabled(true);
    }