平面检测部分
1-场景搭建
场景搭建-下
将上篇博客中提到的物体保存为预制体后,回到unity的界面中,新建一个空物体,命名为Plane Generator,用于生成检测平面的网格。
然后我们为其挂载一个脚本,将其命名为DetectedPlaneGenerator
脚本为:
namespace GoogleARCore.Examples.Common
{
using System.Collections.Generic;
using GoogleARCore;
using UnityEngine;
/// <summary>
/// Manages the visualization of detected planes in the scene.
/// </summary>
public class DetectedPlaneGenerator : MonoBehaviour
{
/// <summary>
/// A prefab for tracking and visualizing detected planes.
/// </summary>
public GameObject DetectedPlanePrefab;
/// <summary>
/// A list to hold new planes ARCore began tracking in the current frame. This object is
/// used across the application to avoid per-frame allocations.
/// </summary>
private List<DetectedPlane> m_NewPlanes = new List<DetectedPlane>();
/// <summary>
/// The Unity Update method.
/// </summary>
public void Update()
{
// Check that motion tracking is tracking.
if (Session.Status != SessionStatus.Tracking)
{
return;
}
// Iterate over planes found in this frame and instantiate corresponding GameObjects to
// visualize them.
Session.GetTrackables<DetectedPlane>(m_NewPlanes, TrackableQueryFilter.New);
for (int i = 0; i < m_NewPlanes.Count; i++)
{
// Instantiate a plane visualization prefab and set it to track the new plane. The
// transform is set to the origin with an identity rotation since the mesh for our
// prefab is updated in Unity World coordinates.
GameObject planeObject =
Instantiate(DetectedPlanePrefab, Vector3.zero, Quaternion.identity, transform);
planeObject.GetComponent<DetectedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
}
}
}
}
并在Unity界面中将我们保存好的预制体拖动到相应的位置上,平面检测的工作就大功告成了