ConfidenceDemo.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Imagine.WebAR.Demo{
  6. public class ConfidenceDemo : MonoBehaviour
  7. {
  8. public Image confidenceIcon;
  9. private RectTransform container;
  10. public Camera cam;
  11. public Transform trackObject;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. container = confidenceIcon.transform.parent.GetComponent<RectTransform>();
  16. }
  17. public void OnConfidence(float confidence){
  18. var col = new Color((1 - confidence)*2, confidence*2, 0);
  19. confidenceIcon.color = col;
  20. }
  21. void Update(){
  22. if(!trackObject.gameObject.activeInHierarchy)
  23. return;
  24. var canvasW = container.rect.width;
  25. var canvasH = container.rect.height;
  26. var viewPos = cam.WorldToViewportPoint(trackObject.position);
  27. // Debug.Log(viewPos + ", " + canvasW + ", " + canvasH);
  28. confidenceIcon.rectTransform.anchoredPosition = new Vector2(
  29. (viewPos.x - 0.5f) * canvasW,
  30. (viewPos.y - 0.5f) * canvasH
  31. );
  32. }
  33. }
  34. }