using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
///
/// For the ZED 2D Object Detection sample.
/// Listens for new object detections (via the ZEDManager.OnObjectDetection event) and moves + resizes canvas prefabs
/// to represent them.
/// Works by instantiating a pool of prefabs, and each frame going through the DetectedFrame received from the event
/// to make sure each detected object has a representative GameObject. Also disables GameObjects whose objects are no
/// longer visible and returns them to the pool.
///
public class ZED2DObjectVisualizer : MonoBehaviour
{
///
/// The scene's ZEDManager.
/// If you want to visualize detections from multiple ZEDs at once you will need multiple ZED3DObjectVisualizer commponents in the scene.
///
[Tooltip("The scene's ZEDManager.\r\n" +
"If you want to visualize detections from multiple ZEDs at once you will need multiple ZED3DObjectVisualizer commponents in the scene. ")]
public ZEDManager zedManager;
///
/// The scene's canvas. This will be adjusted to have required settings/components so that the bounding boxes
/// will line up properly with the ZED video feed.
///
[Tooltip("The scene's canvas. This will be adjusted to have required settings/components so that the bounding boxes " +
"will line up properly with the ZED video feed.")]
public Canvas canvas;
///
/// If true, the ZED Object Detection manual will be started as soon as the ZED is initiated.
/// This avoids having to press the Start Object Detection button in ZEDManager's Inspector.
///
[Tooltip("If true, the ZED Object Detection manual will be started as soon as the ZED is initiated. " +
"This avoids having to press the Start Object Detection button in ZEDManager's Inspector.")]
public bool startObjectDetectionAutomatically = true;
///
/// Prefab object that's instantiated to represent detected objects.
/// This should ideally be the 2D Bounding Box prefab. But otherwise, it expects the object to have a BBox2DHandler script in the root object,
/// and the RectTransform should be bottom-left-aligned (pivot set to 0, 0).
///
[Space(5)]
[Header("Box Appearance")]
[Tooltip("Prefab object that's instantiated to represent detected objects. " +
"This class expects the object to have the default Unity cube as a mesh - otherwise, it may be scaled incorrectly.\r\n" +
"It also expects a BBox3DHandler component in the root object, but you won't have errors if it lacks one. ")]
public GameObject boundingBoxPrefab;
///
/// The colors that will be cycled through when assigning colors to new bounding boxes.
///
[Tooltip("The colors that will be cycled through when assigning colors to new bounding boxes. ")]
//[ColorUsage(true, true)] //Uncomment to enable HDR colors in versions of Unity that support it.
public List boxColors = new List()
{
new Color(.231f, .909f, .69f, 1),
new Color(.098f, .686f, .816f, 1),
new Color(.412f, .4f, .804f, 1),
new Color(1, .725f, 0f, 1),
new Color(.989f, .388f, .419f, 1)
};
///
/// When a detected object is first given a box and assigned a color, we store it so that if the object
/// disappears and appears again later, it's assigned the same color.
/// This is also solvable by making the color a function of the ID number itself, but then you can get
/// repeat colors under certain conditions.
///
private Dictionary idColorDict = new Dictionary();
///
/// If true, draws a 2D mask over where the SDK believes the detected object is.
///
[Space(5)]
[Header("Mask")]
public bool showObjectMask = true;
///
/// Used to warn the user only once if they enable the mask but the mask was not enabled when object detection was initialized. See OnValidate.
///
private bool lastShowObjectMaskValue;
///
/// Display bounding boxes of objects that are actively being tracked by object tracking, where valid positions are known.
///
[Space(5)]
[Header("Filters")]
[Tooltip("Display bounding boxes of objects that are actively being tracked by object tracking, where valid positions are known. ")]
public bool showONTracked = true;
///
/// Display bounding boxes of objects that were actively being tracked by object tracking, but that were lost very recently.
///
[Tooltip("Display bounding boxes of objects that were actively being tracked by object tracking, but that were lost very recently.")]
public bool showSEARCHINGTracked = false;
///
/// Display bounding boxes of objects that are visible but not actively being tracked by object tracking (usually because object tracking is disabled in ZEDManager).
///
[Tooltip("Display bounding boxes of objects that are visible but not actively being tracked by object tracking (usually because object tracking is disabled in ZEDManager).")]
public bool showOFFTracked = false;
///
/// Used to know which of the available colors will be assigned to the next bounding box to be used.
///
private int nextColorIndex = 0;
///
/// Pre-instantiated bbox prefabs currently not in use.
///
private Stack bboxPool = new Stack();
///
/// All active RectTransforms within GameObjects that were instantiated to the prefab and that currently represent a detected object.
/// Key is the object's objectID.
///
private Dictionary liveBBoxes = new Dictionary();
///
/// List of all 2D masks created in a frame. Used so that they can all be disposed of in the frame afterward.
///
private List lastFrameMasks = new List();
private void Start()
{
if (!zedManager)
{
zedManager = FindObjectOfType();
}
zedManager.OnObjectDetection += Visualize2DBoundingBoxes;
zedManager.OnZEDReady += OnZEDReady;
if (!canvas) //If we don't have a canvas in the scene, we need one.
{
GameObject canvasgo = new GameObject("Canvas - " + zedManager.name);
canvas = canvasgo.AddComponent