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

零、最终效果

待上传

一、资源准备

1、Vuforia

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

2、Unity

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

二、主要内容

1、添加虚拟按钮

2、为虚拟按钮设置名字


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

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

csharp 复制代码
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、添加捕捉到图片和丢失图片时触发的事件

csharp 复制代码
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.
解决方法:

三、完整代码

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

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

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

    [Header("拆解零件相关相关")]
    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("消失");

    }
}
相关推荐
深空数字孪生3 小时前
数字孪生+工业控制:打造柔性生产线,应对定制化制造挑战
制造·智能制造·数字孪生
2301_793116943 小时前
Unity 解决天空盒中间出现一条线
unity
恒点虚拟仿真4 小时前
XR数字融合工作站打造智能制造专业学习新范式
人工智能·智能制造·虚拟仿真·虚拟仿真实验·xr数字融合工作站·虚拟仿真实训
佩京科技VR4 小时前
禁毒教育展厅互动设备-禁毒教育基地-禁毒体验馆方案-VR禁毒教育软件
unity·vr·禁毒展厅·vr禁毒学习机
平行云19 小时前
Paraverse平行云实时云渲染助力第82届威尼斯电影节XR沉浸式体验
unity·云原生·ue5·xr·实时云渲染
北京阿法龙科技有限公司19 小时前
AR眼镜在智能制造的应用方向和场景用例|阿法龙XR云平台
ar·xr·制造
大有数据可视化19 小时前
安全与效率兼得:工业控制系统如何借力数字孪生实现双赢?
网络·数字孪生·工业空盒子
Xeon_CC21 小时前
Unity中,软遮罩SoftMaskForUGUI的使用
unity·游戏引擎
DanmF--1 天前
NGUI--三大基础组件
unity·游戏引擎
SmalBox1 天前
【URP】Unity3D物体遮罩的多种方案实现
unity·渲染