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

查看:234
本文介绍了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 :。

  • 我添加了一个预制件列表:

公共列表< AugmentedImageVisualizer> prefabs = new List< AugmentedImageVisualizer>();


  • 对于预制件的相关图像我做了参考通过在可视化器中使用 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);

在<$ c的检查器中$ c> 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天全站免登陆