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

embedded/2025/2/22 16:15:06/
<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 updatevoid 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 framevoid 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 updatevoid Start(){machineObserverEvent.OnTargetFound.AddListener(IsFound);machineObserverEvent.OnTargetLost.AddListener(IsLost);}private void IsLost(){print("丢失图片");}private void IsFound(){print("捕捉到图片");}// Update is called once per framevoid 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 updatevoid 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.ppmy.cn/embedded/164382.html

相关文章

科普:“git“与“github“

Git与GitHub的关系可以理解为&#xff1a;Git是一种软件工具&#xff0c;而GitHub则是一个在线平台&#xff0c;它们是“一家子”。二者的关联最直接体现在你通过Git在GitHub仓库中clone软件包到你的机器中来。 具体来说&#xff1a; 一、Git 定义&#xff1a;Git是一个开源的…

在大语言模型(LLM)中调用(Function Calling) 自定义的结构化函数

函数调用&#xff08;Function Calling&#xff09; 指的是大语言模型&#xff08;LLM&#xff09;自动调用我们定义的结构化函数 &#xff0c;比如这里的RouteQuery&#xff0c;它的作用是根据输入的问题内容&#xff0c;返回对应的数据类别&#xff08;python_docs、js_docs …

Spring Cloud — Hystrix 服务隔离、请求缓存及合并

Hystrix 的核心是提供服务容错保护&#xff0c;防止任何单一依赖耗尽整个容器的全部用户线程。使用舱壁隔离模式&#xff0c;对资源或失败单元进行隔离&#xff0c;避免一个服务的失效导致整个系统垮掉&#xff08;雪崩效应&#xff09;。 1 Hystrix监控 Hystrix 提供了对服务…

堆、优先队列、堆排序

堆&#xff1a; 定义&#xff1a; 必须是一个完全二叉树&#xff08;完全二叉树&#xff1a;完全二叉树只允许最后一行不为满&#xff0c;且最后一行必须从左往右排序&#xff0c;最后一行元素之间不可以有间隔&#xff09; 堆序性&#xff1a; 大根堆&#xff1a;每个父节点…

AI赋能前端开发:如何提升你的问题解决能力?

在飞速发展的AI时代&#xff0c;前端开发面临着前所未有的挑战。快速迭代的需求、日益复杂的交互设计、以及多平台兼容性问题&#xff0c;都对开发者的技能和效率提出了更高的要求。 幸运的是&#xff0c;AI写代码工具的出现为我们提供了解决这些问题的有力武器&#xff0c;它正…

什么是向量化?ElasticSearch如何存储向量化?

向量化(Vectorization)是一种将数据或操作转换为向量的过程,以便利用并行计算和高效处理。向量化将非数值数据(如文本、图像)转换为数值向量,以便计算机处理。而向量化在AIGC中非常的常见,例如知识库对话等等。如果大家感兴趣,后面专门来聊聊。 向量长什么样?例如:[…

Huatuo热更新--如何使用

在安装完huatuo热更新插件后就要开始学习如何使用了。 1.创建主框渐Main 新建文件夹Main&#xff08;可自定义&#xff09;&#xff0c;然后按下图创建文件&#xff0c;注意名称与文件夹名称保持一致 然后新建场景&#xff08;Init场景&#xff09;&#xff0c;添加3个空物体…

深度学习数据集

1 huggingface datasets 需要先安装 datasets库 pip install datasets 用coco数据集举例&#xff0c;我们可以搜索coco&#xff0c;然后通过页面右侧的use this dataset或者是 clone respository来获取数据集 https://huggingface.co/datasets/phiyodr/coco2017 huggingface的…