LoginSignup
18
22

More than 5 years have passed since last update.

unity > グラフ描画 > LineRendererで折れ線を引く

Last updated at Posted at 2015-08-02
動作確認
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 () {

    }
}

Untitled_-_150802_lineRenderer_-_PC__Mac___Linux_Standalone__Personal_.jpg

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)

18
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
22