Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question (ARTrackedObject) I want to call 「ActivateARObjectAdded」 again.

Discussion in 'AR' started by Enta_Hokama, Sep 19, 2023.

  1. Enta_Hokama

    Enta_Hokama

    Joined:
    Sep 12, 2022
    Posts:
    6
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         count = 0;
    4.         flg = false;
    5.         logcount = 0;
    6.         _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
    7.     }
    8.     private void Update()
    9.     {
    10.         if (_arPrefabs[2] != null && _objectManager.referenceLibrary.name == "assetMacker" && !flg)
    11.         {
    12.             flg = true;
    13.             kousin();
    14.         }
    15.     }
    16.     public void kousin()
    17.     {
    18.         referenceLibraryNameCheck = _objectManager.referenceLibrary;
    19.         for (var i = 0; i < _arPrefabs.Length; i++)
    20.         {
    21.             var arPrefab = Instantiate(_arPrefabs[i]);
    22.             _markerNameAndPrefabDictionary.Add(_objectManager.referenceLibrary[i].name, arPrefab);
    23.             arPrefab.SetActive(false);
    24.         }
    25.     }
    26.     public void OnReset()
    27.     {
    28.         Scene loadScene = SceneManager.GetActiveScene();
    29.         SceneManager.LoadScene(loadScene.name);
    30.     }
    31.     private void OnEnable()
    32.     {
    33.         _objectManager.trackedObjectsChanged += OnTrackedObjectsChanged;
    34.     }
    35.     private void OnDisable()
    36.     {
    37.         _objectManager.trackedObjectsChanged -= OnTrackedObjectsChanged;
    38.     }
    39.  
    40.     private void ActivateARObjectAdded(ARTrackedObject trackedObject)
    41.     {
    42.         var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
    43.         var objectMarkerTransform = trackedObject.transform;
    44.         var markerFrontRotation = objectMarkerTransform.rotation * Quaternion.Euler(0f, 0f, 0f);
    45.         arObject.transform.SetPositionAndRotation(objectMarkerTransform.transform.position, markerFrontRotation);
    46.         arObject.transform.SetParent(objectMarkerTransform);
    47.  
    48.         if (trackedObject.trackingState == TrackingState.Tracking && arObject.activeSelf == false)
    49.         {
    50.             arObject.SetActive(true);
    51.         }
    52.     }
    53.     private void ActivateARObjectUpdated(ARTrackedObject trackedObject)
    54.     {
    55.         var arObject = _markerNameAndPrefabDictionary[trackedObject.referenceObject.name];
    56.         var objectMarkerTransform = trackedObject.transform;
    57.         var markerFrontRotation = objectMarkerTransform.rotation * Quaternion.Euler(0f, 0f, 0f);
    58.         arObject.transform.SetPositionAndRotation(objectMarkerTransform.transform.position, markerFrontRotation);
    59.         arObject.transform.SetParent(objectMarkerTransform);
    60.     }
    61.  
    62.     private void OnTrackedObjectsChanged(ARTrackedObjectsChangedEventArgs eventArgs)
    63.     {
    64.         eeventArgs = eventArgs;
    65.         foreach (var trackedObject in eeventArgs.added)
    66.         {
    67.             addCount++;
    68.             ActivateARObjectAdded(trackedObject);
    69.  
    70.         }
    71.         foreach (var trackedObject in eeventArgs.updated)
    72.         {
    73.             updateCount++;
    74.             ActivateARObjectUpdated(trackedObject);
    75.         }
    76.         foreach (var trackedObject in eeventArgs.removed)
    77.         {
    78.             removeCount++;
    79.         }
    80.     }
    81. }
    82.  
    I call for a 「OnReset()」 and then again for a 「ActivateARObjectAdded」.
    However, only a 「ActivateARObjectUpdated」 is called.
    What can I do?