35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using UnityEngine;
|
|
|
|
// Линия-маршрут на карте между двумя точками (координаты 0..1 внутри родителя).
|
|
// Пересчитывается каждый кадр, поэтому не зависит от размера экрана.
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public class WorldLine : MonoBehaviour
|
|
{
|
|
public Vector2 from;
|
|
public Vector2 to;
|
|
public float thickness = 6f;
|
|
|
|
RectTransform rt;
|
|
RectTransform parent;
|
|
|
|
void Awake()
|
|
{
|
|
rt = (RectTransform)transform;
|
|
parent = (RectTransform)transform.parent;
|
|
rt.pivot = new Vector2(0f, 0.5f);
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
if (parent == null) return;
|
|
Vector2 size = parent.rect.size;
|
|
Vector2 a = Vector2.Scale(from, size);
|
|
Vector2 b = Vector2.Scale(to, size);
|
|
Vector2 d = b - a;
|
|
rt.anchorMin = rt.anchorMax = from;
|
|
rt.anchoredPosition = Vector2.zero;
|
|
rt.sizeDelta = new Vector2(d.magnitude, thickness);
|
|
rt.localRotation = Quaternion.Euler(0, 0, Mathf.Atan2(d.y, d.x) * Mathf.Rad2Deg);
|
|
}
|
|
}
|