【Vuforia+Unity】AR判断当前平台获取点击/触摸坐标点选中识别的二维码跳转网页

实现了:【Vuforia+Unity】判断当前平台获取点击/触摸坐标点选中识别的二维码跳转网页

using UnityEngine;

using Vuforia;

public class BarcodeScanner : MonoBehaviour

{

public TMPro.TextMeshProUGUI barcodeAsText;

string platformNum="";

private void Start()

{

switch (Application.platform)

{

case RuntimePlatform.WindowsEditor:

Debug.Log("用户使用的是 WindowsEditor");

platformNum = "WindowsEditor";

break;

case RuntimePlatform.OSXEditor:

Debug.Log("用户使用的是 OSXEditor");

platformNum = "OSXEditor";

break;

case RuntimePlatform.LinuxEditor:

Debug.Log("用户使用的是 LinuxEditor");

platformNum = "LinuxEditor";

break;

case RuntimePlatform.Android:

platformNum = "Android";

Debug.Log("用户使用的是 Android device");

break;

// 可以继续添加其他平台的判断,如 iOS, Windows, macOS 等

default:

Debug.Log("Running on an unspecified platform");

break;

}

}

void Update()

{

if (platformNum== "Android")//判断平台

{

if (Input.touchCount > 0) // 如果有触摸事件

{

Touch touch = Input.GetTouch(0); // 获取第一个点击触摸事件

if (touch.phase == TouchPhase.Began) // 如果触摸开始

{

Debug.Log("用户点击一次");

CastRay(touch.position);

}

}

}

if (platformNum == "WindowsEditor")//判断平台

{

if (Input.GetMouseButtonDown(0))

{

CastRay(Input.mousePosition);

}

}

}

void CastRay(Vector3 onePos)//这个函数负责向用户点击的点坐标位置发射射线,然后击中二维码后读取内容

{

Ray ray = Camera.main.ScreenPointToRay(onePos);

if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit))

{

Debug.Log("射线射击二维码,用户点击成功");

barcodeAsText.text = hit.collider.GetComponent<BarcodeBehaviour>().InstanceData.Text;//获取二维码内容

string barcodeText = barcodeAsText.text;

if (barcodeText.StartsWith("http://") || barcodeText.StartsWith("https://"))

{

// 使用Application.OpenURL打开网址

Application.OpenURL(barcodeText);

}

else

{

barcodeAsText.text = barcodeText; // 如果不是网址,正常显示扫描结果

}

}

}

}

相关推荐
枯萎穿心攻击7 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
X_StarX15 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
霸王•吕布19 小时前
游戏引擎中顶点着色&像素着色
游戏引擎·顶点着色器·像素着色器·顶点颜色·顶点uv·顶点法向
Thomas_YXQ1 天前
Unity URP法线贴图实现教程
开发语言·unity·性能优化·游戏引擎·unity3d·贴图·单一职责原则
徐子竣1 天前
[学习记录]Unity-Shader-几何着色器
unity·游戏引擎·着色器
EQ-雪梨蛋花汤1 天前
【Part 3 Unity VR眼镜端播放器开发与优化】第四节|高分辨率VR全景视频播放性能优化
unity·音视频·vr
与火星的孩子对话2 天前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
图导物联2 天前
基于AR和SLAM技术的商场智能导视系统技术原理详解
ar·数字人交互·slam 算法·unity 开发·智慧商场·虚实融合营销
幻世界2 天前
【Unity智能模型系列】Unity + MediaPipe + Sentis + ArcFace模型:构建高效人脸识别比对系统
unity·游戏引擎
@大迁世界2 天前
AR 如何改变我们构建网站的方式
后端·ar·restful