Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AR Multiple Image Tracking > Multiple Objects

Discussion in 'AR' started by Bentoon, Mar 15, 2020.

Thread Status:
Not open for further replies.
  1. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    98
    Hi
    Using XR interaction toolkit & AR foundations
    I want a variety of images trigger their own specific objects
    Right now I can only have a few images in an XR reference Library spawn the same prefab

    only One object to be triggered by the AR Tracked image Manager (and you can only have one instance of this script of on the camera)...

    but I have 10 prefabs I want to be triggered from 10 different images..

    Crazy right?

    Any ideas ? I am on iOS
    Thanks
    ~be
     
    Last edited: Mar 15, 2020
    Roman_Keivan likes this.
  2. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    30
    Add following script to your game objects. Enter reference image name for each different objects.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.XR.ARSubsystems;
    7.  
    8. public class ARImageAnchor : MonoBehaviour
    9. {
    10.     public string ReferenceImageName;
    11.     private ARTrackedImageManager _TrackedImageManager;
    12.  
    13.     private void Awake()
    14.     {
    15.         _TrackedImageManager = FindObjectOfType<ARTrackedImageManager>();
    16.     }
    17.  
    18.     private void OnEnable()
    19.     {
    20.         if (_TrackedImageManager != null)
    21.         {
    22.             _TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
    23.         }
    24.     }
    25.  
    26.     private void OnDisable()
    27.     {
    28.         if (_TrackedImageManager != null)
    29.         {
    30.             _TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
    31.         }
    32.     }
    33.  
    34.     private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs e)
    35.     {
    36.         foreach (var trackedImage in e.added)
    37.         {
    38.             Debug.Log($"Tracked image detected: {trackedImage.referenceImage.name} with size: {trackedImage.size}");
    39.         }
    40.  
    41.         UpdateTrackedImages(e.added);
    42.         UpdateTrackedImages(e.updated);
    43.     }
    44.  
    45.     private void UpdateTrackedImages(IEnumerable<ARTrackedImage> trackedImages)
    46.     {
    47.         // If the same image (ReferenceImageName)
    48.         var trackedImage =
    49.             trackedImages.FirstOrDefault(x => x.referenceImage.name == ReferenceImageName);
    50.         if (trackedImage == null)
    51.         {
    52.             return;
    53.         }
    54.      
    55.         if (trackedImage.trackingState != TrackingState.None)
    56.         {
    57.             var trackedImageTransform = trackedImage.transform;
    58.             transform.SetPositionAndRotation(trackedImageTransform.position, trackedImageTransform.rotation);  
    59.         }
    60.     }
    61. }
     
  3. VectorArch

    VectorArch

    Joined:
    May 7, 2019
    Posts:
    1
    Thank you for posting the code @ibrahimpenekli. I am quite a noob at Unity and don't get it to run.

    I added your code to the prefabs inside of my asset folder and added the reference image which should trigger the object.
    The AR Session Origin has the AR Tracked Image Manager attached. The correct Library is attached, 2 Moving Images and I left the Tracked Image Prefab empty.

    Do the Prefabs have to be active in the scene? Is it a problem with android?
     
  4. Serenaabraham

    Serenaabraham

    Joined:
    Feb 4, 2020
    Posts:
    2
  5. Serenaabraham

    Serenaabraham

    Joined:
    Feb 4, 2020
    Posts:
    2
    Set up your regular AR scene, keep the library, but keep the tracked prefab empty.
    Then move your prefabs into the game scene. Attach the script to them and type out the exact name of the reference image you want to trigger that specific prefab. Do this for every prefab to have them toggle when called.
     
    nikolaijonassonart likes this.
  6. kageori

    kageori

    Joined:
    Jul 11, 2019
    Posts:
    1
    Thank you @ibrahimpenekli! It worked.

    I have a question...
    Can I remove the object when the marker disappear?
    With this code, the objects remain the same place after I hide the marker.
     
  7. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    30
    You're welcome @kageori

    Actually, AR system does not remove any tracked image if you don't remove intentionally. You can setup a timer to check if tracking state is
    TrackingState.None
    for a specified time interval (e.g. 1-3 sec). If so, you can disable the game object or renderers & colliders.
     
  8. Fish_sandwich

    Fish_sandwich

    Joined:
    Dec 11, 2020
    Posts:
    1
    For some reason, this isn't working. I've got the prefabs in the game scene, and in my assets folder. The scripts are attached, and I've replaced the names with the exact case sensitive name that they have in the project. When I build it out to iOS, and point at one of the images, it doesn't show.

    The tracked Prefab is empty in the Tracked Image Manager. The reference image library has all of my images loaded in.
     
    Last edited: Dec 11, 2020
  9. emmitt67

    emmitt67

    Joined:
    Mar 23, 2021
    Posts:
    4
    Hi, I tried to add this to my file and it was not working. Is there any way to do this without resorting to code, vuforia, or wikitude?
     
  10. SpectreLast

    SpectreLast

    Joined:
    Nov 30, 2021
    Posts:
    1
    The Macbook Air M1 I purchased to build Unity iOS AR was saving the Unity project (etc?) to the cloud, and those files seem to be in a constant state of uploading/updating... when I saved the project locally I stopped getting architecture ARM64 error.
     
Thread Status:
Not open for further replies.