Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question ARFoundation Clone the "Image Tracked Prefab" and leave in current state or position

Discussion in 'Scripting' started by ngajason, May 12, 2024.

  1. ngajason


    Jun 18, 2021
    Hi I'm hoping to get some clarity and help for an Android App I am building where I generate an AR heatmap. I will start by saying this is likely not the best method, but I will share where I'm at in case it helps. Thanks in advance if you are able to help!

    TLDR: I am trying to produce a heatmap effect where I use ARFoundation Track Image Manager to produce the tracked prefab then I would like to generate a clone and leave it at its current position with a button click event and have it store the clones in a empty Game Object called "HeatMapPointHolder" that is parented to the AR camera. My thought was that it would be a good idea to parent the clones to the ARCamera to keep them in their locations at the time the button was clicked. One of my biggest issues is, how do I instantiate a clone of a prefab that hasn't been instantiated at start.

    If you know a method that will work, Im all ears. This is where Im at currently:

    I am able to properly track the image and produce the prefab, but I don't have a clue how to access the instantiated prefab- from Tracked Image Manager that is being displayed on the image. It seems that the prefab should be called m_TrackedImagePrefab so I applied a script to the session origin (called Marker Manager) to try and invoke a clone function from a script (I called CloneSphere) but its not working when I attempt to run a GameObject.Find("m_TrackedImagePrefab")

    Here is my hierarchy:
    Here is the inspector for the Session Origin:
    Here is the button inspector:
    Here is the Marker Manager script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.ARFoundation;
    3. using UnityEngine.UI;
    4. using UnityEngine.Events;
    6. public class MarkerManager : MonoBehaviour
    7. {
    8.     public ARTrackedImageManager m_TrackedImageManager;
    9.     public Text txt;
    10.     public UnityEvent order;
    12.     private void OnEnable()
    13.     {
    14.         m_TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
    15.     }
    17.     private void OnDisable()
    18.     {
    19.         m_TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
    20.     }
    22.     void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    23.     {
    24.         foreach (var trackedImage in eventArgs.added)
    25.         order.Invoke();
    26.         }
    27.     }
    Here is the script called CloneSphere:
    Code (CSharp):
    1. using Unity.Collections;
    2. using UnityEngine;
    3. using UnityEngine.XR.ARFoundation;
    4. using UnityEngine.XR.ARSubsystems;
    6. public class CloneSphere : MonoBehaviour
    7. {
    8.     public GameObject heatMapSphere;
    9.     public GameObject heatMapSphereClone;
    10.     public Transform heatMapPointHolder;
    11.     // Start is called before the first frame update
    12.     private void Update()
    13.     {
    15.     }
    16.     public void CloneHeatMapSphere()
    17.     {
    18.         heatMapSphere = GameObject.Find("m_TrackedImagePrefab");
    19.         heatMapSphereClone = Instantiate(heatMapSphere, heatMapPointHolder) as GameObject;
    20.         heatMapSphereClone.transform.parent = heatMapPointHolder;
    21.     }
    22. }
    My button calls (On Click Event): MarkerManager.enabled
  2. zulo3d


    Feb 18, 2023
    The guys over here can help you.