1. 安装Unity Hub
- 前言
我们只搞iOS端,安卓端不搞,但是安卓的可以参考下
官方网站
- 按照下图所示,下载
- 添加模板
- 补充说明
偏好设置里,可以设置语言 - 注意:创建的项目不要选择托管
4. 打开界面
2. doSomethings
- 补充说明
Hierarchy: 层级的意思
- 在左下方
Project
–>Assets
中
- 创建
Prefabs
文件夹 - 创建
Scripts
文件夹
- 在Hierarchy的空白处,右击,创建
AR Default Plane
2. 将AR Default Plane
拖拽到Prefabs
文件夹中
3. Scenes
中,修改Scene的名字为HelloWorld
4. 添加AR Plane Manger
5. 一般会默认有AR Plane Manager
,那我们就不再添加了 -_- !
-
点击替换
AR Plane Manager
中的Plane Prefab
,替换为我们自定义的文件中的Prefabs1
-
在我们创建的文件夹
Scripts
中添加C#脚本
-
配置AppController
- 将刚刚创建的C#脚本文件重命名为
AppController
,双击,如果你电脑安装了Visual Studio Code
,就会打开
- 脚本编写
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
[RequireComponent(typeof(ARRaycastManager))]
public class AppController : MonoBehaviour
{
public GameObject spawnPrefab;
static List<ARRaycastHit> Hits;
private ARRaycastManager mRaycastManager;
private GameObject spawnedObject = null;
// Start is called before the first frame update
private void Start()
{
Hits = new List<ARRaycastHit>();
mRaycastManager = GetComponent<ARRaycastManager>();
}
// Update is called once per frame
private 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;
}
}
}
}
3. 准备运行
-
添加一个Cube
添加一个material
-
新建了一个圆球
-
着色
-
给新创建的cube选择对应的颜色
-
给Cube添加AppController脚本
- 选中
AR Session Origin
- 点击
Add Component
- 搜索
AppController
- 在
App Controller(Script)
中的Spawn Prefab
中设置为创建的CubeTestCube
- 编译运行
- 安装iOS配置
- 继续相关设置
-
前面因为项目不小心点了托管,然后这里Build and run 无法点击,后面重新建了一次项目,没有点击托管项目,就可以点击Build and run 了。
-
生成的项目用xcode是可以打开,但是网络问题无法链接,打开app就是黑屏状态。。。