亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Unity WebKit 中返回上一頁

如何在 Unity WebKit 中返回上一頁

C#
萬千封印 2022-07-10 16:15:12
我統一編寫了一個報紙應用程序,我正在使用 webKit 來顯示報紙。但是,當它按下電話按鈕時,我需要返回上一頁。有誰知道返回上一頁的 C# 代碼...我寫;if(Input.GetKeyButton(KeyCode.Escape)){    Applicaition.Quit(); // However it quits from the app. Not going back previous page...}謝謝
查看完整描述

2 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

using System.Collections;

using UnityEngine;

using System;

using System.Collections.Generic;



public class SampleWebView : MonoBehaviour

{


    public string Url;

    public GUIText status;

    WebViewObject webViewObject;



    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));

            },

            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.SetMargins(10, 140, 10, Screen.height / 360);

        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;

    }


#if !UNITY_WEBPLAYER

    //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());

    //}

#endif








}

這是我正在使用的 webkit 腳本......所以當我按下電話按鈕返回報紙上的上一頁時我需要代碼......


查看完整回答
反對 回復 2022-07-10
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

歡迎來到堆棧溢出。據我所知,Unity 不支持任何官方的 WebKit。因此,如果您提供代碼的更多詳細信息,將會很有幫助。


對于您的問題,我認為您可以自己保留一個 url 堆棧來處理頁面控件。


Stack<string> openedPages = new Stack<string>();


// When user open another page, put current url into stack.

OnOpenPage()

{

    openedPages.Push(_current_page_url);

}


// When user push the previous page button, pop the last opened page url

OnPreviousPageButton()

{

    string lastUrl = openedPages.Pop();

    _yourBrowser.Open(lastUrl);

}



查看完整回答
反對 回復 2022-07-10
  • 2 回答
  • 0 關注
  • 105 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號