游戏展示
游戏开发
安装 ARKit Package
- Window
- Package Manager
搜索 AR
AR Foundation
必需ARKit XR Plugin
/ iOSARCore XR Plugin
/ Android
打开 Package
时遇到网络问题
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
- XR
- Create
Inspector
- Position:
- Y:
0.05
- Y:
- Scale
- X:
0.1
- Y:
0.1
- Z:
0.1
- X:
- Position:
Gizmos
- 3D Icons:
0
- 3D Icons:
Prefabs
- Hierarchy
- Empty Object:
Game Piece
- Game Piece
- Cube
- Empty Object:
Quad / 四方块
- Hierarchy
- Empty Object:
Placement Indicator
- Placement Indicator
- Quad
- Empty Object:
- Inspector
- Rotation:
- X:
90
- X:
- Scale:
- X:
0.1
- Y:
0.1
- Z:
0.1
- X:
- Rotation:
导入 Toy Plane
导入 package
后,模型调整一下。
模型有点大,可以调整 ARSessionOrigin
,相关于缩小了模型尺寸
- Inspector
- Scale
- X:
10
- Y:
10
- Z:
10
- X:
- Scale
脚本
- 屏幕中心点发出射线
- 如果射线与检测的平面相交
- 在平面显示指示器
- 如果点击指示器
- 指示器位置生成
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)
- Player Settings
- iOS
- Auto Graphics API:
false
! importent - Requires ARKit Support:
true
- Camera Usage Description
- Target minimum iOS Version:
11
- Architecture:
ARM64
- Auto Graphics API:
如果不行,可以要以下设置
- Graphics
- Always Includes Shaders
- Elements 0:
Sprites/Default
- Elements 1:
UI/Default
- Elements 0:
- Always Includes Shaders
- Android:
- Multithreaded Rendering:
false
- Minimum API Level: `Android 7.0 ‘Nougat’ (API 24)
- Multithreaded Rendering:
小结
参考:
下载
游戏
Release: https://github.com/GameDevLog/GameDevLogTemplete/releases