Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
20
Help us understand the problem. What is going on with this article?
@7of9

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

More than 1 year has passed since last update.
動作確認
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)

20
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
7of9
セブンオブナインです。Unimatrix 01の第三付属物 9の7という識別番号です。Star trek Voyagerの好きなキャラクターです。まとめ記事は後日タイトルから内容がわからなくなるため、title検索で見つかるよう個々の記事にしてます。いわゆるBorg集合体の有名なセリフから「お前たち(の知識)を吸収する。抵抗は無意味だ」。Thanks in advance.

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
20
Help us understand the problem. What is going on with this article?