privatevoidAwake() { lr = GetComponent<LineRenderer>(); lr.positionCount = 0;
dots = new List<DotController>(); }
public DotController GetFirstPoint() { return dots[0]; }
publicvoidAddDot(DotController dot) {
dot.SetLine(this); dot.SetIndex(dots.Count);
lr.positionCount++; dots.Add(dot); }
publicvoidSplitPointsAtIndex(int index, out List<DotController> beforePoints, out List<DotController> afterPoints) { List<DotController> before = new List<DotController>(); List<DotController> after = new List<DotController>();
int i = 0; for (; i < index; i++) { before.Add(dots[i]); } i++; for (; i < dots.Count; i++) { after.Add(dots[i]); }
privatevoidLateUpdate() { if (dots.Count >= 2) { for (int i = 0; i < dots.Count; i++) { Vector3 position = dots[i].transform.position; position += new Vector3(0, 0, 5);
using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections; using System; publicclassDotController : MonoBehaviour, IDragHandler, IPointerClickHandler, IBeginDragHandler {
[HideInInspector] public LineController line; [HideInInspector] publicint index;
public Action<DotController> OnDragEvent; publicvoidOnDrag(PointerEventData eventData) { OnDragEvent?.Invoke(this); }
public Action<DotController> OnRightClickEvent; public Action<LineController> OnLeftClickEvent; publicvoidOnPointerClick(PointerEventData eventData) { if (eventData.pointerId == -2) { //Right Click OnRightClickEvent?.Invoke(this); } elseif (eventData.pointerId == -1) { //Left Click OnLeftClickEvent?.Invoke(line); } }
//Get all the positions from the line renderer Vector3[] positions = lineController.GetPositions();
//If we have enough points to draw a line if (positions.Count() >= 2) {
//Get the number of line between two points int numberOfLines = positions.Length - 1;
//Make as many paths for each different line as we have lines polygonCollider.pathCount = numberOfLines;
//Get Collider points between two consecutive points for (int i = 0; i < numberOfLines; i++) { //Get the two next points List<Vector2> currentPositions = new List<Vector2> { positions[i], positions[i+1] };
private List<Vector2> CalculateColliderPoints(List<Vector2> positions) { //Get The Width of the Line float width = lineController.GetWidth();
// m = (y2 - y1) / (x2 - x1) float m = (positions[1].y - positions[0].y) / (positions[1].x - positions[0].x); float deltaX = (width / 2f) * (m / Mathf.Pow(m * m + 1, 0.5f)); float deltaY = (width / 2f) * (1 / Mathf.Pow(1 + m * m, 0.5f));
//Calculate Vertex Offset from Line Point Vector2[] offsets = new Vector2[2]; offsets[0] = new Vector2(-deltaX, deltaY); offsets[1] = new Vector2(deltaX, -deltaY);
privatevoidAwake() { lr = GetComponent<LineRenderer>(); lr.positionCount = 0;
dots = new List<DotController>(); }
public DotController GetFirstPoint() { return dots[0]; }
publicvoidAddDot(DotController dot) {
dot.SetLine(this); dot.SetIndex(dots.Count);
lr.positionCount++; dots.Add(dot); }
publicvoidSplitPointsAtIndex(int index, out List<DotController> beforePoints, out List<DotController> afterPoints) { List<DotController> before = new List<DotController>(); List<DotController> after = new List<DotController>();
int i = 0; for (; i < index; i++) { before.Add(dots[i]); } i++; for (; i < dots.Count; i++) { after.Add(dots[i]); }
privatevoidLateUpdate() { if (dots.Count >= 2) { for (int i = 0; i < dots.Count; i++) { Vector3 position = dots[i].transform.position; position += new Vector3(0, 0, 5);
lr.SetPosition(i, position); } } }
public Vector3[] GetPositions() { Vector3[] positions = new Vector3[lr.positionCount]; lr.GetPositions(positions); return positions; }