Search Unity

ARFoundation - Get images' clear names out of ReferenceImageLibrary

Discussion in 'AR/VR (XR) Discussion' started by TRS_ZueStra, May 21, 2019.

  1. TRS_ZueStra

    TRS_ZueStra

    Joined:
    Mar 6, 2019
    Posts:
    9
    Hi everyone,

    I'm playing around with the Image Tracking functionality of ARFoundation. My goal is currently to instantiate different Prefabs by different images -> image1 instantiates prefab1, image2 instantiates prefab2 and so on...
    For this I edited the scripts based on a post from this link:
    https://forum.unity.com/threads/arfoundation-and-2d-image-target-tracking.538062/page-3#post-4556173

    But for loading the Prefab I don't want to use the trackableId but the clear name of the image which is written in the library (see screenshot) like this:
    Code (CSharp):
    1.  protected override GameObject GetPrefab(string name)
    2.         {
    3.             var image = m_ReferenceImages[name];
    4.             string nameClear = image.name.ToString();
    5.  
    6.             switch (nameClear)
    7.             {
    8.                 case "Rafflesia":
    9.                     return m_TrackedImagePrefab;
    10.                     break;
    11.                 case "QRCode":
    12.                     return m_TrackedImagePrefab2;
    13.                     break;
    14.                 default:
    15.                     return null;
    16.                     break;
    17.             }
    18.             //return m_TrackedImagePrefab;
    19.         }
    Is there a way to do that?
    Thanks a lot!

    Cheers
     

    Attached Files:

  2. LorenzoValente

    LorenzoValente

    Joined:
    May 2, 2019
    Posts:
    37
    Here's how I did it. Create a new MonoBehaviour and add it as a component near your ARTrackedImageManager:
    Code (CSharp):
    1. private void Awake()
    2. {
    3.     var arTrackedImageManager = gameObject.GetComponent<ARTrackedImageManager>();
    4.     arTrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
    5. }
    6.  
    7. private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs ev)
    8. {
    9.     // Check the new tracked images
    10.     foreach (ARTrackedImage image in ev.added)
    11.     {
    12.         XRReferenceImage refImage = image.referenceImage;
    13.         string imgName = refImage.name; // this is the name in the library
    14.         GameObject prefab = myPrefabsDictionary[imgName];
    15.         Instantiate(prefab, image.transform);
    16.     }
    17. }
     
    Last edited: May 23, 2019
  3. Software-Manager

    Software-Manager

    Joined:
    Mar 10, 2014
    Posts:
    3
    what kind of variable?
    myPrefabsDictionary
     
  4. LorenzoValente

    LorenzoValente

    Joined:
    May 2, 2019
    Posts:
    37
    It's a custom
    Dictionary<string, GameObject>
    you can populate in any previous step, just use the library images names as keys.
     
  5. LorenzoValente

    LorenzoValente

    Joined:
    May 2, 2019
    Posts:
    37
    My previous code does not seem to work with the 3.x version of the Foundation.
    Is anyone having the same problem? From my tests the
    image.referenceImage
    does not return a valid
    XRReferenceImage
    anymore
     
  6. LorenzoValente

    LorenzoValente

    Joined:
    May 2, 2019
    Posts:
    37
    ... ok it works now, the Foundation is just damn weird sometimes.
    I deleted the library, closed the editor, opened it, and created the library again