ARFoundation

游戏展示

ARDemo

游戏开发

安装 ARKit Package

  • Window
    • Package Manager

搜索 AR

  • AR Foundation 必需
  • ARKit XR Plugin / iOS
  • ARCore XR Plugin / Android

打开 Package 时遇到网络问题

Network configuration

1
echo '#!/bin/bash
2
export HTTP_PROXY=proxy-url
3
export HTTPS_PROXY=proxy-url
4
nohup "/Applications/Unity Hub.app/Contents/MacOS/Unity Hub" &>/dev/null &' > launchUnityHub.command
5
chmod +x launchUnityHub.command

AR Session

删除自带的 Main Camera

  • Hierarchy

    • Create
      • XR
        • AR Session
        • AR Session Origin
      • Cube
  • Inspector

    • Position:
      • Y: 0.05
    • Scale
      • X: 0.1
      • Y: 0.1
      • Z: 0.1
  • Gizmos

    • 3D Icons: 0

Prefabs

  • Hierarchy
    • Empty Object: Game Piece
    • Game Piece
      • Cube

Quad / 四方块

  • Hierarchy
    • Empty Object: Placement Indicator
    • Placement Indicator
      • Quad
  • Inspector
    • Rotation:
      • X: 90
    • Scale:
      • X: 0.1
      • Y: 0.1
      • Z: 0.1

导入 Toy Plane

导入 package 后,模型调整一下。

模型有点大,可以调整 ARSessionOrigin,相关于缩小了模型尺寸

  • Inspector
    • Scale
      • X: 10
      • Y: 10
      • Z: 10

脚本

  1. 屏幕中心点发出射线
  2. 如果射线与检测的平面相交
    1. 在平面显示指示器
    2. 如果点击指示器
      1. 指示器位置生成 Prefabs 模型

ARTapToPlaceObject.cs

1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4
using UnityEngine.XR.ARFoundation;
5
using UnityEngine.XR.ARSubsystems;
6
7
public class ARTapToPlaceObject : MonoBehaviour
8
{
9
    public GameObject objectToPlace;
10
    public GameObject placementIndictor;
11
12
    private ARRaycastManager raycastManager;
13
    private Pose placementPose;
14
    private bool placementPoseIsVilid;
15
16
    void Start()
17
    {
18
        raycastManager = FindObjectOfType<ARRaycastManager>();
19
    }
20
21
    void Update()
22
    {
23
        UpdatePlacementPose();
24
        UpdatePlacementIndicator();
25
26
        if (placementPoseIsVilid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
27
        {
28
            PlaceObject();
29
        }
30
    }
31
32
    private void UpdatePlacementPose()
33
    {
34
        var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
35
        var hits = new List<ARRaycastHit>();
36
        raycastManager.Raycast(screenCenter, hits, TrackableType.Planes);
37
38
        placementPoseIsVilid = hits.Count > 0;
39
        if (placementPoseIsVilid)
40
        {
41
            placementPose = hits[0].pose;
42
43
            var cameraForword = Camera.current.transform.forward;
44
            var cameraBearing = new Vector3(cameraForword.x, 0, cameraForword.z).normalized;
45
            placementPose.rotation = Quaternion.LookRotation(cameraBearing);
46
        }
47
    }
48
49
    private void UpdatePlacementIndicator()
50
    {
51
        if (placementPoseIsVilid)
52
        {
53
            placementIndictor.SetActive(true);
54
            placementIndictor.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
55
        }
56
        else
57
        {
58
            placementIndictor.SetActive(false);
59
        }
60
    }
61
62
    private void PlaceObject()
63
    {
64
        Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
65
    }
66
}

导出游戏

  • File
    • Build Settings (Shift + Cmd + B)

Build Settings

  • Player Settings
  • iOS
    • Auto Graphics API: false ! importent
    • Requires ARKit Support: true
    • Camera Usage Description
    • Target minimum iOS Version: 11
    • Architecture: ARM64

如果不行,可以要以下设置

  • Graphics
    • Always Includes Shaders
      • Elements 0: Sprites/Default
      • Elements 1: UI/Default
  • Android:
    • Multithreaded Rendering: false
    • Minimum API Level: `Android 7.0 ‘Nougat’ (API 24)

小结

参考:

下载

游戏

Release: https://github.com/GameDevLog/GameDevLogTemplete/releases

源码

GitHub: https://github.com/GameDevLog/GameDevLogTemplete

本文标题:ARFoundation

文章作者:iOSDevLog

发布时间:2019年12月14日 - 11:24:52

最后更新:2019年12月24日 - 21:06:05

原始链接:https://game.iosdevlog.com/2019/12/14/ARKit/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

iOSDevLog wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!