ObjIDText.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class ObjIDText : MonoBehaviour
  6. {
  7. public TextMesh text3D;
  8. public Text text2D;
  9. public Image backgroundImage;
  10. public Outline backgroundOutline;
  11. [Space(2)]
  12. public bool applyColorToText2D = false;
  13. public bool applyColorToBackgroundImage = true;
  14. public bool applyColorToBackgroundOutline = false;
  15. public Transform boxRoot;
  16. public float heightAboveBoxCeiling = 0.05f;
  17. private Vector3 startScale;
  18. [Space(5)]
  19. public bool showID = true;
  20. public bool showDistance = true;
  21. [Space(5)]
  22. public bool lookAtCamera = false;
  23. private int currentID = -1;
  24. private float currentDistance = -1f;
  25. // Start is called before the first frame update
  26. void Awake()
  27. {
  28. Camera.onPreRender += PreRender;
  29. if (!text3D) text3D = GetComponentInChildren<TextMesh>();
  30. if (!text2D) text2D = GetComponentInChildren<Text>();
  31. if (!boxRoot) boxRoot = transform.parent;
  32. startScale = transform.localScale;
  33. }
  34. public void SetID(int id)
  35. {
  36. currentID = id;
  37. UpdateText(currentID, currentDistance);
  38. }
  39. public void SetDistance(float dist)
  40. {
  41. currentDistance = dist;
  42. UpdateText(currentID, currentDistance);
  43. }
  44. public void SetColor(Color col)
  45. {
  46. if (text3D)
  47. {
  48. text3D.color = new Color(col.r, col.g, col.b, text3D.color.a);
  49. }
  50. if (text2D && applyColorToText2D)
  51. {
  52. text2D.color = new Color(col.r, col.g, col.b, text2D.color.a);
  53. }
  54. if (backgroundImage && applyColorToBackgroundImage)
  55. {
  56. backgroundImage.color = new Color(col.r, col.g, col.b, backgroundImage.color.a);
  57. }
  58. if (backgroundOutline && applyColorToBackgroundOutline)
  59. {
  60. backgroundOutline.effectColor = new Color(col.r, col.g, col.b, backgroundOutline.effectColor.a);
  61. }
  62. }
  63. private void Update()
  64. {
  65. transform.localScale = new Vector3(1 / boxRoot.localScale.x * startScale.x,
  66. 1 / boxRoot.localScale.y * startScale.y,
  67. 1 / boxRoot.localScale.z * startScale.z);
  68. transform.localPosition = new Vector3(transform.localPosition.x, 0.5f + heightAboveBoxCeiling / boxRoot.localScale.y, transform.localPosition.z);
  69. }
  70. private void OnDestroy()
  71. {
  72. Camera.onPreRender -= PreRender;
  73. }
  74. private void PreRender(Camera cam)
  75. {
  76. if (lookAtCamera)
  77. {
  78. transform.rotation = Quaternion.LookRotation(transform.position - cam.transform.position, Vector3.up);
  79. }
  80. }
  81. private void UpdateText(int id, float dist)
  82. {
  83. string newtext = "";
  84. if (showID) newtext += "ID: " + id.ToString();
  85. if (showID && showDistance) newtext += "\r\n";
  86. if (showDistance) newtext += dist.ToString("F2") + "m";
  87. if (text3D) text3D.text = newtext;
  88. if (text2D) text2D.text = newtext;
  89. }
  90. }