1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class lineGenerator : MonoBehaviour { [SerializeField] [Header("线条预制体")] private GameObject linePrefab;
[SerializeField] [Header("线条灵敏度")] private float pointDistance = 0.5f;
private Vector3 firstPoint; private Vector3 prePoint; private Vector3 tempPoint; private List<Vector3> points = new List<Vector3>(); private List<Vector2> polygonPoints = new List<Vector2>();
private GameObject line; private bool isMousePressed = false;
void Start() { }
void Update() { GetMouse(); }
void GetMouse() { if(Input.GetMouseButtonDown(0)) { firstPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); firstPoint.Set(firstPoint.x, firstPoint.y, 0); prePoint = firstPoint; isMousePressed = true; line = GenerateLine(firstPoint); points.Add(firstPoint); } if(Input.GetMouseButtonUp(0)) { if(points.Count <= 1) { tempPoint.Set(firstPoint.x - 0.2f, firstPoint.y - 0.2f, 0); LineRenderer renderer = line.GetComponent<LineRenderer>(); renderer.positionCount++; renderer.SetPosition(renderer.positionCount - 1, tempPoint); points.Add(tempPoint); } AddToPolygonPoints(points.ToArray(),line); GeneratePolygonCollider(line); points.Clear(); polygonPoints.Clear(); isMousePressed = false; }
if(isMousePressed) { tempPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); tempPoint.Set(tempPoint.x, tempPoint.y, 0); if(NeedRecord()) { LineRenderer renderer = line.GetComponent<LineRenderer>(); renderer.positionCount++; renderer.SetPosition(renderer.positionCount-1, tempPoint); points.Add(tempPoint); prePoint = tempPoint; } } }
bool NeedRecord() { if(Vector3.Distance(tempPoint,prePoint) > pointDistance) { return true; } return false; }
GameObject GenerateLine(Vector3 startPoint) { LineRenderer renderer = linePrefab.GetComponent<LineRenderer>(); renderer.positionCount = 1; renderer.SetPosition(0, startPoint); return Instantiate(linePrefab, new Vector3(0, 0, 0), new Quaternion(0, 0, 0, 0)); }
void AddToPolygonPoints(Vector3[] pointArray,GameObject line) { float width = line.GetComponent<LineRenderer>().startWidth; int arrayLength = pointArray.Length; for(int i=0;i<arrayLength;i++) { if(i == 0) { Vector3 direction = Vector3.Cross(Vector3.forward, (pointArray[i+1] - pointArray[i])); Vector3 leftPoint = pointArray[i] + (direction.normalized * width * 0.5f); Vector3 rightPoint = pointArray[i] - (direction.normalized * width * 0.5f); Vector2 left = new Vector2(leftPoint.x, leftPoint.y); Vector2 right = new Vector2(rightPoint.x, rightPoint.y); polygonPoints.Insert(0, left); polygonPoints.Add(right);
} else if(i == arrayLength-1) { Vector3 direction = Vector3.Cross(Vector3.forward, (pointArray[i] - pointArray[i-1])); Vector3 leftPoint = pointArray[i] + (direction.normalized * width * 0.5f); Vector3 rightPoint = pointArray[i] - (direction.normalized * width * 0.5f); Vector2 left = new Vector2(leftPoint.x, leftPoint.y); Vector2 right = new Vector2(rightPoint.x, rightPoint.y); polygonPoints.Insert(0, left); polygonPoints.Add(right);
} else { Vector3 verticalVectorOne = Vector3.Cross((pointArray[i - 1] - pointArray[i]), Vector3.forward).normalized; Vector3 verticalVectorTwo= Vector3.Cross( Vector3.forward ,(pointArray[i + 1] - pointArray[i])).normalized; Vector3 direction = ( verticalVectorOne + verticalVectorTwo ).normalized * width * 0.5f; Vector3 leftPoint = pointArray[i] + direction; Vector3 rightPoint = pointArray[i] - direction; Vector2 left = new Vector2(leftPoint.x, leftPoint.y); Vector2 right = new Vector2(rightPoint.x, rightPoint.y); polygonPoints.Insert(0, left); polygonPoints.Add(right); } } }
void GeneratePolygonCollider(GameObject line) { PolygonCollider2D polygon = line.AddComponent<PolygonCollider2D>(); polygon.pathCount = polygonPoints.Count; polygon.points = polygonPoints.ToArray(); } }
|