LoginSignup
14
15

More than 5 years have passed since last update.

unity > LineRendererで1本の線を描く

Last updated at Posted at 2015-08-02
Unity 5.1.2 on MacOS X

LineRendererでグラフを描画したい。
第一歩として、1本の線を描く。

Main Cameraに以下のスクリプトを関連づける。
(LR: LineRenderer)

GenerateLR.cs
using UnityEngine;
using System.Collections;

public class GenerateLR : MonoBehaviour {

    void Start () {
        GameObject newLine = new GameObject ("Line");
        LineRenderer lRend = newLine.AddComponent<LineRenderer> ();
        lRend.SetVertexCount(2);
        lRend.SetWidth (0.2f, 0.2f);
        Vector3 startVec = new Vector3 (0.0f, 0.0f, 0.0f);
        Vector3 endVec = new Vector3 (5.0f, 5.0f, 0.0f);
        lRend.SetPosition (0, startVec);
        lRend.SetPosition (1, endVec);
    }

    void Update () {

    }
}

成果はこちら。

Untitled_-_150802_lineRenderer_-_PC__Mac___Linux_Standalone__Personal_.jpg

同じことをListを使って実装したもの

using UnityEngine;
using System.Collections;
using System.Collections.Generic; // for List<>

public class GenerateLR : MonoBehaviour {

    void Start () {
        List<Vector3> myPoint = new List<Vector3>();
        myPoint.Add (new Vector3 (0.0f, 0.0f, 0.0f));   
        myPoint.Add (new Vector3 (5.0f, 5.0f, 0.0f));

        GameObject newLine = new GameObject ("Line");
        LineRenderer lRend = newLine.AddComponent<LineRenderer> ();
        lRend.SetVertexCount(2);
        lRend.SetWidth (0.2f, 0.2f);
        Vector3 startVec = myPoint[0];
        Vector3 endVec = myPoint [1];
        lRend.SetPosition (0, startVec);
        lRend.SetPosition (1, endVec);
    }

    void Update () {

    }
}
14
15
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
14
15