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

    }
}
相关推荐
huoyingcg6 小时前
VR、AR、互动科技:武汉数字展馆制作引领未来展览新体验
科技·3d·ar·vr·动画·虚拟现实
虾球xz7 小时前
游戏引擎学习第235天:在 Windows 上初始化 OpenGL
windows·学习·游戏引擎
惊鸿醉10 小时前
Unity中的数字孪生项目:两种输入方式对观察物体的实现
数码相机·unity·游戏引擎
智慧城市指北公众号11 小时前
数字孪生城市技术应用典型实践案例汇编(22个典型案例)(附下载)
智慧城市·数字孪生·典型案例
虾球xz12 小时前
游戏引擎学习第232天
c++·学习·游戏引擎
benben04421 小时前
Unity3D仿星露谷物语开发35之锄地动画
前端·游戏·游戏引擎
程序员茶馆1 天前
【unity】Vulkan模式下部分Android机型使用VideoPlayer组件播放视频异常问题
游戏·unity·游戏引擎·图形渲染·unity3d·游戏开发
虾球xz1 天前
游戏引擎学习第238天:让 OpenGL 使用我们的屏幕坐标
学习·游戏引擎
李詹1 天前
防护接入新纪元:DeepSeek攻防大脑如何重塑网络安全防线
网络·安全·web安全·游戏引擎·游戏程序
心前阳光1 天前
Unity-微信截图功能简单复刻-03绘制空心矩形
unity·微信·游戏引擎