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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity3d青子

难题的解决使成本节约,求打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值