【问题标题】:How can I go back previous page in Unity WebKit如何在 Unity WebKit 中返回上一页
【发布时间】:2019-01-07 15:10:40
【问题描述】:

我统一编写了一个报纸应用程序,我正在使用 webKit 来显示报纸。但是,当它按下电话按钮时,我需要返回上一页。有谁知道返回上一页的C#代码...

我写的;

if(Input.GetKeyButton(KeyCode.Escape)){

    Applicaition.Quit(); // However it quits from the app. Not going back previous page...

}

谢谢

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    欢迎来到 Stack Overflow。据我所知,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);
    }
    

    【讨论】:

      【解决方案2】:
      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 脚本......所以当我按下电话按钮返回报纸上的上一页时我需要代码......

      【讨论】:

      • 欢迎来到 Stack Overflow,请考虑将其添加为对您问题的编辑,因为这不会尝试回答问题。
      • 是的,您可以编辑问题以添加更多详细信息,将其写为答案或评论可能会隐藏它并可能导致混淆。
      猜你喜欢
      • 2016-05-28
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      相关资源
      最近更新 更多