BBox2DHandler.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// For the ZED 2D Object Detection sample. Handles the canvas elements that are moved and resized
  7. /// to represent objects detected in 2D.
  8. /// This was designed specifically for the 2D Bounding Box prefab.
  9. /// </summary>
  10. public class BBox2DHandler : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// Text object that displays the ID and distance values of the object.
  14. /// </summary>
  15. [Tooltip("Text object that displays the ID and distance values of the object.")]
  16. public Text infoText;
  17. /// <summary>
  18. /// Outline component around the Image component that surrounds the Text component.
  19. /// </summary>
  20. [Tooltip("Outline component around the Image component that surrounds the Text component.")]
  21. public Outline boxOutline;
  22. /// <summary>
  23. /// All images in the prefab that should be colored when SetColor() is called.
  24. /// </summary>
  25. [Tooltip("All images in the prefab that should be colored when SetColor() is called.")]
  26. public List<Image> imagesToColor = new List<Image>();
  27. /// <summary>
  28. /// RawImage object on the prefab to be used to display the object's mask, if enabled.
  29. /// </summary>
  30. [Space(5)]
  31. [Tooltip("RawImage object on the prefab to be used to display the object's mask, if enabled.")]
  32. public RawImage maskImage;
  33. /// <summary>
  34. /// If true, infoText will display the ID value of the object, assuming it's been set.
  35. /// </summary>
  36. [Space(5)]
  37. [Tooltip("If true, infoText will display the ID value of the object, assuming it's been set.")]
  38. public bool showID = true;
  39. /// <summary>
  40. /// If true, infoText will display the distance value of the object from its capturing camera, assuming it's been set.
  41. /// </summary>
  42. [Tooltip("If true, infoText will display the ID value of the object, assuming it's been set.")]
  43. public bool showDistance = true;
  44. /// <summary>
  45. /// ID of the object that this instance is currently representing.
  46. /// </summary>
  47. public int currentID { get; private set; }
  48. /// <summary>
  49. /// Distance from this object to the ZED camera that detected it.
  50. /// </summary>
  51. public float currentDistance { get; private set; }
  52. private void OnEnable()
  53. {
  54. if(maskImage) //Disable the mask image at first, so you don't see it if we're not applying a mask.
  55. {
  56. maskImage.gameObject.SetActive(false);
  57. }
  58. }
  59. /// <summary>
  60. /// Changes the color of all relevant elements in the prefab (box, text, etc.) to the color provided.
  61. /// Ignores input alpha and uses existing alpha of prefab's components.
  62. /// </summary>
  63. public void SetColor(Color col)
  64. {
  65. foreach (Image img in imagesToColor)
  66. {
  67. float oldimgalpha = img.color.a;
  68. img.color = new Color(col.r, col.g, col.b, oldimgalpha);
  69. }
  70. if (infoText)
  71. {
  72. float oldtextalpha = infoText.color.a;
  73. infoText.color = new Color(col.r, col.g, col.b, oldtextalpha);
  74. }
  75. if(boxOutline)
  76. {
  77. float oldtextalpha = boxOutline.effectColor.a;
  78. boxOutline.effectColor = new Color(col.r, col.g, col.b, oldtextalpha);
  79. }
  80. if(maskImage)
  81. {
  82. float oldmaskalpha = maskImage.color.a;
  83. maskImage.color = new Color(col.r, col.g, col.b, oldmaskalpha);
  84. }
  85. }
  86. /// <summary>
  87. /// Sets the ID value that will be displayed on the box's label.
  88. /// Usually set when the box first starts representing a detected object.
  89. /// </summary>
  90. public void SetID(int id)
  91. {
  92. currentID = id;
  93. UpdateText(currentID, currentDistance);
  94. }
  95. /// <summary>
  96. /// Sets the distance value that will be displayed on the box's label.
  97. /// Designed to indicate the distance from the camera that saw the object.
  98. /// Value is expected in meters. Should be updated with each new detection.
  99. /// </summary>
  100. public void SetDistance(float dist)
  101. {
  102. currentDistance = dist;
  103. UpdateText(currentID, currentDistance);
  104. }
  105. public void SetMaskImage(Texture mask)
  106. {
  107. if(maskImage)
  108. {
  109. maskImage.gameObject.SetActive(true);
  110. maskImage.texture = mask;
  111. }
  112. }
  113. /// <summary>
  114. /// Updates the text in the label based on the given ID and distance values.
  115. /// </summary>
  116. private void UpdateText(int id, float dist)
  117. {
  118. string newtext = "";
  119. if (showID) newtext += "ID: " + id.ToString();
  120. if (showID && showDistance) newtext += "\r\n";
  121. if (showDistance) newtext += dist.ToString("F2") + "m";
  122. if (infoText) infoText.text = newtext;
  123. }
  124. private void OnDestroy()
  125. {
  126. if(maskImage) //Makes sure textures left over on the masks are cleaned up.
  127. {
  128. Destroy(maskImage.texture);
  129. }
  130. }
  131. }