using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// текст над маркером
/// </summary>
public class LabelObjectScript : MonoBehaviour
{
    Camera camera;
    public GameObject target;
    public RectTransform canvasRect;
    public GameObject text;
    // Start is called before the first frame update
    void Start()
    {
        //text = Instantiate(Resources.Load("GameObjects/Capsule", typeof(GameObject))) as GameObject;
        cameraController = Camera.main.GetComponent<CameraController>();
    }

    // Update is called once per frame
    void Update()
    {

        // Offset position above object bbox (in world space)
        float offsetPosY = target.transform.position.y + 1.5f;

        // Final position of marker above GO in world space
        Vector3 offsetPos = new Vector3(target.transform.position.x, offsetPosY, target.transform.position.z);

        // Calculate *screen* position (note, not a canvas/recttransform position)
        Vector2 canvasPos;
        Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);

        // Convert screen position to Canvas / RectTransform space <- leave camera null if Screen Space Overlay
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRect, screenPoint, null, out canvasPos);

        // Set
        //markerRtra.localPosition = canvasPos;
        text.transform.localPosition = canvasPos;

    }

    CameraController cameraController;
    void OnMouseDown()
    {
        cameraController.target = transform;
        cameraController.axes = CameraController.RotationAxes.TrackMarker;
    }
}
