Ver código fonte

Implement IZEDManager interface

Danghor 2 anos atrás
pai
commit
125ad6de15

+ 15 - 17
Assets/ZED/Examples/Object Detection/Scripts/ZED3DObjectVisualizer.cs

@@ -1,4 +1,5 @@
-using System.Collections;
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
@@ -18,8 +19,10 @@ public class ZED3DObjectVisualizer : MonoBehaviour
     /// If you want to visualize detections from multiple ZEDs at once you will need multiple ZED3DObjectVisualizer commponents in the scene.
     /// </summary>
     [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;
+             "If you want to visualize detections from multiple ZEDs at once you will need multiple ZED3DObjectVisualizer commponents in the scene. ")]
+    public IZEDManager ZedManager => zedManagerLazy.Value;
+
+    public Lazy<IZEDManager> zedManagerLazy = new(FindObjectOfType<ZEDManager>);
 
     /// <summary>
     /// If true, the ZED Object Detection manual will be started as soon as the ZED is initiated.
@@ -139,15 +142,10 @@ public class ZED3DObjectVisualizer : MonoBehaviour
     // Use this for initialization
     void Start()
     {
-        if (!zedManager)
-        {
-            zedManager = FindObjectOfType<ZEDManager>();
-        }
-
-        zedManager.OnObjectDetection += Visualize3DBoundingBoxes;
-        zedManager.OnZEDReady += OnZEDReady;
+        ZedManager.OnObjectDetection += Visualize3DBoundingBoxes;
+        ZedManager.OnZEDReady += OnZEDReady;
 
-        if (zedManager.estimateInitialPosition == false && transformBoxToTouchFloor == true)
+        if (ZedManager.EstimateInitialPosition == false && transformBoxToTouchFloor == true)
         {
             Debug.Log("Estimate initial position is set to false. Then, transformBoxToTouchFloor is disable.");
             transformBoxToTouchFloor = false;
@@ -156,9 +154,9 @@ public class ZED3DObjectVisualizer : MonoBehaviour
 
     private void OnZEDReady()
     {
-        if (startObjectDetectionAutomatically && !zedManager.IsObjectDetectionRunning)
+        if (startObjectDetectionAutomatically && !ZedManager.IsObjectDetectionRunning)
         {
-            zedManager.StartObjectDetection();
+            ZedManager.StartObjectDetection();
         }
     }
 
@@ -302,7 +300,7 @@ public class ZED3DObjectVisualizer : MonoBehaviour
             if (boxhandler)
             {
                 boxhandler.SetColor(col);
-                if (zedManager.objectDetectionModel == sl.DETECTION_MODEL.CUSTOM_BOX_OBJECTS)
+                if (ZedManager.ObjectDetectionModel == sl.DETECTION_MODEL.CUSTOM_BOX_OBJECTS)
                 {
                     //boxhandler.SetID(dobj.rawObjectData.rawLabel.ToString());
                     boxhandler.SetID(dobj.id.ToString());
@@ -388,10 +386,10 @@ public class ZED3DObjectVisualizer : MonoBehaviour
 
     private void OnDestroy()
     {
-        if (zedManager)
+        if (ZedManager != null)
         {
-            zedManager.OnObjectDetection -= Visualize3DBoundingBoxes;
-            zedManager.OnZEDReady -= OnZEDReady;
+            ZedManager.OnObjectDetection -= Visualize3DBoundingBoxes;
+            ZedManager.OnZEDReady -= OnZEDReady;
         }
     }
 

+ 26 - 0
Assets/ZED/SDK/Helpers/Scripts/IZEDManager.cs

@@ -0,0 +1,26 @@
+using sl;
+
+public interface IZEDManager
+{
+    /// <summary>
+    /// Whether the object detection module has been activated successfully.
+    /// </summary>
+    bool IsObjectDetectionRunning { get; }
+
+    bool EstimateInitialPosition { get; }
+    DETECTION_MODEL ObjectDetectionModel { get; }
+
+    /// <summary>
+    /// Event that's called whenever the Object Detection module detects a new frame.
+    /// Supplies data in the form of a DetectionFrame instance, which has many helper functions for use in Unity.
+    /// </summary>
+    event ZEDManager.onNewDetectionTriggerDelegate OnObjectDetection;
+
+    /// <summary>
+    /// Called when the ZED has finished initializing successfully.
+    /// Used by many scripts to run startup logic that requires that the ZED is active.
+    /// </summary>
+    event ZEDManager.OnZEDManagerReady OnZEDReady;
+
+    void StartObjectDetection();
+}

+ 5 - 1
Assets/ZED/SDK/Helpers/Scripts/ZEDManager.cs

@@ -14,7 +14,7 @@ using System.Collections.Generic;
 /// ZEDManager is attached to the root objects in the ZED_Rig_Mono and ZED_Rig_Stereo prefabs.
 /// If using ZED_Rig_Stereo, it will set isStereoRig to true, which triggers several behaviors unique to stereo pass-through AR.
 /// </remarks>
-public class ZEDManager : MonoBehaviour
+public class ZEDManager : MonoBehaviour, IZEDManager
 {
 
     /// <summary>
@@ -255,6 +255,8 @@ public class ZEDManager : MonoBehaviour
     [HideInInspector]
     public bool estimateInitialPosition = true;
 
+    public bool EstimateInitialPosition => estimateInitialPosition;
+
     /// <summary>
     /// If true, tracking is enabled but doesn't move after initializing.
     /// </summary>
@@ -376,6 +378,8 @@ public class ZEDManager : MonoBehaviour
     /// </summary>
     [HideInInspector]
     public sl.DETECTION_MODEL objectDetectionModel = sl.DETECTION_MODEL.MULTI_CLASS_BOX;
+    
+    public sl.DETECTION_MODEL ObjectDetectionModel => objectDetectionModel;
 
     /// <summary>
     /// Defines if the body fitting will be applied