【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; // 如果不是网址,正常显示扫描结果

}

}

}

}

相关推荐
爱看科技几秒前
Snap聚焦AR入局下一代计算,苹果/阿里/微美全息加强构筑智能穿戴市场壁垒!
ar
一只蝉nahc10 小时前
vue使用iframe内嵌unity模型,并且向模型传递信息,接受信息
前端·vue.js·unity
WiChP15 小时前
【V0.1B6】从零开始的2D游戏引擎开发之路
java·log4j·游戏引擎
小拉达不是臭老鼠15 小时前
Unity05_3D数学
学习·unity·游戏引擎
风酥糖20 小时前
Godot游戏练习01-第28节-显示效果与音效
游戏·游戏引擎·godot
油炸自行车1 天前
Unity URDF 导入后运行报错问题笔记
笔记·unity·游戏引擎·数字孪生·urdf·工业仿真·虚拟与现实
南無忘码至尊1 天前
Unity学习90天 - 第 5 天 - 阶段小项目
学习·unity·c#·游戏引擎
郝学胜-神的一滴1 天前
中级OpenGL教程 001:从Main函数到相机操控的完整实现
c++·程序人生·unity·图形渲染·unreal engine·opengl
RReality1 天前
【Unity Shader URP】顶点波浪动画(Vertex Wave)实战教程
ui·unity·游戏引擎·图形渲染
魔士于安1 天前
Unity 简单水面效果URP
游戏·unity·游戏引擎·贴图·模型