ARCore + Unity + Augmented Images - Load different prefabs for different Images

走远了吗. 提交于 2019-11-29 05:19:38
Itsme

I assign different prefabs to different images by this way:

  • I modified the AugmentedImageExampleController.cs:.
  • I added a list for prefabs:

public List<AugmentedImageVisualizer> prefabs = new List<AugmentedImageVisualizer>();

  • For the related image for the prefab I did a reference by using the image.DatabaseIndex in the visualizer:

visualizer = (AugmentedImageVisualizer)Instantiate(prefabs[image.DatabaseIndex], anchor.transform);

In the inspector of ExampleController you can put in the prefabs (AugmentedImageVisualizer) now.

That's it, and its working fine!

You could change your AugmentedImageVisualizer prefab, so that at the start all your different objects are inactive (.SetActive(false);) For changing a prefab you can add it to your hierarchy, set all the objects inactive and then apply the changes. After apply you can delete the prefab from your game hierarchy.

Img 1: Add existing prefab to game hierarchy

Img 2: set all the attached objecty to inactive (1) and apply the changes to the prefab (2)

So when you detect a image from your imagedatabase the AugmentedImageVisualizer prefab is attached and only the object with the given index is set to active. Then your code should work.

Because at the start all your objects are inactive you could change this part of your code:

if (Image == null || Image.TrackingState != TrackingState.Tracking)
    {
        obj1.SetActive(false);
        obj2.SetActive(false);
        obj3.SetActive(false);
        obj4.SetActive(false);
        return;
    }

so that you only deactivate the active object:

if (Image.TrackingState != TrackingState.Tracking)
        {
            imagePrefabPairs[Image.DatabaseIndex].SetActive(false);
            return;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!