【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 分钟前
C# &Unity 唐老狮 No.7 模拟面试题
开发语言·unity·c#
咩咩觉主29 分钟前
Unity网络开发基础 (2) 网络协议基础
网络·unity·c#
Tatalaluola1 小时前
Unity实现在镜子间反射光柱
unity·c#·游戏引擎
ccloud116 小时前
OpenGL实现场景编辑器
qt·游戏引擎
Octopus20777 小时前
【Godot】实现对话系统
游戏引擎·godot·游戏程序
WenGyyyL7 小时前
使用OpenCV和MediaPipe库——增强现实特效(在手腕添加虚拟手表)
人工智能·opencv·计算机视觉·ar·cv·mediapipe
mirrornan10 小时前
打破界限!家电行业3D数字化营销,线上线下无缝对接
3d·ar·3d模型·电商·3d数字化
君莫愁。20 小时前
【Unity】搭建基于字典(Dictionary)和泛型列表(List)的音频系统
数据结构·unity·c#·游戏引擎·音频
唐小墨同学1 天前
Pico 4 Enterprise(企业版)与Unity的交互-打包运行及UI交互篇
ui·unity
虾球xz1 天前
游戏引擎学习第143天
学习·游戏引擎