You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DrawLine : MonoBehaviour,IPointerClickHandler
{
RectTransform rt;
void Start()
{
rt = GetComponent<RectTransform>();
Vector2 point1 = rt.anchoredPosition + new Vector2(-rt.sizeDelta.x / 2, rt.sizeDelta.y / 2);
Vector2 point2 = rt.anchoredPosition + new Vector2(rt.sizeDelta.x / 2, rt.sizeDelta.y / 2);
Vector2 point3 = rt.anchoredPosition + new Vector2(-rt.sizeDelta.x / 2, -rt.sizeDelta.y / 2);
Vector2 point4 = rt.anchoredPosition + new Vector2(rt.sizeDelta.x / 2, -rt.sizeDelta.y / 2);
DrawStraightLine(point1, point2);
DrawStraightLine(point2, point4);
DrawStraightLine(point3, point4);
DrawStraightLine(point3, point1);
}
void DrawStraightLine(Vector2 a, Vector2 b)
{
GameObject img = new GameObject();
img.AddComponent<Image>();
img.transform.SetParent(transform);
img.GetComponent<Image>().raycastTarget = false;
float distance = Vector2.Distance(a, b);
float angle = Vector2.SignedAngle(a - b, Vector2.left);
img.GetComponent<RectTransform>().anchoredPosition = (a + b) / 2;
img.GetComponent<RectTransform>().sizeDelta = new Vector2(distance, 5);
img.transform.localRotation = Quaternion.AngleAxis(-angle, Vector3.forward);
img.GetComponent<Image>().color = Color.red;
}
public void OnPointerClick(PointerEventData eventData)
{
}
}