ARFoundation road - environment configuration (Android)

  ARFoundation support cross-platform development, but the development environment configuration and deployment release was quite different, therefore, environment configuration will be divided into two Android and iOS to explain.
  ARFoundation used in the Android terminal is ARCore underlying technology, therefore, environment ready to develop similar use ARCore alone, see Bowen, " ARCore road - Preparing the environment " (no need to install ARCore unity SDK), this article assumes that the reader has been installed Unity2019 .1 latest version.

(A) introducing widget

  Now we will create a new project, and import ARFoundation and ARCore XR Plugin. In the Start menu or desktop icon Unity (Unity Hub or icon) to start Unity, click New to create a new project, we named Hellowrold, then click the "Create Project" button.
Here Insert Picture Description
  After the Unity unified main window opens, click on the menu bar Unity window-> Package Manager, Package Manager dialog box opens.

Here Insert Picture Description
  Package Manager dialog box is not displayed by default ARFoundation and ARCore XR Plugin, because these plug-ins are currently in a preview state. Click the Advanced dialog box drop-down menu, select Show preview package, which will preview all of the plug-ins are also displayed.
Here Insert Picture Description
Here Insert Picture Description
  After selecting ARFoundation in the list on the left to the right panel of the plug-in details will be displayed, click on the bottom right corner of the "Install", installation of plug-ins. Doing so, the ARCore XR Plugin plugin installed. So far, it has been required to develop an AR Android plugin imported.

(B) Setting up the development environment

  在Unity窗口中,按Ctrl+Shift+B快捷键,或者在菜单栏中选择File->Build Settings…,打开设置窗口。选择“Platform”下的“Android”选项,然后单击“Switch Platform”按钮切换到Android平台。当Unity标志出现在Android选项旁边时,发布平台就切换成Android了,如下图所示。
Here Insert Picture Description
  单击下方的“Player Settings”按钮继续后续设置,如下图所示,按顺序依次选择Player,在Player栏中Company Name与Product Name中填写公司名与产品名,选择Android小图标,在Other Setting选项卡中取消勾选Multithreaded Rendering(多线程渲染),最后还要删除Vulkan,因为Android不支持Vulkan。
Here Insert Picture Description
  继续往下滚动,在Identification栏中,PackageName填写应用程序包名,建议与上图Company Name和Product Name中输入的公司和程序包名一致,这个值要求唯一,因为如果它与另一个应用程序具有相同的包名,可能会导致问题。另外,我们还需要设置与ARCore兼容的Android最低版本,找到“Minimun API level”选项,点击其下拉菜单,选择“Android7.0‘nougat’(API level 24)”或以上,正如这个选项名字一样,应用程序与ARCore将不会在Nougat版本之前的Android设备上运行。同时,我们还需要设置一下“Target API Level”,这里设置的是 “Android 8.0 ‘Oreo’ (API level 26)”,因为笔者的测试手机就是这个版本,读者可以根据自己的需要设置,但目标版本不得低于Nougat,不然开发的AR应用将无法运行,如下图所示。

Here Insert Picture Description
   在完成以上设置后,点击“Other Settings”文字以收起Other Settings设置折叠栏,然后点击“XR Settings”展开之,在使用ARCore原生SDK进行开发时,要求选中“ARCore Supported”复选框以确保应用得到ARCore的支持,但是在使用ARFoundation和ARCore XR Plugin进行Android AR开发时一定不要选择这个勾,不然编译将通不过。
Here Insert Picture Description
  完成上述设置操作后,在Unity菜单中点击 Edit->Preferences,打开Unity Preferences对话框,如下图所示,选择External Tools选项卡。在这里,我们不仅可以设置使用的代码开发IDE、图片编辑器,最重要的是设置Android SDK和JDK的路径。在设置时确保路径正确,否则将无法正确编译生成Android应用(这个设置只需要操作一次,与应用开发项目无关,即设置完一次后再创建新应用也不必再次设置)。在2019版Unity中,为了简化用户的操作,Unity在安装的时候会进行环境检测,如果检测到已安装JDK和Android SDK后会自动进行设置,如果没有检测到Unity也可以进行集成安装(安装Unity时会一并安装JDK和Android SDK),无需要用户单独安装JDK和Android SDK。
Here Insert Picture Description
   在完成以上设置后,下面我们来搭建ARFoundation开发AR应用的基础框架,这个基础框架所有AR应用通用。

(三)搭建基础框架

  在Unity打开的中,我们重命名场景文件为“Helloworld”,为后面统一管理文件,在Projects窗口Assets目录下新建Prefabs、Scripts两个文件夹,同时在Hierarchy窗口中删除Main Camera(因为ARFoundation AR Session Origin只带有一个AR摄像机,Directional Light可根据需要决定是否删除),如下图所示。
Here Insert Picture Description
  在Hierarchy窗口中的空白处点击鼠标右键,在弹出的级联菜单中依次选择XR–>AR Session和XR–>AR Session Origin,新建这两个AR基础组件。
Here Insert Picture Description
  在Hierarchy窗口中的空白处点击鼠标右键,在弹出的级联菜单中依次选择XR–>AR Default Plane,将Hierarchy窗口中生成的AR Default Plane拖动到Project窗口中的Prefabs文件夹下,制作一个平面prefab,如下图所示,然后删除Hierarchy窗口中的AR Default Plane对象。
Here Insert Picture Description
  在Hierarchy窗口选中AR Session Origin对象,然后在Inspector窗口中点击Add Component按钮,并在弹出的搜索中搜索“AR Plane Manager”,并添加该组件,如下图所示。
Here Insert Picture Description
  将Project窗口中Prefabs文件夹下的AR Default Plane拖到AR Plane Manager组件下的Plane Prefab属性框,如下图所示。
Here Insert Picture Description
  至此,AR应用的基础框架我们已经搭建好了,这是一个ARFoundation开发AR应用的基础框架,流程通用。

(四)AppController

  在Project窗口Scripts文件夹下,空白处点击鼠标右键,在弹出的级联菜单中依次选择Create -->C# Script,新建一个脚本文件,并命名为AppController,如下图所示。
Here Insert Picture Description
  双击AppController脚本,在Visual Studio中编辑该脚本,添加如下代码。

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARRaycastManager))]
public class AppControler : MonoBehaviour
{
    public  GameObject spawnPrefab;
    static List<ARRaycastHit> Hits;
    private ARRaycastManager mRaycastManager;
    private GameObject spawnedObject = null;
    private void Start()
    {
        Hits = new List<ARRaycastHit>();
        mRaycastManager = GetComponent<ARRaycastManager>();
    }

    void Update()
    {
        if (Input.touchCount == 0)
            return;
        var touch = Input.GetTouch(0);
        if (mRaycastManager.Raycast(touch.position, Hits, TrackableType.PlaneWithinPolygon | TrackableType.PlaneWithinBounds))
        {
            var hitPose = Hits[0].pose;
            if (spawnedObject == null)
            {
                spawnedObject = Instantiate(spawnPrefab, hitPose.position, hitPose.rotation);
            }
            else
            {
                spawnedObject.transform.position = hitPose.position;
            }
        }
    }
}

  在上段代码中,首先我们使用[RequireComponent(typeof(ARRaycastManager))]属性确保添加该脚本的对象上必须要有ARRaycastManager组件,因为射线检测需要用到ARRaycastManager组件。在Update()方法中,我们对手势进行射线检测,在检测到的平面上放置一个虚拟物体,如果该虚拟物体已存在,则将该虚拟物体的移动到射线检测与平面的碰撞点。

(五)运行Helloworld

  经过以上步骤,整体AR应用框架已完全搭建起来了,最后,我们在Hierarchy窗口中制作一个cube,为其赋上红色材质,将其Scale缩放成(0.1,0.1,0.1),最后将其制作成Prefab并删除Hierarchy场景中的cube,如下图所示。

Here Insert Picture Description
  The final step, select AR Session Origin in the Hierarchy window to add AppController scripted front (you can use the Add Component to add AppController in the search box, or directly onto the AppController script AR Session Origin objects), and Previous produced cube onto the AppController script Spawn Prefab property box.
Here Insert Picture Description
  AR applications have been developed finished, connect ARCore support phone via USB cable or WiFi on your computer, the specific connection method, please refer Bowen " ARCore road - connected device debug applications ", press Ctrl + Shift + B key combination (or select File -> Build Settings) to open the Build Settings dialog box. To ensure that the current scene is selected, click Build And Run, set up after the release of the program name in the dialog box that opens, click the "Save" button to start compiled apk, apk generated application is automatically installed on the phone connected and up and running, As shown below.
Here Insert Picture Description
  Should compile without error, after Helloworld AR application is open, to find a relatively flat and rich textures of mobile phone plane plane around the detection, upon detection of a plane, the plane finger click on the phone screen is detected, it will be in the loading a small cube plane, as shown in FIG. At this point, we use the Android Helloworld AR application ARFoundation has been successfully developed.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/yolon3000/article/details/93061181