using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Drawer : MonoBehaviour { public LineRenderer lineRenderer; public PolygonCollider2D edgeCollider2D; List points = new List(); bool dragging = false; public void OnMouseDown(BaseEventData e){ if (drawn) { return; } PointerEventData ped = (PointerEventData) e as PointerEventData; dragging= true; startedPoint = ped.position; lineRenderer.SetPositions(new Vector3[0]); points = new List(); drawn = true; // lineRenderer.transform.position = new Vector3(0, 0, 10); //lineRenderer.GetComponent().simulated = false; } Vector3 startedPoint = new Vector3(); public void OnMouseDrag(BaseEventData e) { PointerEventData ped = (PointerEventData) e as PointerEventData; Vector3 worldPos = Camera.main.ScreenToWorldPoint(ped.position); if(points.Count> 0) { RaycastHit2D hit = Physics2D.Linecast(points[points.Count - 1], worldPos); if (hit.collider != null) { return; } } points.Add(worldPos); UpdateLine(); } public void OnMouseUp(BaseEventData e) { dragging= false; FinishDrawing(); } void UpdateLine() { lineRenderer.positionCount = points.Count; lineRenderer.SetPositions(points.ToArray()); } bool drawn = false; void FinishDrawing() { List points3 = new List(); foreach(Vector2 point in points) { points3.Add(point); } for(int i= points.Count-1; i >0; i--) { points3.Add(points[i] + new Vector3(0.05f,0.1f,0)); } //edgeCollider2D.SetPoints(points3); edgeCollider2D.SetPath(0, points3); edgeCollider2D.GetComponent().simulated = true; } void OnValidate() { if (edgeCollider2D == null) { edgeCollider2D = lineRenderer.GetComponent(); } } }