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

}

}

}

}

相关推荐
先生沉默先17 小时前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图
十画_8241 天前
Visual Studio 小技巧记录
unity·visual studio
red_redemption1 天前
cpp,git,unity学习
git·unity·游戏引擎
tealcwu1 天前
【Unity踩坑】Unity更新Google Play结算库
unity·游戏引擎
先生沉默先1 天前
unity 默认渲染管线材质球的材质通道,材质球的材质通道
unity·游戏引擎·材质
白鹭float.1 天前
【Unity AI】基于 WebSocket 和 讯飞星火大模型
人工智能·websocket·unity
知来者逆1 天前
AR 领域的突破——微型化显示屏为主流 AR 眼镜铺平道路
人工智能·计算机视觉·ar·虚拟现实
一个程序员(●—●)1 天前
Unity各个操作功能+基本游戏物体创建与编辑+Unity场景概念及文件导入导出
unity·游戏引擎
Avalon7122 天前
UniVue大版本更新:UniVue2.0.0-preview
游戏·ui·unity·c#
mirrornan2 天前
AR技术在电商行业的应用及优势有哪些?
ar·3d建模·3d模型·三维建模·ar试戴·ar试穿