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("消失");

    }
}
相关推荐
SmalBox12 小时前
【渲染流水线】[几何阶段]-[归一化NDC]以UnityURP为例
unity·渲染
SmalBox1 天前
【渲染流水线】[几何阶段]-[图元装配]以UnityURP为例
unity·渲染
dundunmm2 天前
【每天一个知识点】生物的数字孪生
人工智能·数字孪生·生物信息·单细胞
霜绛2 天前
Unity:GUI笔记(一)——文本、按钮、多选框和单选框、输入框和拖动条、图片绘制和框绘制
笔记·学习·unity·游戏引擎
ykjhr_3d2 天前
相较于传统AR作战环境虚拟仿真系统,其优势体现在哪些方面?
ar
谷宇.2 天前
【Unity3D实例-功能-移动】角色行走和奔跑的相互切换
游戏·unity·c#·unity3d·游戏开发·游戏编程
17岁的勇气2 天前
Unity Shader unity文档学习笔记(十九):粘土效果,任意网格转化成一个球(顶点动画,曲面着色器)
笔记·学习·unity·图形渲染·顶点着色器·曲面着色器
benben0443 天前
《Unity Shader入门精要》学习笔记一
unity·shader
YF云飞3 天前
Unity图片优化与比例控制全攻略
游戏·unity·游戏引擎·游戏程序·个人开发
SmalBox3 天前
【渲染流水线】[几何阶段]-[几何着色]以UnityURP为例
unity·渲染