|
|
|
|
|
|
[RequireComponent(typeof(ARSessionOrigin))] |
|
|
|
public class PlaceOnPlane : MonoBehaviour |
|
|
|
{ |
|
|
|
[SerializeField] |
|
|
|
[Tooltip("Instantiates this prefab on a plane at the touch location.")] |
|
|
|
GameObject m_PlacedPrefab; |
|
|
|
[SerializeField] [Tooltip("Instantiates this prefab on a plane at the touch location.")] |
|
|
|
public GameObject PlacedPrefab; |
|
|
|
/// <summary>
|
|
|
|
/// The prefab to instantiate on touch.
|
|
|
|
/// </summary>
|
|
|
|
public GameObject placedPrefab |
|
|
|
{ |
|
|
|
get { return m_PlacedPrefab; } |
|
|
|
set { m_PlacedPrefab = value; } |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The object instantiated as a result of a successful raycast intersection with a plane.
|
|
|
|
/// </summary>
|
|
|
|
public GameObject placedObject { get; private set; } |
|
|
|
private List<ARRaycastHit> hits; |
|
|
|
private bool spawnedPrefab = false; |
|
|
|
private GameObject spawnedObject; |
|
|
|
private ARSessionOrigin sessionOrigin; |
|
|
|
m_SessionOrigin = GetComponent<ARSessionOrigin>(); |
|
|
|
sessionOrigin = GetComponent<ARSessionOrigin>(); |
|
|
|
hits = new List<ARRaycastHit>(); |
|
|
|
if (Input.touchCount == 0) |
|
|
|
return; |
|
|
|
|
|
|
|
var touch = Input.GetTouch(0); |
|
|
|
|
|
|
|
var hits = s_RaycastHits; |
|
|
|
|
|
|
|
if (!m_SessionOrigin.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) |
|
|
|
return; |
|
|
|
|
|
|
|
placementHit = hits[0]; |
|
|
|
} |
|
|
|
|
|
|
|
ARSessionOrigin m_SessionOrigin; |
|
|
|
|
|
|
|
ARRaycastHit m_PlacementHit; |
|
|
|
|
|
|
|
ARRaycastHit placementHit |
|
|
|
{ |
|
|
|
get { return m_PlacementHit; } |
|
|
|
set |
|
|
|
if (Input.touchCount > 0) |
|
|
|
m_PlacementHit = value; |
|
|
|
Touch touch = Input.GetTouch(0); |
|
|
|
if (placedObject == null && m_PlacedPrefab != null) |
|
|
|
if (!spawnedPrefab) |
|
|
|
placedObject = Instantiate(m_PlacedPrefab); |
|
|
|
if (touch.phase == TouchPhase.Began) |
|
|
|
{ |
|
|
|
if (sessionOrigin.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) |
|
|
|
{ |
|
|
|
Pose hitPose = hits[0].pose; |
|
|
|
|
|
|
|
spawnedObject = Instantiate(PlacedPrefab, hitPose.position, hitPose.rotation); |
|
|
|
spawnedPrefab = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (placedObject != null) |
|
|
|
else |
|
|
|
var pose = m_PlacementHit.pose; |
|
|
|
placedObject.transform.position = pose.position; |
|
|
|
placedObject.transform.rotation = pose.rotation; |
|
|
|
if (sessionOrigin.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) |
|
|
|
{ |
|
|
|
Pose hitPose = hits[0].pose; |
|
|
|
spawnedObject.transform.position = hitPose.position; |
|
|
|
} |
|
|
|
|
|
|
|
List<ARRaycastHit> s_RaycastHits = new List<ARRaycastHit>(); |
|
|
|
} |