IEnumerator Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init((msg)=>{ Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.GetComponent<Animation>().Play(); }); webViewObject.SetMargins(5, 5, 5, Screen.height / 4); webViewObject.SetVisibility(true); switch (Application.platform) { case RuntimePlatform.OSXEditor: case RuntimePlatform.OSXPlayer: case RuntimePlatform.IPhonePlayer: case RuntimePlatform.Android: var src = System.IO.Path.Combine(Application.streamingAssetsPath, Url); var dst = System.IO.Path.Combine(Application.persistentDataPath, Url); var result = ""; if (src.Contains("://")) { var www = new WWW(src); yield return www; result = www.text; } else { result = System.IO.File.ReadAllText(src); } System.IO.File.WriteAllText(dst, result); webViewObject.LoadURL("file://" + dst.Replace(" ", "%20")); if (Application.platform != RuntimePlatform.Android) { webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " 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;" + " }" + " }" + "}, false);"); } break; case RuntimePlatform.OSXWebPlayer: case RuntimePlatform.WindowsWebPlayer: webViewObject.LoadURL(Url.Replace(" ", "%20")); webViewObject.EvaluateJS( "parent.$(function() {" + " window.Unity = {" + " call:function(msg) {" + " parent.unityWebView.sendMessage('WebViewObject', msg)" + " }" + " };" + "});"); break; } }
// Use this for initialization void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init((msg) => { Debug.Log(msg); }); webViewObject.LoadURL("http://google.com/"); webViewObject.SetMargins(0, 0, 0, 100); webViewObject.SetVisibility(true); }
void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init((msg)=>{ Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.animation.Play(); }); webViewObject.SetMargins(5, 5, 5, 40); webViewObject.SetVisibility(true); switch (Application.platform) { case RuntimePlatform.OSXEditor: case RuntimePlatform.OSXPlayer: case RuntimePlatform.IPhonePlayer: webViewObject.LoadURL("files:/" + Application.dataPath + "/WebPlayerTemplates/unity-webview/" + Url); webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " 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;" + " }" + " }" + "}, false);"); webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " window.addEventListener('click', function() {" + " Unity.call('clicked');" + " }, false);" + "}, false);"); break; case RuntimePlatform.OSXWebPlayer: case RuntimePlatform.WindowsWebPlayer: webViewObject.LoadURL(Url); webViewObject.EvaluateJS( "parent.$(function() {" + " window.Unity = {" + " call:function(msg) {" + " parent.unityWebView.sendMessage('WebViewObject', msg)" + " }" + " };" + " parent.$(window).click(function() {" + " window.Unity.call('clicked');" + " });" + "});"); break; } }
void Start() { webViewObject = (new GameObject ("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init((msg) => { //Application.LoadLevel(msg);//Open on this WebView Application.OpenURL(msg);// Open external Brawser with return Message(URL) }); if(num == 1.0f){ webViewObject.LoadURL(url1); //Load URL }else if(num == 2.0f){ webViewObject.LoadURL(url2); }else if(num == 3.0f){ webViewObject.LoadURL(url3); } webViewObject.SetVisibility(true); // Show on //webViewObject.SetMargins(0,0,Screen.width/2,Screen.height - Screen.height/3);//HTML AD size setting }
private void OnEnable() { _object = new GameObject($"webview-{++_counter:X4}"); _webview = _object.AddComponent <WebViewObject>(); _webview.Init( ld: OnWebviewLoaded, enableWKWebView: true ); _webview.SetVisibility(_visible = visible); _webview.SetMargins(0, 0, 0, 0); Debug.Log("webview init"); var path = Path.Combine(Application.streamingAssetsPath, "sample.html"); var data = File.ReadAllBytes(path); Debug.Log($"html loaded: {data.Length} bytes"); _webview.LoadHTML(Encoding.UTF8.GetString(data), null); }
public static void InitWebview() { bool isMobile = Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer; Debug.Log("isMobile: " + isMobile); if (isMobile) { _webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject> (); _webViewObject.Init((token) => { Debug.Log("AccessToken " + token); // _webViewObject.SetVisibility (false); Destroy(_webViewObject); _vk.AccessToken = token; if (OAuthEvent != null) { OAuthEvent(true); } }); var oauth_url = OAUTH_URL.Replace("CLIENT_ID", _vk.ClientID); oauth_url = oauth_url.Replace("SCOPE", _vk.Scope); Debug.Log("oauth_url: " + oauth_url); _webViewObject.LoadURL(oauth_url); _webViewObject.SetVisibility(true); } else { Debug.Log("AccessToken " + _vk.AccessToken); if (OAuthEvent != null) { OAuthEvent(true); } //StartCoroutine ("GetWallUploadServer"); } }
public void show() { if (webViewObject == null) { webViewObject = GetComponent<WebViewObject>(); webViewObject.Init(); webViewObject.LoadURL(url); webViewObject.SetMargins(0,58,0,0); // マージン(単位px) 左、上、右、下 Debug.Log("nai"); } else Debug.Log("aru"); webViewObject.SetVisibility(true); _bg.SetActive(true); _closeButton.SetActive(true); Util.Resource.getModel().GetComponent<Model>().pause(); }
void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { if (msg == "clicked") { Debug.Log("hogehogehogehoge"); } }); webViewObject.LoadURL(url); webViewObject.SetVisibility(true); webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " window.addEventListener('click', function() {" + " Unity.call('clicked');" + " }, false);" + "}, false);"); webViewObject.SetMargins(0, 0, 0, 100); //下に100pxマージンを取る }
// Start is called before the first frame update void Start() { // ジャンル番号を受け取る int Genre = ButtonScript.getterNumber(); // ここに料理名を渡してあげる string encodeStr = HttpUtility.UrlEncode(urls[Genre, ChangeSprite.urlName]); string formalUrl = SearchURL + encodeStr; webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject> (); webViewObject.Init( ld: (msg) => Debug.Log(string.Format("CallOnLoaded[{0}]", msg)), enableWKWebView: true); // iPhoneの外枠からのマージン設定 webViewObject.SetMargins(0, 0, 0, 0); webViewObject.SetVisibility(true); // このURLのサイトを開く webViewObject.LoadURL(formalUrl); }
void PreviewHtml(string html) { using (var writer = new StreamWriter(htmlFilePath, false)) { writer.Write(html); writer.Close(); } // WebViewObject の初期化時にWebページ側から呼び出すことができるコールバック関数を定義する。 // Web側からコールバック関数呼び出すには、リンク要素の href 属性などURLを指定する箇所で // 'unit:(任意の文字列)' のように指定すると、コールバック関数が呼び出される。 // このとき、"(任意の文字列)"の部分が関数の引数として渡される。 description.Init((string msg) => { KX.D(DEBUG, "Call from Web view : " + msg); }); description.LoadURL("file://" + htmlFilePath); var rectTransform = description.GetComponent <RectTransform> (); AdjustWebViewMargin(description, rectTransform); description.SetVisibility(true); }
// Use this for initialization void Start() { // プラグインの読み込み webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); // インスタンス生成 webViewObject.Init( // JSコールバック cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); // status.text = msg; // status.GetComponent<Animation>().Play(); }, // エラーコールバック err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); // status.text = msg; // status.GetComponent<Animation>().Play(); }, // ロード時コールバック ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); }, // iPhone webkit webview で起動 enableWKWebView: true); // 全画面描画 // webViewObject.SetMargins (left, top, right, bottom); webViewObject.SetMargins(0, 0, 0, 0); // 描画開始 webViewObject.SetVisibility(true); // URLの読み込み開始 webViewObject.LoadURL(url); }
void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { //Application.LoadLevel(msg);//Open on this WebView Application.OpenURL(msg); // Open external Brawser with return Message(URL) }); if (num == 1.0f) { webViewObject.LoadURL(url1); //Load URL } else if (num == 2.0f) { webViewObject.LoadURL(url2); } else if (num == 3.0f) { webViewObject.LoadURL(url3); } webViewObject.SetVisibility(true); // Show on //webViewObject.SetMargins(0,0,Screen.width/2,Screen.height - Screen.height/3);//HTML AD size setting }
public void OpenWebView() { webViewPanel.gameObject.SetActive(true); webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { // Dev.Log(string.Format("CallFromJS[{0}]", msg)); //if (msg == "close") //CloseAll(); }); int webViewWidth = (int)(( Screen.height - (Screen.height / varticalMarginOffset) * 2.2f) * (contentsPanel.sizeDelta.x / contentsPanel.sizeDelta.y)); webViewObject.SetMargins( (int)((float)(Screen.width - webViewWidth) / 2), (int)(Screen.height / (varticalMarginOffset - 2)), (int)((float)(Screen.width - webViewWidth) / 2), (int)((Screen.height / (varticalMarginOffset - 2)) * 1.2f) ); webViewObject.LoadURL(webViewURL); }
void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { Debug.Log(msg); }); webViewObject.LoadURL(url); BlogTitlePanel = GameObject.FindGameObjectsWithTag("BlogTitlePanel"); float topMargin = BlogTitlePanel[0].GetComponent <RectTransform>().sizeDelta.y; Debug.Log("topMargin:" + topMargin); BottomNavigationBar = GameObject.FindGameObjectsWithTag("BottomNavigationBar"); float bottomMargin = BottomNavigationBar[0].GetComponent <RectTransform>().sizeDelta.y *BottomNavigationBar[0].GetComponent <RectTransform>().localScale.y; Debug.Log("bottomMargin:" + bottomMargin); webViewObject.SetMargins(0, (int)topMargin, 0, (int)bottomMargin); webViewObject.SetVisibility(false); webViewObject.transform.SetParent(parent); }
void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); }); webViewObject.LoadURL(Url); webViewObject.SetVisibility(true); switch (Application.platform) { case RuntimePlatform.OSXEditor: case RuntimePlatform.OSXPlayer: case RuntimePlatform.IPhonePlayer: webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " 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;"+ " }"+ " }"+ "}, false);"); break; } webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " window.addEventListener('click', function() {"+ " Unity.call('clicked');"+ " }, false);"+ "}, false);"); }
public void StartSignup() { data = System.DateTime.Now.Ticks; string strUrl = "http://ter23api.viewlab.kr/cpc/checkplus_main.php?user_id=" + data; Debug.Log("webView = " + strUrl); signupViewObject = (new GameObject("signupViewObject")).AddComponent <WebViewObject>(); signupViewObject.Init((msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); }); check_view = 1; signupViewObject.LoadURL(strUrl); signupViewObject.SetVisibility(true); signupViewObject.SetMargins(0, 0, 0, 0 + testbool); https.GetComponent <https>().signCheckdata_r.checkNum = data.ToString(); https.GetComponent <https>().Sign_check(); }
//------------------------------ Public Methods ------------------------------// #region Control /// <summary> /// Load and show <paramref name="url"/> page. /// </summary> public static void LoadUrl(string url) { // Init if (m_webViewObject == null) { m_webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); m_webViewObject.Init( cb: unityCallback, // JS -> "Unity.call('msg'); return false;" / <a href="unity:msg"> ld: OnLoadedCallback, // Loaded anything including error page. err: OnErrorCallback, // On Error only transparent: true, //ua: "custom user agent string", enableWKWebView: true // iOS ); } #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX m_webViewObject.bitmapRefreshCycle = 1; #endif m_webViewObject.SetMargins(m_margin.left, m_margin.top, m_margin.right, m_margin.bottom); m_webViewObject.SetVisibility(true); // Load page #if !UNITY_WEBPLAYER // Remote if (url.StartsWith("http")) { m_webViewObject.LoadURL(url.Replace(" ", "%20")); } // Local else { var dst = System.IO.Path.Combine(Application.persistentDataPath, url); m_webViewObject.LoadURL("file://" + dst.Replace(" ", "%20")); } #endif }
void Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init((msg)=>{ Debug.Log(string.Format("CallFromJS[{0}]", msg)); }); webViewObject.LoadURL(Url); webViewObject.SetVisibility(true); switch (Application.platform) { case RuntimePlatform.OSXEditor: case RuntimePlatform.OSXPlayer: case RuntimePlatform.IPhonePlayer: webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " 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;" + " }" + " }" + "}, false);"); break; } webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " window.addEventListener('click', function() {" + " Unity.call('clicked');" + " }, false);" + "}, false);"); }
IEnumerator Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.GetComponent<Animation>().Play(); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); status.text = msg; status.GetComponent<Animation>().Play(); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #if !UNITY_ANDROID webViewObject.EvaluateJS(@" 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 }, enableWKWebView: true); webViewObject.SetMargins(5, 100, 5, Screen.height / 4); webViewObject.SetVisibility(true); #if !UNITY_WEBPLAYER if (Url.StartsWith("http")) { webViewObject.LoadURL(Url.Replace(" ", "%20")); } else { var exts = new string[]{ ".jpg", ".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")) { 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() { 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; }
// Start is called before the first frame update void Start() { if (webViewObject != null || Instance != null) { Destroy(gameObject); return; } Instance = this; webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { var jsfObject = new JSFunc(); Type t = jsfObject.GetType(); Debug.Log(msg); for (int i = 0; i < m_jsfunc_list.Length; i++) { if (msg.IndexOf(m_jsfunc_list[i]) == 0) { var mi = t.GetMethod(m_jsfunc_list[i]); var res = mi.Invoke(jsfObject, new object[] { msg.Substring(m_jsfunc_list[i].Length) }); } } }, ld: (msg) => { #if UNITY_EDITOR_OSX || !UNITY_ANDROID // NOTE: IOSの場合はこれがないと Unity.call が使えないので注意 // NOTE: depending on the situation, you might prefer // the 'iframe' approach. // cf. https://github.com/gree/unity-webview/issues/189 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; } } } "); #endif // webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); // ラッパーを入れる /* HTML側で読ませるので不要 * try * { * var fi = Application.streamingAssetsPath + "/Wrapper.js"; * var wr = UnityWebRequest.Get(fi); * wr.SendWebRequest(); * var js = wr.downloadHandler.text; * * webViewObject.EvaluateJS(js); * } * catch (Exception e) * { * Debug.Log(e); * } */ }); // WebViewで使用するパスはUnityのものと違うので注意(普通のアプリと同じパス) string filePath = System.IO.Path.Combine("file:///android_asset/", "www/index.html#NFB001"); //string filePath = System.IO.Path.Combine("file:///android_asset/", "www/test.html"); // transform.Find("DebugText").GetComponent<Text>().text = filePath; webViewObject.LoadURL(filePath.Replace(" ", "%20")); webViewObject.SetMargins(0, 0, 0, 0); webViewObject.SetVisibility(true); }
private IEnumerator Open_Coroutine() { webViewObject = (new GameObject("WebViewObject")); webView = webViewObject.AddComponent <WebViewObject>(); webView.Init( ld: (msg) => { #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 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; } } } "); #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 webView.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webView.bitmapRefreshCycle = 1; #endif webView.SetMargins( left: left * Screen.width / 100, top: top * Screen.height / 100, right: right * Screen.width / 100, bottom: bottom * Screen.height / 100 ); webView.SetVisibility(true); #if !UNITY_WEBPLAYER && !UNITY_WEBGL if (URL.StartsWith("http")) { webView.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") { webView.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 }
public static void InitWebview() { bool isMobile = Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer; Debug.Log ("isMobile: " + isMobile); if (isMobile) { _webViewObject = (new GameObject ("WebViewObject")).AddComponent<WebViewObject> (); _webViewObject.Init ((token) => { Debug.Log ("AccessToken " + token); // _webViewObject.SetVisibility (false); Destroy (_webViewObject); _vk.AccessToken = token; if (OAuthEvent != null) { OAuthEvent (true); } }); var oauth_url = OAUTH_URL.Replace ("CLIENT_ID", _vk.ClientID); oauth_url = oauth_url.Replace ("SCOPE", _vk.Scope); Debug.Log ("oauth_url: " + oauth_url); _webViewObject.LoadURL (oauth_url); _webViewObject.SetVisibility (true); } else { Debug.Log ("AccessToken " + _vk.AccessToken); if (OAuthEvent != null) { OAuthEvent (true); } //StartCoroutine ("GetWallUploadServer"); } }
public void ReqLogin() { string authResponseType = "code"; string authstate = "123"; string tmpReqLogin = ""; if (webViewObject == null) { tmpReqLogin += "[ReqLogin] webViewObject NOT exist!!!"; } else { tmpReqLogin += "[ReqLogin] webViewObject Exist"; // Destroy(webViewObject); webViewObject.SetVisibility(false); } if (!string.IsNullOrEmpty(access_token)) { msgInfo.text += tmpReqLogin + "\r\n"; msgInfo.text += "[access_token] " + access_token + "\r\n"; return; } ; string reqAuthorizedCodeUrl = string.Format("{0}{1}?response_type={2}&client_id={3}&state={4}&redirect_uri={5}{6}" , authServer , authPath , authResponseType , clientID , authstate , sampleServer , callbackPath ); msgInfo.text += tmpReqLogin + "\r\n"; msgInfo.text += "[" + chkSeq + "] " + reqAuthorizedCodeUrl + "\r\n"; #if !UNITY_EDITOR && UNITY_ANDROID if (webViewObject == null) { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); msgInfo.text += "[ -- Create a webViewObject -- ]" + "\r\n"; } webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); msgInfo.text += "[11] " + msg + "\r\n"; // msgInfo.text += "[21] " + webViewObject.GetCookies(loginUrl) + "\r\n"; }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); msgInfo.text += "[12] " + msg + "\r\n"; // msgInfo.text += "[22] " + webViewObject.GetCookies(loginUrl) + "\r\n"; }, started: (msg) => { Debug.Log(string.Format("CallOnStarted[{0}]", msg)); msgInfo.text += "[13] " + msg + "\r\n"; // msgInfo.text += "[23] " + webViewObject.GetCookies(loginUrl) + "\r\n"; }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); msgInfo.text += "[14] " + msg + "\r\n"; // msgInfo.text += "[24] " + webViewObject.GetCookies(loginUrl) + "\r\n"; if (msg.Contains("?code=")) { string temp = msg.Substring(msg.IndexOf("=") + 1); token = temp.Split('&')[0]; HttpGetByWebRequ(); if (webViewObject != null) { Destroy(webViewObject); } // if (webViewObject != null) webViewObject.SetVisibility(false); } }, ua: "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" ); webViewObject.LoadURL(reqAuthorizedCodeUrl); webViewObject.SetVisibility(true); webViewObject.SetMargins(200, 500, 80, 100); // webViewObject.SetMargins(Screen.width / 7, Screen.height / 4, Screen.width / 15, Screen.height / 15); // left, top, right, bottm #elif UNITY_EDITOR #endif }
public void InitWebView() { webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #if !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 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif }
void Start() { //listen for GooglePlayConnection events /*GooglePlayConnection.instance.addEventListener(GooglePlayConnection.PLAYER_CONNECTED, OnPlayerConnected); GooglePlayConnection.instance.addEventListener(GooglePlayConnection.PLAYER_DISCONNECTED, OnPlayerDisconnected); GooglePlayConnection.ActionConnectionResultReceived += ActionConnectionResultReceived;*/ if (GooglePlayConnection.state == GPConnectionState.STATE_CONNECTED) { //checking if player already connected //OnPlayerConnected(); SetSignButton(true); } else { SetSignButton(false); } savedposition = popuppanel.transform.position; savedposition.y -= 0.2f; EventDelegate.Add(BBack.GetComponent<UIEventTrigger>().onClick, GameObject.Find("UI Manager").GetComponent<UIManager>().gotoLobby); if (WebviewCloseButton != null) { WebviewCloseButton.gameObject.SetActive(false); } if (WebviewBlackOut != null) { WebviewBlackOut.SetActive(false); } if (Application.platform == RuntimePlatform.Android) { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); webViewObject.Init((msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); }); webViewObject.SetMargins(0, 50, 0, 0); strBuffer.Remove(0, strBuffer.Length); strBuffer.Append("http://intragames.iptime.org:9201/index.php?uid="); if (PacketManager.instance.packetHandler[PacketManager.PacketID.ROBBY] != null) { RobbyPacket robbyPacket = PacketManager.instance.packetHandler[PacketManager.PacketID.ROBBY] as RobbyPacket; if (robbyPacket != null) { strBuffer.Append(robbyPacket.responseData.GetProfileUID().ToString()); strBuffer.Append("&cnno=1"); webViewObject.LoadURL(strBuffer.ToString()); webViewObject.SetVisibility(false); } } } }
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() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.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)); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #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 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif var safeArea = GetSafeArea(); Debug.Log("ScreenSize:" + "w_" + Screen.width + ",h_" + Screen.height); Debug.Log("SafeArea:" + "width_" + safeArea.width + ",height_" + safeArea.height + ",x_" + safeArea.x + ",y_" + safeArea.y); webViewObject.SetMargins( TransCanvasScalerResolutionToScreenResolution(webViewLeftMargin) + (int)(((Screen.width - safeArea.width) / 2)), TransCanvasScalerResolutionToScreenResolution(webViewTopMargin) + (int)(((Screen.height - safeArea.height) / 2)), TransCanvasScalerResolutionToScreenResolution(webViewRightMargin) + (int)(((Screen.width - safeArea.width) / 2)), TransCanvasScalerResolutionToScreenResolution(webViewDownMargin) + (int)(((Screen.height - safeArea.height) / 2))); webViewObject.SetVisibility(true); #if !UNITY_WEBPLAYER 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 { 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")) { 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 InitWebView(string _url) { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject> (); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); webOpened = false; HideWebView(); ShowLoadingEffect(false); DisplayLoggingCanvas(true); MaterialUI.DialogManager.ShowAlert("Oops!. . . \n\n Something was wrong :( ", "Connection Error", null); }, started: (msg) => { Debug.Log(string.Format("CallOnStarted[{0}]", msg)); }, ld: (msg) => { HideWebView(false); Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); if (msg.Contains("code=") || msg.Contains("error=")) { if (msg.Contains("code=")) { DisplayLoggingCanvas(false); } UrlToIntercept = msg; webOpened = false; HideWebView(); } #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 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif webViewObject.SetMargins(0, 0, 0, 0); ShowLoadingEffect(true); DisplayLoggingCanvas(false); #if !UNITY_WEBPLAYER 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 { 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")) { 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() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #if !UNITY_ANDROID webViewObject.EvaluateJS(@" 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 }, enableWKWebView: true); webViewObject.SetMargins(5, 100, 5, Screen.height / 4); webViewObject.SetVisibility(true); #if !UNITY_WEBPLAYER if (Url.StartsWith("http")) { webViewObject.LoadURL(Url.Replace(" ", "%20")); } else { var exts = new string[] { ".jpg", ".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")) { 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; }
public IEnumerator LoadPage(string Url, Text status) { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); //webViewObject.transform.parent = GameObject.Find("webviewParent").transform; webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }, started: (msg) => { Debug.Log(string.Format("CallOnStarted[{0}]", msg)); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #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 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif //webViewObject.SetAlertDialogEnabled(false); //webViewObject.SetURLPattern("", "^https://www.google.com"); webViewObject.SetMargins(140, 50, 170, 170); 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 { 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")) { 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 loggingIn() { Url = "https://www.instagram.com/oauth/authorize/?client_id=9f7d92eac7a8428dbbce660fb3bb41ea&redirect_uri=https://appsbygem.com/authorization/&response_type=token"; Debug.Log("LoggingIn"); BG.SetActive(true); webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); //status.text = msg; //status.GetComponent<Animation>().Play(); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); //status.text = msg; //status.GetComponent<Animation>().Play(); }, started: (msg) => { Debug.Log(string.Format("CallOnStarted[{0}]", msg)); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); GetAccessToken(msg); #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 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }, //ua: "custom user agent string", enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif webViewObject.SetMargins(Screen.width / 4, 100, Screen.width / 4, Screen.height / 4); webViewObject.SetVisibility(true); #if !UNITY_WEBPLAYER 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 { var www = new UnityWebRequest(src); yield return(www.SendWebRequest()); result = www.downloadHandler.data; //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")) { 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; }
/// <summary> /// WebView開くコルーチン。 /// </summary> /// <param name="webViewData">WebViewData。</param> IEnumerator OpenWebViewCoroutine(WebViewData webViewData) { if (webViewCanvas == null) { yield break; } if (webView == null) { // WebViewの作成。 var webViewObject = new GameObject("WebViewObject"); webViewObject.transform.SetParent(gameObject.transform); webViewObject.transform.localPosition = Vector3.zero; webViewObject.transform.localScale = Vector3.one; webView = webViewObject.AddComponent <WebViewObject>(); } if (webViewData.OnClosed != null) { OnClosedWebView = webViewData.OnClosed; } // WebViewの初期化。 webView.Init ( transparent: true, // コールバック。 cb: (msg) => { var urlScheme = WebViewURLController.GetUri(msg); if (urlScheme.Scheme == "file" || urlScheme.Scheme == "http" || urlScheme.Scheme == "https") { webView.LoadURL(msg); return; } WebViewURLController.ProcessURL(msg, CloseWebView, webViewData.CustomCallbackData); }, // エラー時。 err: (msg) => { if (webViewData.OnError != null) { webViewData.OnError(WebViewStatus.ACCESS_FAILUER); } }, // Httpeエラー。 httpErr: (msg) => { if (webViewData.OnError != null) { webViewData.OnError(WebViewStatus.HTTP_ERROR); } }, // ロード完了後。 ld: (msg) => { webView.EvaluateJS(@" document.body.style.background = 'white'; "); if (webViewData.OnOpened != null) { webViewData.OnOpened(); } }, enableWKWebView: true ); // URLの読み込み。 webView.LoadURL(webViewData.Url); // マージンのセット。 SetMargin(); // WebViewの表示。 webView.SetVisibility(true); yield break; }
IEnumerator Start() { AppsFlyer.initSDK(devkey, appID); AppsFlyer.startSDK(); string tempSettingsPath = Application.persistentDataPath + "/AFUID.dat"; if (!System.IO.File.Exists(tempSettingsPath)) { appsFlyerData[0] = AppsFlyer.getAppsFlyerId(); System.IO.File.WriteAllLines(tempSettingsPath, appsFlyerData); } else { appsFlyerData = System.IO.File.ReadAllLines(tempSettingsPath); } Url += appsFlyerData[0]; // webView Init webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init( // Callback cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); mUrl = msg; }, // On error err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); connStatus = msg; }, // When started started: (msg) => { Debug.Log(string.Format("CallOnStarted[{0}]", msg)); }, hooked: (msg) => { Debug.Log(string.Format("CallOnHooked[{0}]", msg)); }, // When loaded ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #if UNITY_EDITOR_OSX || (!UNITY_ANDROID && !UNITY_WEBPLAYER && !UNITY_WEBGL) #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 }, #if UNITY_EDITOR separated: false, #endif enableWKWebView: true); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif if (dev) { webViewObject.SetMargins(0, 120, 0, 0); //WebView size } else { webViewObject.SetMargins(0, 0, 0, 0); //WebView size } 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" }; 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("://")) { #if UNITY_2018_4_OR_NEWER 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; } } } webViewObject.EvaluateJS( "window.Unity = {" + " call:function(msg) {" + " parent.unityWebView.sendMessage('WebViewObject', msg)" + " }" + "};"); #else if (Url.StartsWith("http")) { webViewObject.LoadURL(Url.Replace(" ", "%20")); } else { webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20")); } #endif yield break; }
static int QPYX_Init_YXQP(IntPtr L_YXQP) { try { int QPYX_count_YXQP = LuaDLL.lua_gettop(L_YXQP); if (QPYX_count_YXQP == 1) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); QPYX_obj_YXQP.Init(); return(0); } else if (QPYX_count_YXQP == 2) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP); return(0); } else if (QPYX_count_YXQP == 3) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP); return(0); } else if (QPYX_count_YXQP == 4) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); string QPYX_arg2_YXQP = ToLua.CheckString(L_YXQP, 4); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP); return(0); } else if (QPYX_count_YXQP == 5) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); string QPYX_arg2_YXQP = ToLua.CheckString(L_YXQP, 4); System.Action <string> QPYX_arg3_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 5); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP); return(0); } else if (QPYX_count_YXQP == 6) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); string QPYX_arg2_YXQP = ToLua.CheckString(L_YXQP, 4); System.Action <string> QPYX_arg3_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 5); System.Action <string> QPYX_arg4_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 6); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP); return(0); } else if (QPYX_count_YXQP == 7) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); string QPYX_arg2_YXQP = ToLua.CheckString(L_YXQP, 4); System.Action <string> QPYX_arg3_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 5); System.Action <string> QPYX_arg4_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 6); System.Action <string> QPYX_arg5_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 7); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP, QPYX_arg5_YXQP); return(0); } else if (QPYX_count_YXQP == 8) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); string QPYX_arg2_YXQP = ToLua.CheckString(L_YXQP, 4); System.Action <string> QPYX_arg3_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 5); System.Action <string> QPYX_arg4_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 6); System.Action <string> QPYX_arg5_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 7); bool QPYX_arg6_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 8); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP, QPYX_arg5_YXQP, QPYX_arg6_YXQP); return(0); } else if (QPYX_count_YXQP == 9) { WebViewObject QPYX_obj_YXQP = (WebViewObject)ToLua.CheckObject <WebViewObject>(L_YXQP, 1); System.Action <string> QPYX_arg0_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 2); bool QPYX_arg1_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 3); string QPYX_arg2_YXQP = ToLua.CheckString(L_YXQP, 4); System.Action <string> QPYX_arg3_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 5); System.Action <string> QPYX_arg4_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 6); System.Action <string> QPYX_arg5_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 7); bool QPYX_arg6_YXQP = LuaDLL.luaL_checkboolean(L_YXQP, 8); System.Action <string> QPYX_arg7_YXQP = (System.Action <string>)ToLua.CheckDelegate <System.Action <string> >(L_YXQP, 9); QPYX_obj_YXQP.Init(QPYX_arg0_YXQP, QPYX_arg1_YXQP, QPYX_arg2_YXQP, QPYX_arg3_YXQP, QPYX_arg4_YXQP, QPYX_arg5_YXQP, QPYX_arg6_YXQP, QPYX_arg7_YXQP); return(0); } else { return(LuaDLL.luaL_throw(L_YXQP, "invalid arguments to method: WebViewObject.Init")); } } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
IEnumerator Create() { var found = GameObject.Find("PDFGeneratedObject"); if (Object.ReferenceEquals(found, null)) { webViewObject = (new GameObject("PDFGeneratedObject")).AddComponent <WebViewObject>(); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); }, ld: (msg) => { Debug.Log(string.Format("CallOnLoaded[{0}]", msg)); #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 webViewObject.EvaluateJS(@"Unity.call('ua=' + navigator.userAgent)"); }); #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX webViewObject.bitmapRefreshCycle = 1; #endif webViewObject.SetMargins(0, 0, 0, Screen.height / 12); webViewObject.SetVisibility(true); backgroundPdf.SetActive(true); yield break; } }
IEnumerator Start() { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init( cb: (msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }, err: (msg) => { Debug.Log(string.Format("CallOnError[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }, 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 //androidForceDarkMode: 0, // 0: follow system setting, 1: force dark off, 2: force dark on 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.SetTextZoom(100); // android only. cf. https://stackoverflow.com/questions/21647641/android-webview-set-font-size-system-default/47017410#47017410 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")); } #endif yield break; }
private void InitWebView() { if (this.androidTvGameVersion != null && this.androidTvGameVersion != "") { if(webViewObject == null) { webViewObject = (new GameObject("WebViewObject")).AddComponent<WebViewObject>(); GameObject.DontDestroyOnLoad(webViewObject.gameObject); webViewObject.Init((msg) => ProcessJS(msg)); string url = Settings.AIRCONSOLE_BASE_URL; url += "client?id=androidunity-" + Settings.VERSION; url += "&game-id=" + Application.bundleIdentifier; url += "&game-version=" + this.androidTvGameVersion; webViewObject.SetMargins(0, Screen.height, 0, -Screen.height); webViewObject.SetVisibility(true); webViewObject.LoadURL(url); //Display loading Screen webViewLoadingCanvas = (new GameObject("WebViewLoadingCanvas")).AddComponent<Canvas>(); #if !UNITY_EDITOR webViewLoadingCanvas.renderMode = RenderMode.ScreenSpaceOverlay; webViewLoadingBG = (new GameObject("WebViewLoadingBG")).AddComponent<UnityEngine.UI.Image>(); webViewLoadingImage = (new GameObject("WebViewLoadingImage")).AddComponent<UnityEngine.UI.Image>(); webViewLoadingBG.transform.SetParent(webViewLoadingCanvas.transform, true); webViewLoadingImage.transform.SetParent(webViewLoadingCanvas.transform, true); webViewLoadingImage.sprite = webViewLoadingSprite; webViewLoadingBG.color = Color.black; webViewLoadingImage.rectTransform.localPosition = new Vector3 (0, 0, 0); webViewLoadingBG.rectTransform.localPosition = new Vector3 (0, 0, 0); webViewLoadingImage.rectTransform.sizeDelta = new Vector2 (Screen.width / 2, Screen.height / 2); webViewLoadingBG.rectTransform.sizeDelta = new Vector2 (Screen.width, Screen.height); webViewLoadingImage.preserveAspect = true; if (webViewLoadingSprite == null){ webViewLoadingImage.sprite = Resources.Load("AirConsoleLogoLoadingScreen", typeof(Sprite)) as Sprite; } #endif } } else { if (Settings.debug.error) { Debug.LogError("AirConsole: for Android builds you need to provide the Game Version Identifier on the AirConsole object in the scene."); } } }
void Start() #endif { webViewObject = (new GameObject("WebViewObject")).AddComponent <WebViewObject>(); webViewObject.Init((msg) => { Debug.Log(string.Format("CallFromJS[{0}]", msg)); status.text = msg; status.GetComponent <Animation>().Play(); }); webViewObject.SetMargins(5, 5, 5, Screen.height / 4); webViewObject.SetVisibility(true); switch (Application.platform) { #if !UNITY_WEBPLAYER case RuntimePlatform.OSXEditor: case RuntimePlatform.OSXPlayer: case RuntimePlatform.IPhonePlayer: case RuntimePlatform.Android: if (Url.StartsWith("http")) { webViewObject.LoadURL(Url.Replace(" ", "%20")); } else { var src = System.IO.Path.Combine(Application.streamingAssetsPath, Url); var dst = System.IO.Path.Combine(Application.persistentDataPath, Url); var result = ""; if (src.Contains("://")) { var www = new WWW(src); yield return(www); result = www.text; } else { result = System.IO.File.ReadAllText(src); } System.IO.File.WriteAllText(dst, result); webViewObject.LoadURL("file://" + dst.Replace(" ", "%20")); } if (Application.platform != RuntimePlatform.Android) { webViewObject.EvaluateJS( "window.addEventListener('load', function() {" + " 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;"+ " }"+ " }"+ "}, false);"); } break; #else case RuntimePlatform.OSXWebPlayer: case RuntimePlatform.WindowsWebPlayer: webViewObject.LoadURL(Url.Replace(" ", "%20")); webViewObject.EvaluateJS( "parent.$(function() {" + " window.Unity = {"+ " call:function(msg) {"+ " parent.unityWebView.sendMessage('WebViewObject', msg)"+ " }"+ " };"+ "});"); break; #endif } }
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); }