Remove prefab when image is out of view

Include the following details (edit as applicable):

  • Issue category: ARDK Documentation / Unity Example Package
  • Device type & OS version: Android
  • Host machine & OS version: Windows
  • Issue Environment : On Device
  • Xcode version:
  • ARDK version: v2.0.0
  • Unity version: 2020.3.30f1

Description of the issue:

I’m using the Image Detection Example Manager for my project. How does one go about removing the instantiated prefab when the image is out of the camera’s view?

I tried saving the plane as a global variable and removing it using Destroy() under OnAnchorsRemoved(), which did not work.

GameObject newPlane;

private void OnAnchorsAdded(AnchorsArgs args)
    {
      foreach (var anchor in args.Anchors)
      {
        if (anchor.AnchorType != AnchorType.Image)
          continue;

        var imageAnchor = (IARImageAnchor) anchor;
        var imageName = imageAnchor.ReferenceImage.Name;

        newPlane = Instantiate(_plane);
        // instantiated = newPlane;
        newPlane.name = "Image-" + imageName;
        SetPlaneColor(newPlane, imageName);
        _detectedImages[anchor.Identifier] = newPlane;

        UpdatePlaneTransform(imageAnchor);
      }
    }

    private void OnAnchorsUpdated(AnchorsArgs args)
    {
      foreach (var anchor in args.Anchors)
      {
        if (!_detectedImages.ContainsKey(anchor.Identifier))
          continue;

        var imageAnchor = anchor as IARImageAnchor;
        UpdatePlaneTransform(imageAnchor);
      }
    }

    private void OnAnchorsRemoved(AnchorsArgs args)
    {
      foreach (var anchor in args.Anchors)
      {
        if (!_detectedImages.ContainsKey(anchor.Identifier))
          continue;

        Destroy(_detectedImages[anchor.Identifier]);
        
        // destroyPlane();
        _detectedImages.Remove(anchor.Identifier);
        Destroy(newPlane);
        
      }
    }

Hello Wei,

If I am understanding your post correctly, you are under the impression that an anchor no longer being in camera space will call OnAnchorsRemoved and delete the plane.

OnAnchorsRemoved is called when an object the anchor is being placed on is no longer there. The example code detects an image and when it does, it places a red block over it which is the anchor. That anchor is meant to persist even after the camera moves away from it.

When running the example, you can enable the Yeti image which means that when you hover the camera over the included Yeti image in the Unity project, it is detected and the plane anchor is placed over it. The example also allows you to disable the Yeti image (as if the object was not there) to show that the anchor is also removed and that is when OnAnchorsRemoved would be called.

In order to achieve the effect you’re looking for, I would recommend looking into OnBecameInvisible which is a Unity callback function. You can place your removal code within the OnBecameInsivible() callback in order to remove the instantiated prefab when the image is out of the camera’s view.

1 Like