Unity + Vuforia [ Augmented Reality ]

Unity + Vuforia [ Augmented Reality ]

A Game Engine


1.下载所需软件

  • UnityHub,是 Unity 的下载器,安装完成后可以选择近几个版本进行安装,
  • Vuforia,是一个增强现实的平台,提供了很多 API 接口,只需用户简单进行调用即可实现增强现实的效果
    • 点击名称跳转至下载,其中 Vuforia 除了在网站上下载,在 UnityHub 安装 Unity 时,也有些版本会带有 Vuforia 的插件,那就只要把复选框勾上即可,不用再在网站上下载
      Unity + Vuforia [ Augmented Reality ]

2.创建 Unity 项目

Unity + Vuforia [ Augmented Reality ]


Unity + Vuforia [ Augmented Reality ]


3.配置 MainCamera

  • 删除主相机 MainCamera,在 GameObject菜单 - Vuforia Engine 中点击创建 AR Camera,点击之后会要导入一些资源,确定就好
  • 获取许可证。
    • 点击打开 Vuforia 官网,注册一个账号
    • 点击 Develop - Get Development Key
      Unity + Vuforia [ Augmented Reality ]
    • 如下创建

      Unity + Vuforia [ Augmented Reality ]
    • 点击 Name 获取许可证
      Unity + Vuforia [ Augmented Reality ]
      Unity + Vuforia [ Augmented Reality ]
  • 配置 AR Camera
    • 打开系统相机
      • File - Build Setting...

        Unity + Vuforia [ Augmented Reality ]
      • Payer Settings... - Player - XR Settings 勾选 Virtual Reality SupportedVuforia Augmented Reality Supported
        Unity + Vuforia [ Augmented Reality ]
    • 配置 AR Camera 许可证
      • 选中 AR Camera ,在它的信息中选择打开 Open Vuforia Engine Configuration
        Unity + Vuforia [ Augmented Reality ]
      • Vuforia 官网申请的许可证粘贴至此

        Unity + Vuforia [ Augmented Reality ]

4.配置要识别的目标 - Image

  • 配置 Image, 在 GameObject菜单 - Vuforia Engine 中点击创建 Image

    Unity + Vuforia [ Augmented Reality ]

  • 这个 Image 是作为识别的目标,就是说,用户 Image 上设置有一张图片,当相机识别到和这个图片相似的图片时,就可以创建出显示增强的物体

  • 先来说怎么自定义图片,我们不能直接给他设置图片,要先上传到 Vuforia 官网,然后再把一张或多张图片以 Unity包 的形式下载下来,然后导入后才可使用

    • 来到 Vuforia 官网, Support - Add DataBase
      Unity + Vuforia [ Augmented Reality ]
    • 然后如下设置
      Unity + Vuforia [ Augmented Reality ]
    • 点击 name 进入
      Unity + Vuforia [ Augmented Reality ]
    • 选择 Add Target
      Unity + Vuforia [ Augmented Reality ]
    • 上传,Type 默认 Single ImageWidthName 随意

      Unity + Vuforia [ Augmented Reality ]
    • 上传完后,稍等片刻即可,其中Rating 就是可识别度,Vuforia 服务器是会将图片进行灰度处理,然后对其进行评分,用户最好是选用轮廓分明、色差较大的图片作为目标
      Unity + Vuforia [ Augmented Reality ]
    • 下载,选择 Unity Editor,然后 DownLoad

      Unity + Vuforia [ Augmented Reality ]

  • 导入下载的数据包

    • 将包直接拖拽至 Project 面板的 Assets 文件夹,Import
      Unity + Vuforia [ Augmented Reality ]
      Unity + Vuforia [ Augmented Reality ]
    • 导入成功
      Unity + Vuforia [ Augmented Reality ]
  • 设置 Image
    选中 ImageTarget ,在右侧 Inspector 面板中的 DataBase 选择为下载的数据包,然后将其下面的 Image Target 设置为需要的图片。
    Unity + Vuforia [ Augmented Reality ]

    • 成功
      Unity + Vuforia [ Augmented Reality ]

5.创建显示增强对象

简单来说就是设置在识别出图片之后要显示出什么东西

  • 设置方法就是将要显示的物体拖拽放置在 ImageTarget ,使它成为 ImageTarget 的子对象
  • 这里我用几个简单模型稍做演示
    Unity + Vuforia [ Augmented Reality ]

6.运行

Unity + Vuforia [ Augmented Reality ]
Unity + Vuforia [ Augmented Reality ]