Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

AR Multiple Image Tracking > Multiple Objects

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

  1. Bentoon

    Bentoon

    Joined:
    Apr 26, 2013
    Posts:
    76
    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
  2. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    17
    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. }
     
    nikolaijonassonart likes this.
  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. nikolaijonassonart

    nikolaijonassonart

    Joined:
    Jul 12, 2020
    Posts:
    2
    Thank you, but could you perhaps elaborate on the way the prefabs should be in the scene? Should they be hidden in the scene? In my case, I followed all of your instructions and the prefabs do get switched when switching between tracked images in real life, real-time, however, in most cases, these prefabs that don't apply to a specific image target will stay within the scene outside of the view, although glitching out.
     
    Last edited: Jul 13, 2020
  7. 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.
     
  8. ibrahimpenekli

    ibrahimpenekli

    Joined:
    Mar 10, 2015
    Posts:
    17
    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.
     
unityunity