【03】Unity AR 2022Vuforia图片识别超详细案例(小岛动画交互)【含代码】

Unity AR Vuforia(拓展)——Vuforia图片识别超详细案例(小岛动画交互)【含代码】

1.环境搭建

环境搭建,包括Vuforia官网的注册登录,密钥获取,在Unity中对Vuforia Engine AR插件的安装等等
这里是在Unity中对Vuforia Engine AR插件的方法https://blog.csdn.net/m0_57495651/article/details/128107903
这里是环境搭建需要完成的其他步骤:https://blog.csdn.net/m0_57495651/article/details/128108242

2.资源导入

我已经在我的个人主页资源中上传(名称同本文章标题)
将FBX文件导入Unity

3.交互之旋转 & 附加代码

(1)Create C# Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotate : MonoBehaviour {
    
    

    float xSpeed = 150.0f;

	// Use this for initialization
	void Start () {
    
    
		
	}
	
	// Update is called once per frame
	void Update () {
    
    
        if (Input.GetMouseButton(0))
        {
    
    
            if (Input.touchCount == 1)
            {
    
    
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
    
    
                    transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * -xSpeed * Time.deltaTime, Space.World);
                }
            }
        }
	}
}

(2)再Create C# Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enlarge : MonoBehaviour {
    
    

    Vector2 oldPos1;
    Vector2 oldPos2;

	// Use this for initialization
	void Start () {
    
    
		
	}
	
	// Update is called once per frame
	void Update () {
    
    
        if (Input.touchCount == 2)
        {
    
    
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
    
    
                Vector2 tempos1 = Input.GetTouch(0).position;
                Vector2 tempos2 = Input.GetTouch(1).position;

                if (isEnlarge(oldPos1, oldPos2, tempos1, tempos2))
                {
    
    
                    float oldScale = transform.localScale.x;
                    float newScalse = oldScale * 1.025f;

                    transform.localScale = new Vector3(newScalse, newScalse, newScalse);
                }
                else
                {
    
    
                    float oldScale = transform.localScale.x;
                    float newScalse = oldScale / 1.025f;

                    transform.localScale = new Vector3(newScalse, newScalse, newScalse);
                }

                oldPos1 = tempos1;
                oldPos2 = tempos2;
            }
        }
	}

    //判断手势
    bool isEnlarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2)
    {
    
    
        float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

        if (length1 < length2)
        {
    
    
            return true;
        }
        else
        {
    
    
            return false;
        }
    }
}

4. Making Animator

由于内容过于简单不进行图文描述

【读者也可跳过此步骤,上述步骤3的代码交互旋转亦可实现,
此步骤是制作动画状态机,不制作动画状态机项目也可以正常运行,
不会产生任何影响】

提醒:读者Making Animator后,记得将状态机挂载至对象上

5.试运行导出APK

【以下内容源自我的第二篇文章(02图片识别教程)6——8步骤】
不了解的读者可以点击【02】2022.11最新超详细Vuforia图片识别教程此链接学习6——8步骤,也可继续阅读下面内容学习,一模一样的,请放心学习!
点击如下图所示,File——Building Settings

选择安卓平台

在这里插入图片描述
选如下图所示红框(Switch Platform)选择该安卓平台

在这里插入图片描述

选择【Build】
在这里插入图片描述

因为是第一次导出APK,会比较慢!请耐心等待!别急!会成功的!

进度条走完后,弹出框如下图所示
(自行命名,我比较喜欢以时间命名来区分,现在是2022.11.27,我就以今天命名了,读者可以自由命名)
命名完后,点击【保存】
在这里插入图片描述
接下来,找到我们刚刚导出的APK,将APK发送到安卓手机上。

6.使用安卓手机下载安装及可能出现的问题

安卓手机接收到APK后,点击接收,再点击安装

这里提醒一下:安卓手机首次安装该apk,会被要求谷歌下载一个东西,根据提示放心下载!!!后面我们做项目将apk导入安卓手机时就不会弹出来了

安装后打开对着刚刚添加的Image,即可看到效果(我们Image Target下面的物体)
注意:这里分享一下常见问题
(1)手机无法安装
(2)点开后黑屏

7.适用的机型及问题的解决方法

(1)适用机型:小米,OPPO, VIVO ,iQOO,一加,联想等安卓手机均可实现
(2)上述两个问题的解决方法:
A1:手机无法安装:IOS系统和华为手机无法使用、含手机管家的需要关闭或不让其扫描或不开启安全安装,选择直接安装
A2:点开后黑屏:没有开启摄像头权限

8.效果展示

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

上述的部分内容源自我的第一篇,第二篇文章中。本文提炼了案例的主干内容,但是所有步骤都已经写明,且当时本文引用的出处已经标记了引自我的哪篇文章哪几个步骤,学习起来也很方便的!
【01】如何在Unity 中 安装 Vuforia Engine AR 插件?(附加检验安装成功方法) https://blog.csdn.net/m0_57495651/article/details/128107903
【02】2022.11最新超详细Vuforia图片识别教程https://blog.csdn.net/m0_57495651/article/details/128108242

猜你喜欢

转载自blog.csdn.net/m0_57495651/article/details/128108261