Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

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

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);
    }

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...?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

enter image description here Img 1: Add existing prefab to game hierarchy enter image description here 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;
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...