|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.XR.ARFoundation; |
|
|
|
using UnityEngine.XR.ARSubsystems; |
|
|
|
using UnityEngine.InputSystem; |
|
|
|
|
|
|
|
namespace UnityEngine.XR.ARFoundation.Samples |
|
|
|
{ |
|
|
|
public class InputSystem_PlaceOnPlane : MonoBehaviour |
|
|
|
{ |
|
|
|
|
|
|
|
[SerializeField] |
|
|
|
[Tooltip("Instantiates this prefab on a plane at the touch location.")] |
|
|
|
GameObject m_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 spawnedObject { get; private set; } |
|
|
|
|
|
|
|
void Awake() |
|
|
|
{ |
|
|
|
m_RaycastManager = GetComponent<ARRaycastManager>(); |
|
|
|
} |
|
|
|
|
|
|
|
public void AddObject(InputAction.CallbackContext context) |
|
|
|
{ |
|
|
|
Debug.Log("Spawn!"); |
|
|
|
var touchPosition = context.ReadValue<Vector2>(); |
|
|
|
Debug.Log(touchPosition); |
|
|
|
if (m_RaycastManager.Raycast(touchPosition, s_Hits, TrackableType.PlaneWithinPolygon)) |
|
|
|
{ |
|
|
|
// Raycast hits are sorted by distance, so the first one
|
|
|
|
// will be the closest hit.
|
|
|
|
var hitPose = s_Hits[0].pose; |
|
|
|
|
|
|
|
if (spawnedObject == null) |
|
|
|
{ |
|
|
|
spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
spawnedObject.transform.position = hitPose.position; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update() |
|
|
|
{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>(); |
|
|
|
|
|
|
|
ARRaycastManager m_RaycastManager; |
|
|
|
} |
|
|
|
|
|
|
|
} |