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
| using System.Collections.Generic; using System.Linq; using UnityEngine;
namespace Starry { public class Test_0 : MonoBehaviour { public Material line; public bool gravity; public Color lineColor = Color.white; private List<GameObject> lineList = new List<GameObject>(); private List<Vector2> pointList = new List<Vector2>(); private GameObject currentLine; private LineRenderer currentLineRenderer;
private bool stopHolding; private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor");
private void Update() { if (Input.GetMouseButtonDown(0)) { pointList.Clear(); CreateLine(); }
if (Input.GetMouseButton(0)) { Vector2 item = Camera.main.ScreenToWorldPoint(Input.mousePosition); if (!pointList.Contains(item)) { pointList.Add(item); currentLineRenderer.positionCount = pointList.Count; currentLineRenderer.SetPosition(pointList.Count - 1, pointList.Last()); if (pointList.Count >= 2) { Vector2 vector1 = pointList[pointList.Count - 2]; Vector2 vector2 = pointList[pointList.Count - 1];
GameObject currentCollierObject = new GameObject("Collider"); currentCollierObject.transform.position = (vector1 + vector2) / 2f; currentCollierObject.transform.right = (vector2 - vector1).normalized; currentCollierObject.transform.parent = currentLine.transform; BoxCollider2D currentBoxCollider2D = currentCollierObject.AddComponent<BoxCollider2D>(); currentBoxCollider2D.size = new Vector3((vector2 - vector1).magnitude, 0.1f, 0.1f); currentBoxCollider2D.enabled = false; } } }
if (Input.GetMouseButtonUp(0)) { if (currentLine.transform.childCount > 0) { for (int i = 0; i < currentLine.transform.childCount; i++) { currentLine.transform.GetChild(i).GetComponent<BoxCollider2D>().enabled = true; }
lineList.Add(currentLine);
if (gravity) { currentLine.AddComponent<Rigidbody2D>().useAutoMass = true; } } else { Destroy(currentLine); } }
if (Input.GetMouseButtonDown(1)) { ClearAll(); } } private void CreateLine() { currentLine = new GameObject("Line"); currentLineRenderer = currentLine.AddComponent<LineRenderer>(); currentLineRenderer.material = line; currentLineRenderer.material.EnableKeyword("_EMISSION"); currentLineRenderer.material.SetColor(EmissionColor, this.lineColor); currentLineRenderer.positionCount = 0; currentLineRenderer.startWidth = 0.1f; currentLineRenderer.endWidth = 0.1f; currentLineRenderer.startColor = lineColor; currentLineRenderer.endColor = lineColor; currentLineRenderer.useWorldSpace = false; } public void ClearAll() { foreach (var obj in lineList) { Destroy(obj); } lineList.Clear(); } } }
|