LabelObjectScript.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. //public GameObject target;
  11. RectTransform canvasRect;
  12. GameObject text;
  13. public bool TrackMarker = true;
  14. // Start is called before the first frame update
  15. void Start()
  16. {
  17. //text = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
  18. cameraController = Camera.main.GetComponent<CameraController>();
  19. canvasRect = transform.GetChild(0).GetComponent<RectTransform>();
  20. text = transform.GetChild(0).GetChild(0).GetChild(0).gameObject;
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. // Offset position above object bbox (in world space)
  26. float offsetPosY = transform.position.y + 1.5f;
  27. // Final position of marker above GO in world space
  28. Vector3 offsetPos = new Vector3(transform.position.x, offsetPosY, transform.position.z);
  29. // Calculate *screen* position (note, not a canvas/recttransform position)
  30. Vector2 canvasPos;
  31. Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
  32. // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
  33. RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);
  34. // Set
  35. //markerRtra.localPosition = canvasPos;
  36. //if(TrackMarker) text.transform.localPosition = canvasPos;
  37. //else
  38. text.transform.parent.transform.localPosition = canvasPos;
  39. }
  40. CameraController cameraController;
  41. void OnMouseDown()
  42. {
  43. if (TrackMarker)
  44. {
  45. cameraController.target = transform;
  46. cameraController.axes = CameraController.RotationAxes.TrackMarker;
  47. }
  48. }
  49. }