使用Unity在ARfoundation 2.1中进行图像跟踪无法正常工作?

时间:2019-06-26 08:49:11

标签: c# android ios unity3d

我有一组图像,对于这些图像,我需要显示不同的预制件。在这个示例中,我使用了两个图像和两个预制件。对于imagetarget1,我想显示prefab1;对于imagetarget2我想显示prefab2。

密封类ARTrackedImageManager的游戏对象部分如下所示。

[SerializeField]
    [Tooltip("If not null, instantiates this prefab for each detected image.")]
    GameObject m_TrackedImagePrefab;

    /// <summary>
    /// If not null, instantiates this prefab for each detected image.
    /// </summary>
    public GameObject trackedImagePrefab
    {
        get { return m_TrackedImagePrefab; }
        set { m_TrackedImagePrefab = value; }
    }

    protected override GameObject GetPrefab()
    {
        return m_TrackedImagePrefab;
    }

    /// <summary>
    /// Invoked once per frame with information about the <see cref="ARTrackedImage"/>s that have changed, i.e., been added, updated, or removed.
    /// This happens just before <see cref="ARTrackedImage"/>s are destroyed, so you can set <c>ARTrackedImage.destroyOnRemoval</c> to <c>false</c>
    /// from this event to suppress this behavior.
    /// </summary>
    public event Action<ARTrackedImagesChangedEventArgs> trackedImagesChanged;

我创建了另一个类来加载两个可以在设备检测到图像时显示的预制件。

public class ImageTest : MonoBehaviour
{
ARTrackedImageManager ImgTrackedmanager;
public  GameObject[] Showprefabs;
List<GameObject> listobjects = new List<GameObject>();
int visiblity = 0;

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

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    if(visiblity==1)
    {
        Showprefabs[0].SetActive(true);
        Showprefabs[1].SetActive(false);

    }
    if(visiblity==2)
    {
        ImgTrackedmanager.trackedImagePrefab = null;
        Showprefabs[0].SetActive(false);
        Showprefabs[1].SetActive(true);

    }

}


private void OnEnable()
{
    ImgTrackedmanager.trackedImagesChanged += OnTrackedImagesChanged;
}


void OnDisable()
{
    ImgTrackedmanager.trackedImagesChanged -= OnTrackedImagesChanged;
}



void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
    foreach (var trackedImage in eventArgs.added)
    {
        // Give the initial image a reasonable default scale
        //trackedImage.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);

       // Debug.Log("Image Name is .."+trackedImage.gameObject.name);
        Debug.Log("Tracked Name is .." + trackedImage.referenceImage.name);


        if(trackedImage.referenceImage.name == "imagetargets_1")
        {
            if(ImgTrackedmanager.trackedImagePrefab == null)
            {
                GameObject obj1 = Instantiate(Showprefabs[0], trackedImage.transform);
                listobjects.Add(obj1);
                ImgTrackedmanager.trackedImagePrefab = obj1;
                visiblity = 1;

                Debug.Log("Prefab1");

            }



        }
        if(trackedImage.referenceImage.name == "imagetargets_2")
        {

            Destroy(listobjects[0]);

                GameObject obj2 = Instantiate(Showprefabs[1], trackedImage.transform);
                ImgTrackedmanager.trackedImagePrefab = obj2;
                Debug.Log("Prefab2");
                visiblity = 2;


        }

    }
}

}

这里,当它检测到第一个映像时,xcode中的调试语句显示为'Prefab1',而对于第二个映像,则显示为'Prefab2'。问题是当设备显示给第二个映像时,再次实例化了第一个预制件。不明白为什么要实例化添加到中的第一个预制件。

ImgTrackingArfoundation

0 个答案:

没有答案