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里面加个方法

    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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,(代码有点长,大家可以直接去上一个链接那里复制就行)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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底部留白)

 
 
 

posted @   Domefy  阅读(406)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示