0%

Unity创建一个物理线

代码如下:

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
{
/// <summary>
/// 线
/// </summary>
public Material line;
/// <summary>
/// 是否开启重力
/// </summary>
public bool gravity;
/// <summary>
/// 线的颜色
/// </summary>
public Color lineColor = Color.white;
/// <summary>
/// 线列表
/// </summary>
private List<GameObject> lineList = new List<GameObject>();
/// <summary>
/// 点列表
/// </summary>
private List<Vector2> pointList = new List<Vector2>();
/// <summary>
/// 当前线
/// </summary>
private GameObject currentLine;
/// <summary>
/// 当前线渲染器
/// </summary>
private LineRenderer currentLineRenderer;

private bool stopHolding;
/// <summary>
/// 获取Shader中材质的发光颜色
/// </summary>
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)//如果点列表内的数量大于等于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>();//给该对象添加2d碰撞器
currentBoxCollider2D.size = new Vector3((vector2 - vector1).magnitude, 0.1f, 0.1f);//设置碰撞器尺寸
currentBoxCollider2D.enabled = false;//碰撞器开关暂时不开启
}
}
}

if (Input.GetMouseButtonUp(0))//鼠标左键抬起
{
if (currentLine.transform.childCount > 0)//如果当前线的子物体大于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();//清除所有线
}
}
/// <summary>
/// 创建线
/// </summary>
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;//是否开启在世界空间中定义线
}
/// <summary>
/// 清空全部
/// </summary>
public void ClearAll()
{
foreach (var obj in lineList)
{
Destroy(obj);
}
lineList.Clear();
}
}
}