uniwebView插件与GUI的适配问题

2021/8/9 6:08:08

本文主要是介绍uniwebView插件与GUI的适配问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 

这次突然被要求实现内嵌浏览器。在网上到处找资料参考,下面几个链接的内容感觉很实用

https://blog.csdn.net/qq_37310110/article/details/79761844

https://blog.csdn.net/shen71702163/article/details/79283645

https://docs.uniwebview.com/guide/position-and-size.html#setting-frame

分享一下这次心得吧,以下是个人感受,技术不太好,说错了敬请纠正。

1.首先找插件,小编就找到了个UniWebView2.9,附上下载链接如下,https://pan.baidu.com/s/1HPvFzKU7WNHSvxHrKtX8zg   提取码:4g2i

2.导入之后就是这样,还有个参考Demo

   

3.参考网上教程:(https://blog.csdn.net/qq_37310110/article/details/79761844#commentsedit)

在UniWebViewHelper里面加个方法

    

public static UniWebView CreateUniWebView(GameObject go, string url, float top, float left, float bottom, float right)
{

        if (go == null || !go.activeSelf)

        {

            return null;

        }

        var view = go.GetComponent<UniWebView>();

        if (view == null)

        {

            view = go.AddComponent<UniWebView>();

        }
 view.insets = new UniWebViewEdgeInsets(UniWebViewHelper.ConvertPixelToPoint(top, false), UniWebViewHelper.ConvertPixelToPoint(left, true), UniWebViewHelper.ConvertPixelToPoint(bottom, false), UniWebViewHelper.ConvertPixelToPoint(right, true));

        view.SetShowSpinnerWhenLoading(true);

        view.immersiveMode = false;

        view.url = url;

        return view;

    }

  

   

3.后面发现报红线了

   

发现这个方法没有,网上那个教程也没说这个方法,只能自立更生思考怎么写了,发现这个是界面显示方法,参考官方文档不知道怎么写好,只能换种思路,继续查“UniWebView案例”。

4.终于找到了一篇“unity 网页 基于 UniWebView 做UGUI 适配”https://blog.csdn.net/qq_37310110/article/details/79761844#commentsedit

private static int ConvertPixelToPoint(float num, bool v)

    {

#if UNITY_IOS && !UNITY_EDITOR

        float scale = 0;

        if(v)

        {

            scale = 1f * screenWidth / Screen.width;

        }

        else

        {

            scale = 1f * screenHeight / Screen.height;

        }

        return (int)(num*scale);

#endif

        return (int)num;

    }

  

   

5.创建一个新脚本OpenURL,(代码有点长,大家可以直接去上一个链接那里复制就行)

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class OpenURL : MonoBehaviour

{

    public InputField _url;

    public Button _enterBtn;

    public Button _backBtn;

    UniWebView _view;

    private void Awake()

    {

        _enterBtn.onClick.AddListener(OpenUrl);

        _backBtn.onClick.AddListener(CloseUrl);

        _backBtn.gameObject.SetActive(false);

    }

    public void OpenUrl()

    {

        if (_view != null)

        {

            _view.CleanCache();

        }

        if (_url.text == null)

        {

            return;

        }

        _view = UniWebViewHelper.CreateUniWebView(gameObject, "https://" + _url.text, 100, 0, 50, 0);

        _view.OnLoadComplete += View_OnLoadComplete;

        _view.Load();

    }

    private void View_OnLoadComplete(UniWebView webView, bool success, string errorMessage)

    {

        if (success)

        {

            //  显示 加载完成的界面 

            webView.Show();

            _backBtn.gameObject.SetActive(true);

        }

        else

        {

            //  输出 错误码 

            Debug.LogError("Something wrong in webview loading: " + errorMessage);

        }

    }

    public void CloseUrl()

    {

        _view.Hide();

        _view.OnLoadComplete -= View_OnLoadComplete;

        Destroy(_view);

    }

}

  

6.创建两个按钮和一个输入框,在canvas下挂OpenURL和UniWebView这两个脚本,记得给OpenURL挂按钮,那个UniWebView参数直接是在代码改变的,所以不用设置。

       

7.这样就成功了,不过注意如图下是因为在OpenURL脚本里的OpenUrl方法本来加了http://,可以选择去掉或者输入不要http://。

       

8.效果如下(之前的top=100是为了空白可以显示上面的返回按钮,bottom=50底部留白)

       


这篇关于uniwebView插件与GUI的适配问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程