/// <summary> /// Uninitialize and clean up AR Foundation components. /// </summary> private void UninitializeARFoundation() { if (!isInitialized) { return; } if (!preExistingArSessionOriginObject && (arSessionOriginObject != null)) { UnityObjectExtensions.DestroyObject(trackedPoseDriver); trackedPoseDriver = null; UnityObjectExtensions.DestroyObject(arCameraBackground); arCameraBackground = null; UnityObjectExtensions.DestroyObject(arCameraManager); arCameraManager = null; UnityObjectExtensions.DestroyObject(arSessionOrigin); arSessionOrigin = null; } if (!preExistingArSessionObject && (arSessionObject != null)) { UnityObjectExtensions.DestroyObject(arInputManager); arInputManager = null; UnityObjectExtensions.DestroyObject(arSession); arSession = null; UnityObjectExtensions.DestroyObject(arSessionObject); arSessionObject = null; } isInitialized = false; }
/// <summary> /// Initialize AR Foundation components. /// </summary> /// <remarks> /// This method ensures AR Foundation required components (ex: AR Session, Tracked Pose Driver, etc) are /// exist or are added to the appropriate scene objects. These components are used by AR Foundation to /// communicate with the underlying AR platform (ex: AR Core), track the device and perform other necessary tasks. /// </remarks> private void InitializeARFoundation() { if (!isSupportedArConfiguration) { return; } if (isInitialized) { return; } FindARFoundationComponents(); if (arSessionObject == null) { arSessionObject = new GameObject("AR Session"); arSessionObject.transform.parent = null; } arSession = arSessionObject.EnsureComponent <ARSession>(); arInputManager = arSessionObject.EnsureComponent <ARInputManager>(); if (arSessionOriginObject == null) { arSessionOriginObject = MixedRealityPlayspace.Transform.gameObject; } CameraCache.Main.transform.parent = arSessionOriginObject.transform; arSessionOrigin = arSessionOriginObject.EnsureComponent <ARSessionOrigin>(); arSessionOrigin.camera = CameraCache.Main; GameObject cameraObject = arSessionOrigin.camera.gameObject; arCameraManager = cameraObject.EnsureComponent <ARCameraManager>(); arCameraBackground = cameraObject.EnsureComponent <ARCameraBackground>(); trackedPoseDriver = cameraObject.EnsureComponent <TrackedPoseDriver>(); trackedPoseDriver.SetPoseSource( TrackedPoseDriver.DeviceType.GenericXRDevice, ArEnumConversion.ToUnityTrackedPose(poseSource)); trackedPoseDriver.trackingType = ArEnumConversion.ToUnityTrackingType(trackingType); trackedPoseDriver.updateType = ArEnumConversion.ToUnityUpdateType(updateType); trackedPoseDriver.UseRelativeTransform = false; isInitialized = true; }