如题、公司想做早教方面的小游戏、文档中有连线需求
找了几个帖子、直接复制粘贴、运行、不是自己想要的功能、于是尝试改、
逻辑看的我头大、一个List
非要写vector3[]
然后又自己去封装数组的自增
public class DrawLine : MonoBehaviour {
public Shader shader;
private static Material m;
private GameObject g;
private List spppp;
private GUIStyle labelStyle;
private GUIStyle linkStyle;
List> linesPoint;
void Start () {
labelStyle = new GUIStyle();
labelStyle.normal.textColor = Color.black;
linkStyle = new GUIStyle();
linkStyle.normal.textColor = Color.blue;
spppp = new List ();
m = new Material(shader);
g = new GameObject("g");
linesPoint = new List> ();
}
/** Replace the Update function with this one for a click&drag drawing option */
void Update() {
Vector3 e;
if(Input.GetMouseButtonDown(0)) {
spppp = new List ();
List points = new List ();
linesPoint.Add (points);
}
if(Input.GetMouseButton(0)) {
spppp.Add (GetNewPoint ());
linesPoint [linesPoint.Count-1].Add (GetNewPoint());
}
if(Input.GetMouseButtonUp(0)) {
}
}
Vector3 GetNewPoint() {
return g.transform.InverseTransformPoint(
Camera.main.ScreenToWorldPoint(
new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.z * -1.0f)
)
);
}
void OnPostRender() {
m.SetPass(0);
GL.PushMatrix();
GL.MultMatrix(g.transform.transform.localToWorldMatrix);
GL.Begin( GL.LINES );
GL.Color( new Color(1,1,1,1) );
for (int i = 0; i < spppp.Count-2; i++) {
GL.Vertex3 (spppp [i].x, spppp [i].y, spppp [i].z);
GL.Vertex3 (spppp [i+1].x, spppp [i+1].y, spppp [i+1].z);
}
for (int i = 0; i < linesPoint.Count; i++) {
for (int j = 0; j < linesPoint[i].Count-1; j++) {
GL.Vertex3 (linesPoint [i][j].x, linesPoint [i][j].y, linesPoint [i][j].z);
GL.Vertex3 (linesPoint [i][j+1].x, linesPoint [i][j+1].y, linesPoint [i][j+1].z);
//GL.Vertex3 (spppp [j+1].x, spppp [j+1].y, spppp [j+1].z);
}
}
GL.End();
GL.PopMatrix();
}
}