123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class BBox2DHandler : MonoBehaviour
- {
-
-
-
- [Tooltip("Text object that displays the ID and distance values of the object.")]
- public Text infoText;
-
-
-
- [Tooltip("Outline component around the Image component that surrounds the Text component.")]
- public Outline boxOutline;
-
-
-
- [Tooltip("All images in the prefab that should be colored when SetColor() is called.")]
- public List<Image> imagesToColor = new List<Image>();
-
-
-
- [Space(5)]
- [Tooltip("RawImage object on the prefab to be used to display the object's mask, if enabled.")]
- public RawImage maskImage;
-
-
-
- [Space(5)]
- [Tooltip("If true, infoText will display the ID value of the object, assuming it's been set.")]
- public bool showID = true;
-
-
-
- [Tooltip("If true, infoText will display the ID value of the object, assuming it's been set.")]
- public bool showDistance = true;
-
-
-
- public int currentID { get; private set; }
-
-
-
- public float currentDistance { get; private set; }
- private void OnEnable()
- {
- if(maskImage)
- {
- maskImage.gameObject.SetActive(false);
- }
- }
-
-
-
-
- public void SetColor(Color col)
- {
- foreach (Image img in imagesToColor)
- {
- float oldimgalpha = img.color.a;
- img.color = new Color(col.r, col.g, col.b, oldimgalpha);
- }
- if (infoText)
- {
- float oldtextalpha = infoText.color.a;
- infoText.color = new Color(col.r, col.g, col.b, oldtextalpha);
- }
- if(boxOutline)
- {
- float oldtextalpha = boxOutline.effectColor.a;
- boxOutline.effectColor = new Color(col.r, col.g, col.b, oldtextalpha);
- }
- if(maskImage)
- {
- float oldmaskalpha = maskImage.color.a;
- maskImage.color = new Color(col.r, col.g, col.b, oldmaskalpha);
- }
- }
-
-
-
-
- public void SetID(int id)
- {
- currentID = id;
- UpdateText(currentID, currentDistance);
- }
-
-
-
-
-
- public void SetDistance(float dist)
- {
- currentDistance = dist;
- UpdateText(currentID, currentDistance);
- }
- public void SetMaskImage(Texture mask)
- {
- if(maskImage)
- {
- maskImage.gameObject.SetActive(true);
- maskImage.texture = mask;
- }
- }
-
-
-
- private void UpdateText(int id, float dist)
- {
- string newtext = "";
- if (showID) newtext += "ID: " + id.ToString();
- if (showID && showDistance) newtext += "\r\n";
- if (showDistance) newtext += dist.ToString("F2") + "m";
- if (infoText) infoText.text = newtext;
- }
- private void OnDestroy()
- {
- if(maskImage)
- {
- Destroy(maskImage.texture);
- }
- }
- }
|