Unity结合Vuforia虚拟按键实现AR机械仿真动画效果

news/2025/2/22 5:47:57
<article class="baidu_pl">
article_content" class="article_content clearfix">
arkdown_views prism-atom-one-dark"> arker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

零、最终效果

待上传

一、资源准备

1、Vuforia

Vuforia版本不能高于10.17.4(往上的版本虚拟按键功能被删除)

2、Unity

Unity版本必须要高于2022.3.x,不然使用Vuforia插件时会出现bug

二、主要内容

1、添加虚拟按钮

在这里插入图片描述

2、为虚拟按钮设置名字

在这里插入图片描述
注意:当一个图片中包含多个按钮时,需要为按钮设置不同的名字,否则会出现bug

3、为按钮添加按下和释放时触发的事件

arp">using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class NewBehaviourScript : MonoBehaviour
{
    //获取关联按钮
    public VirtualButtonBehaviour virtualButton;
    // Start is called before the first frame update
    void Start()
    {
        virtualButton.RegisterOnButtonPressed(ButtonPress);
        virtualButton.RegisterOnButtonReleased(ButtonReleased);
    }

	private void ButtonReleased(VirtualButtonBehaviour obj)
	{
		throw new NotImplementedException();
	}

	private void ButtonPress(VirtualButtonBehaviour obj)
	{

        print("虚拟按钮被按下");
	}

	// Update is called once per frame
	void Update()
    {
        
    }
}

4、添加捕捉到图片和丢失图片时触发的事件

arp">using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class NewBehaviourScript : MonoBehaviour
{
    //获取图片捕捉相关脚本
    public DefaultObserverEventHandler machineObserverEvent;
    // Start is called before the first frame update
    void Start()
    {
        machineObserverEvent.OnTargetFound.AddListener(IsFound);
        machineObserverEvent.OnTargetLost.AddListener(IsLost);
    }

	private void IsLost()
	{
        print("丢失图片");
    }

	private void IsFound()
	{
        print("捕捉到图片");
	}

	// Update is called once per frame
	void Update()
    {
        
    }
}

5、添加自己的识别图片数据库

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
将下载的包导入Unity中
在这里插入图片描述

6、导出过程遇到的bug及解决方法

bug:
Manifest merger failed : uses-sdk:minSdkVersion 22 cannot be smaller than version 23 declared in library [:VuforiaEngine:] C:\Users\龚子亦.gradle\caches\transforms-3\5e9057ea50ce3b6971d5e4ed2fb08fbe\transformed\VuforiaEngine\AndroidManifest.xml as the library might be using APIs not available in 22 See the Console for details.
解决方法:
在这里插入图片描述

三、完整代码

arp">using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class Move : MonoBehaviour
{
    [Headerarguments">("捕捉图片相关")]
    public GameObject machine;
    public DefaultObserverEventHandler machineObserverEvent;

    [Headerarguments">("播放动画相关")]
    public VirtualButtonBehaviour play;
    public Animator machineAnimator;
    //判断是否播放动画
    private bool isPress = false;

    [Headerarguments">("拆解零件相关相关")]
    public VirtualButtonBehaviour move;
    private bool isSplit=false;
    // Start is called before the first frame update
    void Start()
    {
        //添加播放按钮按下时 触发  播放动画事件
        play.RegisterOnButtonPressed(PlayAnimation);
        //添加移动按钮按下时 触发  零件拆解事件
        move.RegisterOnButtonPressed(SplitItem);

		//捕捉到图片时    出现模型
		machineObserverEvent.OnTargetFound.AddListener(IsFound);
        //图片消失时      隐藏模型
        machineObserverEvent.OnTargetLost.AddListener(IsLost);

	}

    private void PlayAnimation(VirtualButtonBehaviour play)
	{
        
        isPress = !isPress;
        
		if (isPress)
		{
            print("开始动画按钮被点击");
            machineAnimator.SetBool("isPlay", true);
        }
		else
		{
            print("结束动画按钮被点击");
            machineAnimator.SetBool("isPlay", false);
        }
       
	}
    private void SplitItem(VirtualButtonBehaviour split)
	{
        
        isSplit = !isSplit;
		if (isSplit)
		{
            //零件拆开
            //Split();
            print("拆解零件按钮被点击");
            machineAnimator.SetBool("isSplit", true);
            machineAnimator.SetBool("isMerge", false);
        }
		else
		{
            //零件合并
            //Merge();
            print("合并零件按钮被点击");
            machineAnimator.SetBool("isSplit", false);
            machineAnimator.SetBool("isMerge", true);
        }
    }





    public void IsFound()
	{
	
        machine.SetActive(true);
        print("出现");
    }

    public void IsLost()
	{
        machine.SetActive(false);
        print("消失");

    }
}


http://www.niftyadmin.cn/n/5861682.html

相关文章

华为动态路由-OSPF-完全末梢区域

华为动态路由-OSPF-完全末梢区域 一、OSPF简介 1、OSPF概述 OSPF是一种开放式的、基于链路状态的内部网关协议&#xff08;IGP&#xff09;&#xff0c;用于在自治系统内部进行路由选择和通信。 OSPF是互联网工程任务组&#xff08;IETF&#xff09;定义的标准之一&#xff…

C# WPF调用C++ dll 结合opencv

第一步、打开vs2019&#xff08;其他版本的也可以&#xff09;&#xff0c;创建新项目 第二步、选择c&#xff0c;并创建dll 项目 第三步、在vs2019新项目中配置opencv 第四步、配置完成后写代码&#xff08;写在dllmain.cpp中&#xff09; #include "pch.h" #inclu…

蓝桥杯 r格式(高精度*低精度)

输入样例&#xff1a;2 3.14 输出样例&#xff1a;13 思路&#xff1a;首先注意到本题的数字大小很大&#xff0c;需要用到高精度&#xff0c;因此应该定义string来存储数据d&#xff0c;为了后续计算&#xff0c;在存储完字符串d之后还需要先将每一位转化为数字并且找到小数点…

Flutter项目中设置安卓启动页

AndroidManifest.xml 设置 android:theme“style/LaunchTheme” <applicationandroid:label"string/app_name"android:name"${applicationName}"android:icon"mipmap/ic_launcher"android:roundIcon"mipmap/ic_launcher"android:t…

阿里云通过docker安装skywalking及elasticsearch操作流程

系统 本文使用系统为 Alibaba Cloud Linux 3.2104 LTS 64位 配置为 4核8G PS&#xff1a;最低配置应为2核4G&#xff0c;配置过低无法启动 安装docker 1.卸载旧版本docker yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-…

零基础学QT、C++(五)QT编译、导入OpenCV

目录 一、前言 二、准备工作 三、下载OpenCV包 四、包目录 五、版本匹配问题 六、QT导入OpenCV 七、手动编译OpenCV 1、导入项目&#xff08;他会加载很久&#xff0c;耐心等待&#xff09; 2、项目导入完成后&#xff0c;我们就可以开始构建 3、构建完成&#xff0c;就可以看看…

DuodooBMS源码解读之 equipment_work_order模块

Odoo 18 MrpWorkOrder 扩展模块用户使用手册 一、引言 本用户使用手册旨在帮助你了解并使用基于 Odoo 18 原生模块扩展的 MrpWorkOrder 模块。该扩展模块为制造工单管理提供了额外的功能&#xff0c;包括设备作业单关联、生产数量计算、检验合格数量统计等。通过本手册&#…

UI 自动化测试框架介绍

UI 自动化测试框架是用于模拟用户操作、验证界面功能和行为的工具。以下是一些常见的 UI 自动化测试框架,适用于不同的平台和技术栈: 1. Web UI 自动化框架 用于测试 Web 应用程序的 UI 自动化框架: Selenium: Java、Python、C#、JavaScript、Ruby 等。 : 支持多种浏览器(…