69 lines
1.5 KiB
C#
69 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class Drawer : MonoBehaviour
|
|
{
|
|
public LineRenderer lineRenderer;
|
|
public EdgeCollider2D edgeCollider2D;
|
|
List<Vector3> points = new List<Vector3>();
|
|
|
|
|
|
bool dragging = false;
|
|
public void OnMouseDown(BaseEventData e){
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
|
|
dragging= true;
|
|
startedPoint = ped.position;
|
|
lineRenderer.SetPositions(new Vector3[0]);
|
|
points = new List<Vector3>();
|
|
|
|
|
|
}
|
|
|
|
|
|
Vector3 startedPoint = new Vector3();
|
|
public void OnMouseDrag(BaseEventData e) {
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
|
|
points.Add(Camera.main.ScreenToWorldPoint(ped.position));
|
|
UpdateLine();
|
|
}
|
|
|
|
public void OnMouseUp(BaseEventData e)
|
|
{
|
|
dragging= false;
|
|
FinishDrawing();
|
|
}
|
|
|
|
|
|
void UpdateLine()
|
|
{
|
|
lineRenderer.positionCount = points.Count;
|
|
lineRenderer.SetPositions(points.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
void FinishDrawing()
|
|
{
|
|
List<Vector2> points3 = new List<Vector2>();
|
|
foreach(Vector2 point in points)
|
|
{
|
|
points3.Add(point);
|
|
}
|
|
edgeCollider2D.SetPoints(points3);
|
|
edgeCollider2D.GetComponent<Rigidbody2D>().simulated = true;
|
|
}
|
|
|
|
void OnValidate()
|
|
{
|
|
if (edgeCollider2D == null)
|
|
{
|
|
edgeCollider2D = lineRenderer.GetComponent<EdgeCollider2D>();
|
|
}
|
|
}
|
|
}
|