LabelObjectScript.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// текст над маркером
  7. /// </summary>
  8. public class LabelObjectScript : MonoBehaviour
  9. {
  10. Camera camera;
  11. public GameObject target;
  12. public RectTransform canvasRect;
  13. public GameObject text;
  14. public bool TrackMarker = true;
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. //text = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
  19. cameraController = Camera.main.GetComponent<CameraController>();
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. // Offset position above object bbox (in world space)
  25. float offsetPosY = target.transform.position.y + 1.5f;
  26. // Final position of marker above GO in world space
  27. Vector3 offsetPos = new Vector3(target.transform.position.x, offsetPosY, target.transform.position.z);
  28. // Calculate *screen* position (note, not a canvas/recttransform position)
  29. Vector2 canvasPos;
  30. Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
  31. // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
  32. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
  33. // Set
  34. //markerRtra.localPosition = canvasPos;
  35. if(TrackMarker) text.transform.localPosition = canvasPos;
  36. else text.transform.parent.transform.localPosition = canvasPos;
  37. }
  38. CameraController cameraController;
  39. void OnMouseDown()
  40. {
  41. if (TrackMarker)
  42. {
  43. cameraController.target = transform;
  44. cameraController.axes = CameraController.RotationAxes.TrackMarker;
  45. }
  46. }
  47. }