ARCore + Unity + 增强图像 - 为不同的图像加载不同的预制件 [英] ARCore + Unity + Augmented Images - Load different prefabs for different Images

查看:27
本文介绍了ARCore + Unity + 增强图像 - 为不同的图像加载不同的预制件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为不同的图像分配不同的预制件?

How do I assign different prefabs to different images?

现在,我的所有预制件都在彼此之上加载,但我如何获得它以便每个预制件仅在一张图像上加载一次,以便每个图像都有不同的预制件?

right now, I have all my prefabs loading in on top of each other but how do I get it so each prefab loads in only once on top of one image so each image has a different prefab?

我修改了示例代码(框架角)以加载到我自己的预制件中,并使用字典将预制件与数据库中的图像配对,但是当程序运行时,它会将所有预制件安装在同一位置而不是在每张图像上放置一个预制件 它将所有预制件放在每张图像上 - 这是我一直在使用的代码:

I've modified the sample code (the frame corners) to load in my own prefab and used a dictionary to pair the prefabs with images from the database but when the program runs it instatiates all the prefabs in the same place rather than putting one prefrab on each image it puts all the prefabs on every image - this is the code I've been using:

        public GameObject obj1, obj2, obj3, obj4;
    Dictionary<int, GameObject> imagePrefabPairs;
    public AugmentedImage Image;

    void Start()
    {
        instantiateDictionary();
    }

    // TODO: make initialisation dynamic, to match the size of the db.
    public void instantiateDictionary()
    {
        imagePrefabPairs = new Dictionary<int, GameObject>
        {
            { 0, obj1 },
            { 1, obj2 },
            { 2, obj3 },
            { 3, obj4 }
        };
    }

    public void Update()
    {
        if (Image == null || Image.TrackingState != TrackingState.Tracking)
        {
            obj1.SetActive(false);
            obj2.SetActive(false);
            obj3.SetActive(false);
            obj4.SetActive(false);
            return;
        }
        Pose _centerPose = Image.CenterPose;
        imagePrefabPairs[Image.DatabaseIndex].transform.localPosition = _centerPose.position;
        imagePrefabPairs[Image.DatabaseIndex].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
        imagePrefabPairs[Image.DatabaseIndex].SetActive(true);
    }

我认为我需要某种 if 语句来询问是否加载了一个预制件,然后选择加载下一个预制件并让它们一次实例化一个,但我不知道该怎么做,或者是否有更直接的方法来实现...?

I figure that I need to have some kind of if statement to ask if one prefab is loaded in and then just choose to load in the next one and have them instantiate one at a time but I am not sure how to do that, or if there is a more direct way to make that happen...?

推荐答案

我通过这种方式为不同的图像分配不同的预制件:

I assign different prefabs to different images by this way:

  • 我修改了 AugmentedImageExampleController.cs:.
  • 我添加了一个预制件列表:

公共列表prefabs = new List();

  • 对于预制件的相关图像,我使用可视化工具中的 image.DatabaseIndex 做了一个参考:
  • 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);

ExampleController 的检查器中,您现在可以放入预制件 (AugmentedImageVisualizer).

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

就是这样,它工作正常!

That's it, and its working fine!

这篇关于ARCore + Unity + 增强图像 - 为不同的图像加载不同的预制件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆