void OnGUI() { var x = 10; GUI.enabled = webViewObject.CanGoBack(); if (GUI.Button(new Rect(x, 10, 80, 80), "<")) { webViewObject.GoBack(); } GUI.enabled = true; x += 90; GUI.enabled = webViewObject.CanGoForward(); if (GUI.Button(new Rect(x, 10, 80, 80), ">")) { webViewObject.GoForward(); } GUI.enabled = true; x += 90; if (GUI.Button(new Rect(x, 10, 80, 80), "r")) { webViewObject.Reload(); } x += 90; GUI.TextField(new Rect(x, 10, 180, 80), "" + webViewObject.Progress()); x += 190; if (GUI.Button(new Rect(x, 10, 80, 80), "*")) { var g = GameObject.Find("WebViewObject"); if (g != null) { Destroy(g); } else { StartCoroutine(Start()); } } x += 90; if (GUI.Button(new Rect(x, 10, 80, 80), "c")) { Debug.Log(webViewObject.GetCookies(Url)); } x += 90; if (GUI.Button(new Rect(x, 10, 80, 80), "x")) { webViewObject.ClearCookies(); } x += 90; }
void OnGUI() { GUI.enabled = webViewObject.CanGoBack(); if (GUI.Button(new Rect(10, 10, 80, 80), "<")) { webViewObject.GoBack(); } GUI.enabled = true; GUI.enabled = webViewObject.CanGoForward(); if (GUI.Button(new Rect(100, 10, 80, 80), ">")) { webViewObject.GoForward(); } GUI.enabled = true; GUI.TextField(new Rect(200, 10, 300, 80), "" + webViewObject.Progress()); if (GUI.Button(new Rect(600, 10, 80, 80), "*")) { var g = GameObject.Find("WebViewObject"); if (g != null) { Destroy(g); } else { StartCoroutine(Start()); } } GUI.enabled = true; if (GUI.Button(new Rect(700, 10, 80, 80), "c")) { Debug.Log(webViewObject.GetCookies(Url)); } GUI.enabled = true; }
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; }
IEnumerator Start() { Screen.orientation = ScreenOrientation.Portrait; webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("cb : CallFromJS[{0}]", msg)); //status.text = msg; //status.GetComponent<Animation>().Play(); // Try validating with cookie if (!string.IsNullOrEmpty(msg)) { string authToken; bool success = TryGetToken(msg, out authToken); if (success) { FinishLogin(authToken); return; } } }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); //status.text = msg; //status.GetComponent<Animation>().Play(); }, started: (msg) => { Debug.Log(string.Format("started : CallOnStarted[{0}]", msg)); }, ld: (msg) => { string currentUrl = msg; Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); canvas.SetActive(false); loginPage.SetActive(false); webViewObject.SetVisibility(true); if (!currentUrl.Contains(AUTH_TOKEN_URL)) { return; } string cookieValue = webViewObject.GetCookies(AUTH_TOKEN_URL); Debug.LogFormat("Cookies from {0}: {1}", currentUrl, cookieValue); // Try validating with cookie if (!string.IsNullOrEmpty(cookieValue)) { string authToken; bool success = TryGetToken(cookieValue, out authToken); if (success) { FinishLogin(authToken); return; } } #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 // Try getting cookie from js webViewObject.EvaluateJS(@"Unity.call(document.cookie)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif webViewObject.SetMargins(Screen.width / 8, Screen.height / 4, Screen.width / 8, Screen.height / 4); //webViewObject.SetVisibility(true); webViewObject.ClearCookies(); #if !UNITY_WEBPLAYER if (Url.StartsWith("http") || Url.StartsWith("https")) { 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 { var www = new WWW(src); yield return(www); result = www.bytes; } 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") || Url.StartsWith("https")) { 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; }