動作確認
Unity 5.1.2 on MacOS X
とりあえず折れ線を引くコードはできた。
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List<>
public class GenerateLR : MonoBehaviour {
void DrawLine(List<Vector2> my2DVec, int startPos) {
List<Vector3> myPoint = new List<Vector3>();
for(int idx=0; idx<2; idx++) {
myPoint.Add(new Vector3(my2DVec[startPos+idx].x, my2DVec[startPos+idx].y, 0.0f));
}
GameObject newLine = new GameObject ("Line");
LineRenderer lRend = newLine.AddComponent<LineRenderer> ();
lRend.SetVertexCount(2);
lRend.SetWidth (0.1f, 0.1f);
Vector3 startVec = myPoint[0];
Vector3 endVec = myPoint[1];
lRend.SetPosition (0, startVec);
lRend.SetPosition (1, endVec);
}
void Start () {
List<Vector2> my2DPoint = new List<Vector2> ();
for (int idx=0; idx<100; idx++) {
my2DPoint.Add (new Vector2 (-10 + 0.2f * idx, Random.Range(0.0f, 5.0f)));
}
for (int idx=0; idx < my2DPoint.Count - 1; idx++) {
DrawLine (my2DPoint, /* startPos=*/idx);
}
}
void Update () {
}
}
grouping付き
以下ではlineの要素をLineGroupというGameObjectでまとめるようにした。
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List<>
public class GenerateLR : MonoBehaviour {
private GameObject lineGroup; // for grouping
void DrawLine(List<Vector2> my2DVec, int startPos) {
List<Vector3> myPoint = new List<Vector3>();
for(int idx=0; idx<2; idx++) {
myPoint.Add(new Vector3(my2DVec[startPos+idx].x, my2DVec[startPos+idx].y, 0.0f));
}
GameObject newLine = new GameObject ("Line" + startPos.ToString() );
LineRenderer lRend = newLine.AddComponent<LineRenderer> ();
lRend.SetVertexCount(2);
lRend.SetWidth (0.1f, 0.1f);
Vector3 startVec = myPoint[0];
Vector3 endVec = myPoint[1];
lRend.SetPosition (0, startVec);
lRend.SetPosition (1, endVec);
newLine.transform.parent = lineGroup.transform; // for grouping
}
void Start () {
lineGroup = new GameObject ("LineGroup");
List<Vector2> my2DPoint = new List<Vector2> ();
for (int idx=0; idx<100; idx++) {
my2DPoint.Add (new Vector2 (-10 + 0.2f * idx, Random.Range(0.0f, 5.0f)));
}
for (int idx=0; idx < my2DPoint.Count - 1; idx++) {
DrawLine (my2DPoint, /* startPos=*/idx);
}
}
void Update () {
}
}
関連
(追記: 2018/11/15)