Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Adaptation and use of PrefabImagePairManager script

Discussion in 'AR' started by Temp23457, Dec 5, 2023.

  1. Temp23457

    Temp23457

    Joined:
    Jan 28, 2022
    Posts:
    6
    Hello.

    I posted before about wanting to have a prefab and it's relevant UI loaded/deloaded when a reference image is scanned. I have now adapted the PrefabImagePairManager script to try and solve this and have linked it to a singleton that I have developed. In the PrefabImagePairManager script, the function OnTrackedImagesChanged has been adapted as follows:
    Code (CSharp):
    1. void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    2. {
    3.     foreach (var trackedImage in eventArgs.added)
    4.     {
    5.         // Give the initial image a reasonable default scale
    6.         var minLocalScalar = Mathf.Min(trackedImage.size.x, trackedImage.size.y) / 2;
    7.         trackedImage.transform.localScale = new Vector3(minLocalScalar, minLocalScalar, minLocalScalar);
    8.         AssignPrefab(trackedImage);
    9.     }
    10.     foreach (var trackedImage in eventArgs.updated)
    11.     {
    12.         if (m_Instantiated[trackedImage.referenceImage.guid])
    13.         {
    14.             if (trackedImage.trackingState == TrackingState.Limited) //TrackingState.Limited is called to test if an image is outside of the camera's point of view
    15.             {
    16.                 Debug.Log("Image lost");
    17.                 if (ARManager.instance.footballSpawned == true)
    18.                 {
    19.                     ARManager.instance.RemoveFootballContainer();
    20.                 }
    21.                 else if (ARManager.instance.cathedralSpawned == true)
    22.                 {
    23.                     ARManager.instance.RemoveCathedralContainer();
    24.                 }
    25.                 else if (ARManager.instance.laptopSpawned == true)
    26.                 {
    27.                     ARManager.instance.RemoveLaptopContainer();
    28.                 }
    29.             }
    30.             break;
    31.         }
    32.        
    33.         // Give the initial image a reasonable default scale
    34.         var minLocalScalar = Mathf.Min(trackedImage.size.x, trackedImage.size.y) / 2;
    35.         trackedImage.transform.localScale = new Vector3(minLocalScalar, minLocalScalar, minLocalScalar);
    36.         AssignPrefab(trackedImage);
    37.     }
    38. }
    Equally, the ARManager script that it refers to is detailed here:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.XR.ARFoundation.samples;
    6.  
    7. public class ARManager : MonoBehaviour
    8. {
    9.     public GameObject cathedralImage;
    10.     public GameObject laptopImage;
    11.     public GameObject footballImage;
    12.     public GameObject cathedralContainer;
    13.     public GameObject laptopContainer;
    14.     public GameObject footballContainer;
    15.     public bool footballSpawned;
    16.     public bool laptopSpawned;
    17.     public bool cathedralSpawned;
    18.     public Button cathedralButton;
    19.     public Button laptopButton;
    20.     public Button footballButton;
    21.     public PrefabImagePairManager prefabImagePairManager;
    22.     public static ARManager instance;
    23.     private void Awake()
    24.     {
    25.         if(instance != null && instance != this)
    26.         {
    27.             Destroy(this); //already have one in scene
    28.         }
    29.         else
    30.         {
    31.             instance = this;
    32.         }
    33.     }
    34.     private void Start()
    35.     {
    36.         footballSpawned = false;
    37.         laptopSpawned = false;
    38.         cathedralSpawned = false;
    39.         //footballButton.onClick.AddListener(() => RemoveFootballContainer());
    40.         //laptopButton.onClick.AddListener(() => RemoveLaptopContainer());
    41.         //cathedralButton.onClick.AddListener(() => RemoveCathedralContainer());
    42.     }
    43.     private void Update()
    44.     {
    45.         if (footballSpawned)
    46.         {
    47.             footballContainer.SetActive(true);
    48.         }
    49.         else if(laptopSpawned)
    50.         {
    51.             laptopContainer.SetActive(true);
    52.         }
    53.         else if(cathedralSpawned)
    54.         {
    55.             cathedralContainer.SetActive(true);
    56.         }
    57.     }
    58.  
    59.     public void RemoveFootballContainer()
    60.     {
    61.         footballSpawned = false;
    62.         Debug.Log(footballSpawned);
    63.         foreach(var gb in prefabImagePairManager.m_Instantiated)
    64.         {
    65.             if (gb.Value.gameObject.tag.Equals("Football"))
    66.             {
    67.                 Destroy(gb.Value.gameObject);
    68.             }
    69.         }
    70.     }
    71.     public void RemoveLaptopContainer()
    72.     {
    73.         laptopSpawned = false;
    74.         Debug.Log(laptopSpawned);
    75.         foreach (var gb in prefabImagePairManager.m_Instantiated)
    76.         {
    77.             if (gb.Value.gameObject.tag.Equals("Laptop"))
    78.             {
    79.                 Destroy(gb.Value.gameObject);
    80.             }
    81.         }
    82.     }
    83.     public void RemoveCathedralContainer()
    84.     {
    85.         cathedralSpawned = false;
    86.         Debug.Log(cathedralSpawned);
    87.         foreach (var gb in prefabImagePairManager.m_Instantiated)
    88.         {
    89.             if (gb.Value.gameObject.tag.Equals("Angel"))
    90.             {
    91.                 Destroy(gb.Value.gameObject);
    92.             }
    93.         }
    94.     }
    95. }
    The plan for this code is, when scanning a marker image, display the relevant marker prefab, as well as the text and button associated with the prefab as well (as each prefab has text related to it). Then, when this marker is scanned again, ideally the same prefab should spawn with the same information again. My current issue however is that it seems like I am getting a nullexceptionreference after scanning the object for the first time and then removing it from the scene. I am checking what prefab is assign and what prefab is deleted, and it seems to be that the prefab assigned in the inspector is the one assigning the prefab and the prefab deleted is a clone of this prefab. I'm not sure why I'm getting this error so I was hoping someone might be able to explain what is happening and potentially offer a better solution.
     
  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    956