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

相关文章

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

禁止HTML页面滚动的操作方法

《禁止HTML页面滚动的操作方法》:本文主要介绍了三种禁止HTML页面滚动的方法:通过CSS的overflow属性、使用JavaScript的滚动事件监听器以及使用CSS的position:fixed属性,每种方法都有其适用场景和优缺点,详细内容请阅读本文,希望能对你有所帮助... 在前端开发中,禁止htm

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

macOS怎么轻松更换App图标? Mac电脑图标更换指南

《macOS怎么轻松更换App图标?Mac电脑图标更换指南》想要给你的Mac电脑按照自己的喜好来更换App图标?其实非常简单,只需要两步就能搞定,下面我来详细讲解一下... 虽然 MACOS 的个性化定制选项已经「缩水」,不如早期版本那么丰富,www.chinasem.cn但我们仍然可以按照自己的喜好来更换

使用JavaScript将PDF页面中的标注扁平化的操作指南

《使用JavaScript将PDF页面中的标注扁平化的操作指南》扁平化(flatten)操作可以将标注作为矢量图形包含在PDF页面的内容中,使其不可编辑,DynamsoftDocumentViewer... 目录使用Dynamsoft Document Viewer打开一个PDF文件并启用标注添加功能扁平化