コード例 #1
0
    IEnumerator Start()
    {
        //webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>();
        webViewObject.Init(
            cb: (msg) =>
        {
            Debugging.instance.DebugLog(string.Format("CallFromJS [ {0} ]", msg));
            status.text = msg;
            status.GetComponent <Animation>().Play();
            if (STATE == LOGINSTATE.receivewaitjson)
            {
                if (msg.Contains("token"))
                {
                    try
                    {
                        Debugging.instance.DebugLog($"jsonSting : {msg}");
                        loginAuth = JsonConvert.DeserializeObject <LoginAuth>(msg);
                        STATE     = LOGINSTATE.complete;
                        webViewObject.EvaluateJS(@"window.location.replace('" + Url + "');");
                        Debugging.instance.DebugLog("location.replace");

                        StartCoroutine(SendToken(REQUEST_TYPE.Post, loginAuth.member_no));
                    }
                    catch (Exception ex)
                    {
                        Debugging.instance.DebugLog($"jsonConvert file : {ex.Message}");
                    }

                    //window.location.assign('http://www.example.com');
                }
            }
        },
            err: (msg) =>
        {
            Debugging.instance.DebugLog(string.Format("CallOnError[ {0} ]", msg));
            status.text = msg;
            status.GetComponent <Animation>().Play();
        },
            started: (msg) =>
        {
            Debugging.instance.DebugLog(string.Format("CallOnStarted[ {0} ]", msg));

            if (msg.Contains(@"member/login"))
            {
                if (!msg.Contains("response_type=jwt"))
                {
                    Debugging.instance.DebugLog("page redirect");
                    webViewObject.LoadURL($"{Url}/member/login?response_type=jwt");
                }
            }
            else if (msg.Contains(@"/member/logout"))
            {
                StartCoroutine(SendToken(REQUEST_TYPE.Delete, loginAuth.member_no));
            }
        },
            hooked: (msg) =>
        {
            Debugging.instance.DebugLog(string.Format("CallOnHooked[{0}]", msg));
        },
            ld: (msg) =>
        {
            Debugging.instance.DebugLog(string.Format("CallOnLoaded[{0}]", msg));

            if (Debugging.instance.UrlText != null)
            {
                Debugging.instance.UrlText.text = msg;
            }

            if (msg.Contains(@"response_type=jwt"))
            {
                CallInnerText();
            }
            else if (msg.Contains(@"member/login"))
            {
                var cookies = webViewObject.GetCookies(Url);
                Debugging.instance.DebugLog($"cookies :: {cookies}");
            }
            else
            {
                //outher
            }


#if UNITY_EDITOR_OSX || !UNITY_ANDROID
            // 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
#endif

            //ua: "custom user agent string",
            //webViewObject.EvaluateJS(@"Unity.call('ua1 = ' + navigator.userAgent)");
        },
            enableWKWebView: true);


#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
        webViewObject.bitmapRefreshCycle = 1;
#endif
        //webViewObject.SetAlertDialogEnabled(false);
        //webViewObject.SetURLPattern("", "^https://.*youtube.com", "^https://.*google.com");

        if (Screen.width < Screen.height)
        {
            webViewObject.SetMargins(0, 0, 0, (int)(Screen.height * 0.1));
        }
        else
        {
            webViewObject.SetMargins(0, 0, 0, (int)(Screen.height - 192));
        }

        //Debug.Log($"log >>> : height : {Screen.height} , 0.1 : {(int)(Screen.height * 0.1)}  차이 {Screen.height- (int)(Screen.height * 0.1)}  ");
        //Debug.Log($"log >>> : width : {Screen.width} , 0.1 : {(int)(Screen.width * 0.1)}  차이 {Screen.width - (int)(Screen.width * 0.1)}  ");
        webViewObject.SetVisibility(true);

#if !UNITY_WEBPLAYER && !UNITY_WEBGL
        if (Url.StartsWith("http"))
        {
            webViewObject.LoadURL(Url.Replace(" ", "%20"));
        }
        else
        {
            var exts = new string[] {
                ".jpg",
                ".js",
                ".html"  // should be last
            };
            foreach (var ext in exts)
            {
                var    url    = Url.Replace(".html", ext);
                var    src    = System.IO.Path.Combine(Application.streamingAssetsPath, url);
                var    dst    = System.IO.Path.Combine(Application.persistentDataPath, url);
                byte[] result = null;
                if (src.Contains("://"))
                {  // for Android
#if UNITY_2018_4_OR_NEWER
                    // NOTE: a more complete code that utilizes UnityWebRequest can be found in https://github.com/gree/unity-webview/commit/2a07e82f760a8495aa3a77a23453f384869caba7#diff-4379160fa4c2a287f414c07eb10ee36d
                    var unityWebRequest = UnityWebRequest.Get(src);
                    yield return(unityWebRequest.SendWebRequest());

                    result = unityWebRequest.downloadHandler.data;
#else
                    var www = new WWW(src);
                    yield return(www);

                    result = www.bytes;
#endif
                }
                else
                {
                    result = System.IO.File.ReadAllBytes(src);
                }
                System.IO.File.WriteAllBytes(dst, result);
                if (ext == ".html")
                {
                    webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
                    break;
                }
            }
        }
#else
        if (Url.StartsWith("http"))
        {
            webViewObject.LoadURL(Url.Replace(" ", "%20"));
        }
        else
        {
            webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20"));
        }
        webViewObject.EvaluateJS(
            "parent.$(function() {" +
            "   window.Unity = {" +
            "       call:function(msg) {" +
            "           parent.unityWebView.sendMessage('WebViewObject', msg)" +
            "       }" +
            "   };" +
            "});");
#endif
        yield break;
    }
コード例 #2
0
 public void CallInnerText()
 {
     //webViewObject.EvaluateJS("Unity.call('"+tag+">>> '+ document.documentElement.innerText.toString());");
     webViewObject.EvaluateJS("Unity.call(document.documentElement.innerText.toString());");
     STATE = LOGINSTATE.receivewaitjson;
 }