Привет. Я пытаюсь расширить различные префабы для разных изображений, скажем, около 20 моделей. В настоящее время проводится тестирование с 2 моделями для 2 изображений в образце сцены AugmentedImage. Я добавил скрипт AugmentedImageVisualizer.cs к каждому префабу.модели в сценарии. В AugmenetedImageExampleController.cs я внес следующие изменения:
namespace GoogleARCore.Examples.AugmentedImage
{
using System.Collections.Generic;
using System.Runtime.InteropServices;
using GoogleARCore;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Controller for AugmentedImage example.
/// </summary>
public class AugmentedImageExampleController : MonoBehaviour
{
/// <summary>
/// A prefab for visualizing an AugmentedImage.
/// </summary>
// public AugmentedImageVisualizer AugmentedImageVisualizerPrefab;
public List<AugmentedImageVisualizer> AugmentedImageVisualizerPrefab = new List<AugmentedImageVisualizer>();
/// <summary>
/// The overlay containing the fit to scan user guide.
/// </summary>
public GameObject FitToScanOverlay;
private Dictionary<int, AugmentedImageVisualizer> m_Visualizers
= new Dictionary<int, AugmentedImageVisualizer>();
private List<AugmentedImage> m_TempAugmentedImages = new List<AugmentedImage>();
/// <summary>
/// The Unity Update method.
/// </summary>
public void Update()
{
// Exit the app when the 'back' button is pressed.
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
// Check that motion tracking is tracking.
if (Session.Status != SessionStatus.Tracking)
{
return;
}
// Get updated augmented images for this frame.
Session.GetTrackables<AugmentedImage>(m_TempAugmentedImages, TrackableQueryFilter.Updated);
// Create visualizers and anchors for updated augmented images that are tracking and do not previously
// have a visualizer. Remove visualizers for stopped images.
foreach (var image in m_TempAugmentedImages)
{
AugmentedImageVisualizer visualizer = null;
m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);
if (image.TrackingState == TrackingState.Tracking && visualizer == null)
{
// Create an anchor to ensure that ARCore keeps tracking this augmented image.
Anchor anchor = image.CreateAnchor(image.CenterPose);
visualizer = (AugmentedImageVisualizer)Instantiate(AugmentedImageVisualizerPrefab[image.DatabaseIndex], anchor.transform);
visualizer.Image = image;
m_Visualizers.Add(image.DatabaseIndex, visualizer);
}
else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
{
m_Visualizers.Remove(image.DatabaseIndex);
GameObject.Destroy(visualizer.gameObject);
}
}
// Show the fit-to-scan overlay if there are no images that are Tracking.
foreach (var visualizer in m_Visualizers.Values)
{
if (visualizer.Image.TrackingState == TrackingState.Tracking)
{
FitToScanOverlay.SetActive(false);
return;
}
}
FitToScanOverlay.SetActive(true);
}
}
}
Мой экран единства выглядит следующим образом: ![Unity screen view](https://i.stack.imgur.com/rSOkw.jpg)
Добавлено увеличенное изображениеСкрипт визуализатора для префабов для префабов Кролика и префаба Обезьяны. Изображение дано ниже ![Prefab-Inspector](https://i.stack.imgur.com/H67ED.jpg)
Вот как это должно быть сделано? Проблема, когда модель появляется, не исчезнет.Когда я показываю следующее изображение, поверх него появляется модель пыльника. Как скрыть модель, когда изображение не отслеживается?
В AugmentedImageControllerExample.cs мы используем приведенный ниже код. До сих пор я не понимаю, почемумодели не исчезают после того, как они потеряли отслеживание изображения.
else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
{
m_Visualizers.Remove(image.DatabaseIndex);
GameObject.Destroy(visualizer.gameObject);
}
AugmentedImageVКод isualizer.cs, приведенный ниже? Я ссылался на это Ссылка .
namespace GoogleARCore.Examples.AugmentedImage
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using GoogleARCore;
using GoogleARCoreInternal;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Uses 4 frame corner objects to visualize an AugmentedImage.
/// </summary>
public class AugmentedImageVisualizer : MonoBehaviour
{
/// <summary>
/// The AugmentedImage to visualize.
/// </summary>
public AugmentedImage Image;
public GameObject[] Models;
private void Start()
{
}
/// <summary>
/// A model for the lower left corner of the frame to place when an image is detected.
/// </summary>
// public GameObject FrameLowerLeft;
/// <summary>
/// A model for the lower right corner of the frame to place when an image is detected.
/// </summary>
// public GameObject FrameLowerRight;
/// <summary>
/// A model for the upper left corner of the frame to place when an image is detected.
/// </summary>
// public GameObject FrameUpperLeft;
/// <summary>
/// A model for the upper right corner of the frame to place when an image is detected.
/// </summary>
// public GameObject FrameUpperRight;
/// <summary>
/// The Unity Update method.
/// </summary>
public void Update()
{
if (Image == null || Image.TrackingState != TrackingState.Tracking)
{
Models[Image.DatabaseIndex].SetActive(false);
//Models[0].SetActive(false);
//Models[1].SetActive(false);
return;
}
if (Image == null || Image.TrackingState == TrackingState.Stopped)
{
Models[Image.DatabaseIndex].SetActive(false);
//Models[0].SetActive(false);
//Models[1].SetActive(false);
return;
}
if (Image == null || Image.TrackingState == TrackingState.Paused)
{
Models[Image.DatabaseIndex].SetActive(false);
//Models[0].SetActive(false);
//Models[1].SetActive(false);
return;
}
Models[Image.DatabaseIndex].SetActive(true);
}
}
}