kadyr

Untitled

Dec 3rd, 2021
731
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.XR.ARFoundation;
  3. using UnityEngine.XR.ARSubsystems;
  4. using System.Collections;
  5.  
  6. namespace Ninsar.AR
  7. {
  8.     public class ARController : MonoBehaviour
  9.     {
  10.         public ARSessionOrigin ARSessionOrigin;
  11.         public ARTrackedImageManager TrackedImageManager;
  12.         private bool isTracked = false;
  13.         public Transform Content;
  14.  
  15.         private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs args)
  16.         {
  17.             foreach (var trackedImage in args.updated)
  18.             {
  19.                 if (trackedImage.trackingState != TrackingState.Tracking) continue;
  20.  
  21.                 ARSessionOrigin.MakeContentAppearAt(
  22.                     Content, trackedImage.transform.position, trackedImage.transform.localRotation
  23.                 );
  24.                 Content.gameObject.SetActive(true);
  25.                 if(isTracked == false)
  26.                     StartCoroutine(sessionDisable());
  27.                 break;
  28.             }
  29.         }
  30.         private IEnumerator sessionDisable()
  31.         {
  32.             isTracked = true;
  33.             yield return new WaitForSeconds(2f);
  34.             TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
  35.         }
  36.  
  37.         private void OnEnable()
  38.         {
  39.             TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
  40.             Content.gameObject.SetActive(false);
  41.         }
  42.  
  43.         private void OnDisable()
  44.         {
  45.             TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
  46.         }
  47.     }
  48. }
RAW Paste Data