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

    }
}
相关推荐
★YUI★11 小时前
学习游戏制作记录(克隆技能)7.25
学习·游戏·unity·c#
不绝19115 小时前
ARPG开发流程第一章——方法合集
算法·游戏·unity·游戏引擎
玩代码15 小时前
Unity里的加力
开发语言·unity
贵州晓智信息科技16 小时前
Unity 性能优化全攻略
unity·性能优化·游戏引擎
UWA18 小时前
UWA DAY 2025 游戏开发者大会|全议程
游戏·unity·性能优化·游戏开发·uwa·unreal engine
ykjhr_3d18 小时前
医疗行业新变革:AR 培训系统助力手术培训精准高效
ar
unicrom_深圳市由你创科技18 小时前
Unity 的UI动画调节
ui·unity·游戏引擎
咩咩觉主1 天前
Unity编辑器拓展 IMGUI与部分Utility知识总结(代码+思维导图)
unity·c#·编辑器·游戏引擎
龚子亦1 天前
【Unity开发】数据存储——XML
xml·unity·游戏引擎·数据存储·游戏开发
write_the_code2 天前
Unity国际版下载链接分享(非c1国内版)
unity·游戏引擎