其他分享
首页 > 其他分享> > AR Foundation(2),扫码显示两个模型

AR Foundation(2),扫码显示两个模型

作者:互联网

1.首先添加Reference Image Library,在Create->XR->Reference Image Library。

设置两个图片

2.在AR Session Origin中,添加AR Tracked Image Manager组件,并加入上一步设置的图片库

 

3.0添加新的脚本MultiImageTracking,如下


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;

public class MultiImageTracking : MonoBehaviour
{
    ARTrackedImageManager ImgTrackedManager;
    private Dictionary<string, GameObject> mPrefabs = new Dictionary<string, GameObject>();

    private void Awake()
    {
        ImgTrackedManager = GetComponent<ARTrackedImageManager>();
    }

    void Start()
    {
       
        mPrefabs.Add("kiss", Resources.Load("TVvideo") as GameObject);
        mPrefabs.Add("qr", Resources.Load("car") as GameObject);
    }

    private void OnEnable()
    {
        ImgTrackedManager.trackedImagesChanged += OnTrackedImagesChanged;
    }
    void OnDisable()
    {
        ImgTrackedManager.trackedImagesChanged -= OnTrackedImagesChanged;
    }

    void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    {
        foreach (var trackedImage in eventArgs.added)
        {
            OnImagesChanged(trackedImage);
        }

    }

    private void OnImagesChanged(ARTrackedImage referenceImage)
    {
        Debug.Log("Image name:" + referenceImage.referenceImage.name);
        Instantiate(mPrefabs[referenceImage.referenceImage.name], referenceImage.transform);
        



    }
}

3.1.也可以采取添加如下控件,并填入模型和图片库即可

4.打包安装,运行 ,扫码

参考(22条消息) ARFoundation之路-图像跟踪_DavidWang-CSDN博客

标签:Foundation,扫码,ImgTrackedManager,referenceImage,void,private,AR,using,mPrefabs
来源: https://blog.csdn.net/Chasder/article/details/120423997