unity 内嵌h5页面跳转原生app

2024-08-21 18:38

本文主要是介绍unity 内嵌h5页面跳转原生app,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

unity 内嵌H5界面需要用到Uniwebview 差件,百度搜索下就可以找到插件下载地址,这里就不提供了

Uniwebview 支持Android 、IOS。  PC平台下不显示,需要打包测试。

下面是封装好的动态加载外部网页类

using System;
using UnityEngine;public class WebView : Singleton<WebView> {public delegate void WebOnReceivedMessage(UniWebView webView, UniWebViewMessage message);public WebOnReceivedMessage OnWebReceivedMessage;#if !(UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN) private UniWebView webView;
#endifprivate Action onClose;private void CreateWebView(Rect panelPosition,Action loadFinished = null) {
#if !(UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN)if (this.webView == null) {this.webView = this.gameObject.AddComponent<UniWebView>();}          int top, left, bottom, right;
#if UNITY_ANDROIDleft = (int)panelPosition.x;top = (int)panelPosition.y;bottom = (int)(Screen.height - panelPosition.height - top);right = (int)(Screen.width - panelPosition.width - left) ;
#endif#if UNITY_IOSfloat ratio;ratio = 1f / UniWebViewHelper.screenScale;left = (int)(panelPosition.x * ratio);top = (int)(panelPosition.y* ratio);bottom = UniWebViewHelper.screenHeight - (int)((panelPosition.y+panelPosition.height)*ratio);right = UniWebViewHelper.screenWidth - (int)((panelPosition.x + panelPosition.width) * ratio);         
#endifthis.webView.insets = new UniWebViewEdgeInsets(top,left,bottom,right);Color bgColor = Color.white;bgColor.a = 0;this.webView.SetBackgroundColor(bgColor);this.webView.bouncesEnable = false;this.webView.SetShowSpinnerWhenLoading(true);if (loadFinished!=null) {this.webView.OnLoadComplete += (UniWebView webView, bool success, string errorMessage) => {this.webView.AddUrlScheme("http");this.webView.AddUrlScheme("https");loadFinished();};}this.webView.OnWebViewShouldClose += (view) => {return false;};this.webView.OnReceivedMessage += (webView, message) => {OnWebReceivedMessage(webView, message);UniWebviewTest.Instance.text.text = message.rawMessage;Application.OpenURL(message.rawMessage);};
#endif}public void Show(string url, Rect panelPosition, Action loadFinish = null,bool transition = false, Action onclosed = null) {#if UNITY_EDITOR || UNITY_STANDALONE_WINif (loadFinish != null) {loadFinish();}return;
#endif#if !(UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN) CreateWebView(panelPosition,loadFinish);this.webView.Load(url);this.webView.Show(true, transition ? UniWebViewTransitionEdge.Top : UniWebViewTransitionEdge.None, 0.3f);this.onClose = onclosed;  
#endif}public void Hide() {
#if !(UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN) if (this.webView == null) {return;}this.webView.Hide(true, UniWebViewTransitionEdge.None, 0.2f, () => {CloseWebView();});
#endif}private void CloseWebView() {
#if !(UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN) if (this.webView == null) {return;}this.webView.CleanCache();DestroyImmediate(this.webView);this.webView = null;this.onClose.Invoke();this.onClose = null;
#endif}}

Singleton单例

using UnityEngine;public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour {private static T instance;// ReSharper disable once StaticMemberInGenericTypeprivate static readonly object Locker = new object();// ReSharper disable once StaticMemberInGenericTypeprivate static bool applicationIsQuitting;public static T Instance {get {if (Singleton<T>.applicationIsQuitting) {return null;}lock (Singleton<T>.Locker) {if (Singleton<T>.instance == null) {Singleton<T>.instance = Object.FindObjectOfType(typeof(T)) as T;if (Object.FindObjectsOfType(typeof(T)).Length > 1) {return Singleton<T>.instance;}if (Singleton<T>.instance == null) {GameObject singleton = new GameObject();Singleton<T>.instance = singleton.AddComponent<T>();singleton.name = "(singleton) " + typeof(T);Object.DontDestroyOnLoad(singleton);} else {}}return Singleton<T>.instance;}}}protected virtual void Awake() {Singleton<T>.applicationIsQuitting = false;}protected virtual void OnDestroy() {}private void OnApplicationQuit() {Singleton<T>.applicationIsQuitting = true;}
}

测试类 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;public class UniWebviewTest : MonoBehaviour {private WebView webView;public UnityEngine.UI.Text text;public static UniWebviewTest Instance;private string session_token = "45dbc533f2f47d9e693b621f0b2361a6";private string id = "10000";private void Awake() {Instance = this;}// Use this for initializationvoid Start () {Rect rect = new UnityEngine.Rect(this.GetComponent<RectTransform>().position, this.GetComponent<RectTransform>().rect.size);this.webView = Singleton<WebView>.Instance;this.webView.OnWebReceivedMessage += ReceivedMessage;this.webView.Show("http://192.168.18.57/pay.html",rect,() => { Debug.Log("H5"); });}private void ReceivedMessage(UniWebView webView, UniWebViewMessage message) {text.text = webView.url + ":" + message.path ;}}

 

具体解析:

 

webView.AddUrlScheme()

是用来添加拦截链接  ://  前的部分,比如http://或者https://

如果h5里的按钮点击是访问上面两种开头的链接,则会被拦截,不进行访问

这时webView.OnReceivedMessage这个事件将会接收到拦截的url信息,也就是上面的message.rawMessage里

再通过Appliction.OpenUrl() 就可以在浏览器中打开想要访问的网站(PC,Android,IOS平台都可访问,亲测可用)

 

附:

Uniwebview 不能拦截动态获取的含有form表单的链接(与服务器交互获得的url 而且存在需要提交的表单),测试时是拦截不到,也可能是我了解不够的问题,你们如果能够测试通过,希望你们能给我留言,将不胜感激

 

这篇关于unity 内嵌h5页面跳转原生app的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1093961

相关文章

go rate 原生标准限速库的使用

《gorate原生标准限速库的使用》本文主要介绍了Go标准库golang.org/x/time/rate实现限流,采用令牌桶算法控制请求速率,提供Allow/Reserve/Wait方法,具有一定... 目录介绍安装API介绍rate.NewLimiter:创建限流器limiter.Allow():请求是否

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Flutter监听当前页面可见与隐藏状态的代码详解

《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

MySQL表锁、页面锁和行锁的作用及其优缺点对比分析

《MySQL表锁、页面锁和行锁的作用及其优缺点对比分析》MySQL中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock