using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.XR.ARFoundation; /// /// This example demonstrates how to toggle plane detection, /// and also hide or show the existing planes. /// [RequireComponent(typeof(ARPlaneManager))] public class PlaneDetectionController : MonoBehaviour { [Tooltip("The UI Text element used to display plane detection messages.")] [SerializeField] Text m_TogglePlaneDetectionText; /// /// The UI Text element used to display plane detection messages. /// public Text togglePlaneDetectionText { get { return m_TogglePlaneDetectionText; } set { m_TogglePlaneDetectionText = value; } } /// /// Toggles plane detection and the visualization of the planes. /// public void TogglePlaneDetection() { m_ARPlaneManager.enabled = !m_ARPlaneManager.enabled; string planeDetectionMessage = ""; if (m_ARPlaneManager.enabled) { planeDetectionMessage = "Disable Plane Detection and Hide Existing"; SetAllPlanesActive(true); } else { planeDetectionMessage = "Enable Plane Detection and Show Existing"; SetAllPlanesActive(false); } if (togglePlaneDetectionText != null) togglePlaneDetectionText.text = planeDetectionMessage; } /// /// Iterates over all the existing planes and activates /// or deactivates their GameObjects'. /// /// Each planes' GameObject is SetActive with this value. void SetAllPlanesActive(bool value) { m_ARPlaneManager.GetAllPlanes(s_Planes); foreach (var plane in s_Planes) plane.gameObject.SetActive(value); } void Awake() { m_ARPlaneManager = GetComponent(); } ARPlaneManager m_ARPlaneManager; static List s_Planes = new List(); }