Unity中内嵌网页插件 UniWebView,缩放网页

https://mp.weixin.qq.com/s/cesU-Jlxf_a_lGxIv6q_yQ

转载开发者俱乐部并且修改了

https://docs.uniwebview.com/api/

插件文档,可使用百度翻译查看

    

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestWebView : MonoBehaviour
{

    private UniWebView uniWebView;

    public RectTransform viewImage;



    public UniWebView GetUniWebView

    {

        get

        {

            if (uniWebView == null)

            {

                uniWebView = GameObject.FindObjectOfType();

                if (uniWebView == null)

                {

                    GameObject webViewGameObject = GameObject.Find("UniWebView");

                    if (webViewGameObject == null)

                        webViewGameObject = new GameObject("UniWebView");

                    uniWebView = webViewGameObject.AddComponent();

                    uniWebView.ReferenceRectTransform = viewImage;


                    //设置Web视图是否支持缩放权限以更改内容大小。默认不支持缩放
                    uniWebView.SetZoomEnabled(true);
                     
 //设置Web视图是否支持缩放权限以更改内容大小。默认不支持缩放
                    uniWebView.SetZoomEnabled(true);

                    //允许用户调整Web视图窗口的大小。只在苹果电脑上支持不支持手机
                    //uniWebView.SetWindowUserResizeEnabled(true);

                    //设置是否显示包含导航按钮和完成按钮的工具栏。
                    //仅仅是ios端的
                    uniWebView.SetShowToolbar(true,true,true,true);

                    //设置Web视图是否应在沉浸式模式下运行,即使用粘性样式隐藏状态栏和导航栏。默认是启用的
                    //仅仅是安卓端的
                    uniWebView.SetImmersiveModeEnabled(true);

                    //设置Web视图是否以概览模式加载页面,即缩小内容以适应屏幕宽度。
                    //默认是false,仅仅是安卓端的
                    uniWebView.SetLoadWithOverviewMode(true);

                    //设置滚动到页面边缘时Web视图是否应显示跳出效果。
                    uniWebView.SetBouncesEnabled(true);

                    //设置当Web内容超出Web视图边界时是否应显示垂直滚动条。
                    uniWebView.SetVerticalScrollBarEnabled(true);

                    //设置当Web内容超出Web视图边界时是否应显示水平滚动条。
                    uniWebView.SetHorizontalScrollBarEnabled(true);

                    //设置是否在加载过程中显示加载指示器。
                    uniWebView.SetShowSpinnerWhileLoading(true);

                    //设置在Web视图中单击的链接是否应在外部浏览器中打开页面。
                    //重点关注
                    uniWebView.SetOpenLinksInExternalBrowser(false);

                }

                uniWebView.OnMessageReceived += OnMessageReceived;

                uniWebView.OnPageStarted += OnPageStarted;

                uniWebView.OnPageFinished += OnPageFinished;

                uniWebView.OnKeyCodeReceived += OnKeyCodeReceived;

                uniWebView.OnPageErrorReceived += OnPageErrorReceived;



                uniWebView.OnShouldClose += OnShouldClose;



                uniWebView.SetBackButtonEnabled(false);// 回退钮  物理按键



            }

            return uniWebView;

        }

    }





    private void OnPageErrorReceived(UniWebView webView, int errorCode, string errorMessage)

    {

        Debug.Log("OnPageErrorReceived :" + string.Format("errorCode:{0},errorMessage{1}", errorCode, errorMessage));

    }



    private void OnKeyCodeReceived(UniWebView webView, int keyCode)

    {

        Debug.Log("OnKeyCodeReceived keycode:" + keyCode);

    }



    private void OnPageFinished(UniWebView webView, int statusCode, string url)

    {

        Debug.Log("OnPageFinished statusCode:" + string.Format("statusCode:{0},url{1}", statusCode, url));

    }



    private void OnPageStarted(UniWebView webView, string url)

    {



        Debug.Log("OnPageStarted " + url);

    }



    private void OnMessageReceived(UniWebView webView, UniWebViewMessage message)

    {

        Debug.Log("OnMessageReceived :" + message.RawMessage);



        if (message.Path.Equals("game"))

        {

            var score = message.Args["score"];

            var name = message.Args["name"];

            Debug.Log("Your final score is: " + score + "name :" + name);



            if (GetUniWebView.isActiveAndEnabled)

            {

                string content = string.Format("openParamOne({0});", int.Parse(score) * 2 + int.Parse(name) * 1);

                GetUniWebView.EvaluateJavaScript(content, (payload) => {

                    if (payload.resultCode.Equals("0"))

                    {

                        Debug.Log("Game Started!=========>");

                    }

                    else

                    {

                        Debug.Log("Something goes wrong: " + payload.data);

                    }

                });

            }

        }

    }



    private bool OnShouldClose(UniWebView webView)

    {

        webView.CleanCache();

        webView = null;

        return true;

    }



    private string url = "http://www.baidu.com";



    private void OnGUI()

    {

        url = GUILayout.TextField(url, GUILayout.Width(250), GUILayout.Height(80));


        if (GUILayout.Button("Load", GUILayout.Height(80)))

        {

            OnLoaded();

        }

        //按钮一  刷新网页
        if (GUILayout.Button("ReLoad", GUILayout.Height(80)))

        {

            OnReLoaded();

        }


        //按钮二  
        if (GUILayout.Button("Close", GUILayout.Height(80)))

        {

            OnClose();

        }



        if (GUILayout.Button("Call JS", GUILayout.Height(80)))

        {

            OnCallJavaScript();

        }



        if (GUILayout.Button("GoBack", GUILayout.Height(80)))

        {

            if (GetUniWebView.CanGoBack)

            {

                GetUniWebView.GoBack();

            }



        }

        if (GUILayout.Button("GoForward", GUILayout.Height(80)))

        {

            if (GetUniWebView.CanGoForward)

            {

                GetUniWebView.GoForward();

            }



        }

    }



    private void OnLoaded()

    {



        GetUniWebView.Load(url);

        GetUniWebView.Show();

    }



    private void OnReLoaded()

    {

        if (GetUniWebView.isActiveAndEnabled)

        {

            GetUniWebView.Reload();

        }

    }





    private void OnClose()

    {

        GetUniWebView.Hide();
         GetUniWebView.CleanCache();//清理Web视图缓存。

        Destroy(GetUniWebView.gameObject);

    }



    private void OnCallJavaScript()
    {
        if (GetUniWebView.isActiveAndEnabled)

        {

            GetUniWebView.EvaluateJavaScript("openParam();", (payload) => {

                if (payload.resultCode.Equals("0"))

                {

                    Debug.Log("Game Started!");

                }

                else

                {

                    Debug.Log("Something goes wrong: " + payload.data);

                }

            });

        }



    }

}

 

你可能感兴趣的:(Unity中内嵌网页插件 UniWebView,缩放网页)