using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems; [RequireComponent(typeof(ARRaycastManager))] public class PlaceMultipleObjectsOnPlane : MonoBehaviour { [SerializeField] [Tooltip("Instantiates this prefab on a plane at the touch location.")] GameObject m_PlacedPrefab; /// /// The prefab to instantiate on touch. /// public GameObject placedPrefab { get { return m_PlacedPrefab; } set { m_PlacedPrefab = value; } } /// /// The object instantiated as a result of a successful raycast intersection with a plane. /// public GameObject spawnedObject { get; private set; } /// /// Invoked whenever an object is placed in on a plane. /// public static event Action onPlacedObject; ARRaycastManager m_RaycastManager; static List s_Hits = new List(); void Awake() { m_RaycastManager = GetComponent(); } void Update() { if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon)) { Pose hitPose = s_Hits[0].pose; spawnedObject = Instantiate(m_PlacedPrefab, hitPose.position, hitPose.rotation); if (onPlacedObject != null) { onPlacedObject(); } } } } } }