Search Unity

Question Using Tracked Image Manager to make a button interactable

Discussion in 'AR' started by tfvwalkden, Jul 7, 2021.

  1. tfvwalkden

    tfvwalkden

    Joined:
    Jun 29, 2021
    Posts:
    1
    Hey!

    What I am basically trying to do is create a menu where I can select prefabs and place them on a plane, and I do have this working, but I want to make them unlock-able. So what I want is to be able to scan an image using the Image Tracker and then have it make a specific button interact-able.

    What I have so far is a working tracker, where it finds the correct object that corresponds with the right image. Is there a way I can change this to instead spawn the prefab but just make a specific button interact-able?

    This is what I have (sorry, I am new to this and not sure on the forum etiquette)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.XR.ARFoundation;
    6. using UnityEngine.XR;
    7.  
    8.  
    9. [RequireComponent(typeof(ARTrackedImageManager))]
    10. public class ImageTracking : MonoBehaviour
    11. {
    12.  
    13.     [SerializeField]
    14.     private GameObject[] placeablePrefabs;
    15.  
    16.     private Dictionary<string, GameObject> spawnedPrefabs = new Dictionary<string, GameObject>();
    17.     private ARTrackedImageManager trackedImageManager;
    18.  
    19.     private void Awake()
    20.     {
    21.         trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
    22.  
    23.         foreach (GameObject prefab in placeablePrefabs)
    24.         {
    25.             GameObject newPrefab = Instantiate(prefab, Vector3.zero, Quaternion.identity);
    26.             newPrefab.name = prefab.name;
    27.             spawnedPrefabs.Add(prefab.name, newPrefab);
    28.         }
    29.     }
    30.  
    31.     private void OnEnable()
    32.     {
    33.         trackedImageManager.trackedImagesChanged += ImageChanged;
    34.     }
    35.     private void OnDisable()
    36.     {
    37.         trackedImageManager.trackedImagesChanged -= ImageChanged;
    38.     }
    39.  
    40.     private void ImageChanged(ARTrackedImagesChangedEventArgs eventArgs)
    41.     {
    42.         foreach (ARTrackedImage trackedImage in eventArgs.added)
    43.         {
    44.             UpdateImage(trackedImage);
    45.         }
    46.         foreach (ARTrackedImage trackedImage in eventArgs.updated)
    47.         {
    48.             UpdateImage(trackedImage);
    49.         }
    50.         foreach (ARTrackedImage trackedImage in eventArgs.removed)
    51.         {
    52.             spawnedPrefabs[trackedImage.name].SetActive(false);
    53.         }
    54.     }
    55.  
    56.     private void UpdateImage(ARTrackedImage trackedImage)
    57.     {
    58.         string name = trackedImage.referenceImage.name;
    59.         Vector3 position = trackedImage.transform.position;
    60.  
    61.         GameObject prefab = spawnedPrefabs[name];
    62.         prefab.transform.position = position;
    63.         prefab.SetActive(true);
    64.  
    65.         foreach (GameObject go in spawnedPrefabs.Values)
    66.         {
    67.             if (go.name != name || trackedImage.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.None || trackedImage.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.Limited)
    68.             {
    69.                 go.SetActive(false);
    70.             }
    71.         }
    72.     }
    73.  
    74. }
    75.  
    76.  
    77.  
    78.  
    A bit of a backstory: I am a student animator, and have been hired by the uni to create an AR experience for new students on a campus orientation trail. They gave me full creative freedom so I have chosen to create an app in Unity. (They suggested Aero but that was not all that great for what I wanted to do, it was too unreliable for sharing). So I am new to this, but I really want to learn more code, and this has been very fun! If you can help me with this, that would be brilliant!

    Thank you!