- Home /
Why does my array of LineRenders only render one line?
I don't quite get something. But, this is my first attempt at both Unity3D and C# so that doesn't really come as a surprise...;^) I have written other C# code that successfully uses a single LineRenderer to generate a line based on data read from an XML file. I want to generate several separate lines based on data from the XML file, where lines may have several segments, each with it's own LineRender. (Yes, I know there is something called Vectrosity out there...;^) (It doesn't appear to give me the kind of control I need over colors, I don't have a spare $30 at the moment, and I'd like to understand this anyway.))
I emulate the multiline input process here by just filling arrays with data. I've tried several variations on initializing the LineRenderers which I have left as comments in the code.
In the inspector I added an empty gameObject, attached a LineRenderer and my code. It runs without error, but, only shows one line segment in the game display. (I'm wondering if I need to add all the gameObjects and LineRenderers in the inspector and not rely on the script alone. I hope not!)
Also, it doesn't use the assigned colors for the segment it does render.
I'd really appreciate any input on what I'd doing wrong!
Sorry for not including the code formatted as code, but the Markdown formatting doesn't seem to work for me.
// Test of using array of LineRenderers in a script to generate segments of a
// line. Eventually to be incorporated into visualization data display. The
// array is two-dimensional. The first index counts the tracks, the second index
// counts the segments within a track. Doing the lines with individual 2 point
// LineRenderers allows changing the color in each segment for color mapping.
// Note the second index must be set to the largest number of track segments.
// NOTE: The hardcoded values used here will be determined by reading data from
// an XML file in the operational code. The segment endpoint colors will
// be determined by temperature color map. Endpoint positions will come
// from the data records.
using UnityEngine;
using System.Linq;
public class ArrayLineTest : MonoBehaviour {
// declare types public outside and set array sizes in Start() after read // data and get track and track record counts
public LineRenderer[,] tracks;
public int trkcnt;
public int[] segcnts;
public Color[,,] segclrs;
public int maxsegs;
public Vector3[,,] segpos;
void Start () {
// Here would read data and obtain data set sizes:
trkcnt = 3;
segcnts = new int[] {1,3,2}; // 3 tracks so must be 3 segment sets
maxsegs = segcnts.Max();
// Initialize LineRenderer array:
tracks = new LineRenderer[trkcnt,maxsegs];
// Here would determine start and end colors for segments based on
// temperature colormap (values here from CsharpColorNameTable.pdf/255)
segclrs = new Color[trkcnt,maxsegs,2];
// track 0, with 1 segment:
segclrs[0,0,0] = new Color(0f,0f,0f,1f); // Black
segclrs[0,0,1] = new Color(0f,1f,0f,1f); // Lime
// track 1, with 3 segments:
segclrs[1,0,0] = new Color(0.729f,0.333f,0.824f,1f); // MediumOrchid
segclrs[1,0,1] = new Color(1f,0.498f,0.314f,1f); // Coral
segclrs[1,1,0] = new Color(1f,0.498f,0.314f,1f);
segclrs[1,1,1] = new Color(0f,1f,1f,1f); // Cyan
segclrs[1,2,0] = new Color(0f,1f,1f,1f);
segclrs[1,2,1] = new Color(1f,0f,0f,1f); // Red
// track 2, with 2 segments:
segclrs[2,0,0] = new Color(0f,0f,0f,1f); // Black
segclrs[2,0,1] = new Color(0f,1f,0f,1f); // Lime
segclrs[2,1,0] = new Color(0f,1f,0f,1f);
segclrs[2,1,1] = new Color(0.729f,0.333f,0.824f,1f); // MediumOrchid
// Here would determine start and end positions for segments from data:
segpos = new Vector3[trkcnt,maxsegs,2]; // X Z Y values
// track 0, with 1 segment:
segpos[0,0,0] = new Vector3(1f,1f,1f);
segpos[0,0,1] = new Vector3(0f,0.5f,-1f);
// track 1, with 3 segments:
segpos[1,0,0] = new Vector3(-1f,-1f,-1f);
segpos[1,0,1] = new Vector3(0f,0.75f,-0.5f); // following start matches
segpos[1,1,0] = new Vector3(0f,0.75f,-0.5f); // previous end
segpos[1,1,1] = new Vector3(0.5f,0.75f,0f);
segpos[1,2,0] = new Vector3(0.5f,0.75f,0f);
segpos[1,2,1] = new Vector3(0.375f,0.375f,0f);
// track 2, with 2 segments:
segpos[2,0,0] = new Vector3(0f,0f,0f);
segpos[2,0,1] = new Vector3(0.75f,0.5f,0.375f);
segpos[2,1,0] = new Vector3(0.75f,0.5f,0.375f);
segpos[2,1,1] = new Vector3(0.1f,-0.3f,-0.25f);
// Create and load LineRenderers, attaching each to a gameObject:
for (int i=0; i<trkcnt; i++) {
int segcnt = segcnts[i];
for (int j=0; j<segcnt; j++) {
// tracks[i,j] = GetComponent<LineRenderer>() as LineRenderer;
// tracks[i,j] = GetComponent<LineRenderer>();
tracks[i,j] = gameObject.AddComponent<LineRenderer>(); // as
LineRenderer;
tracks[i,j].SetVertexCount(2);
tracks[i,j].SetColors(segclrs[i,j,0], segclrs[i,j,1]);
tracks[i,j].SetWidth(0.1f, 0.1f);
tracks[i,j].material = new
Material(Shader.Find("Particles/Additive"));
tracks[i,j].enabled = true;
}
}
}
// Update is called once per frame
void Update () {
for (int i=0; i<trkcnt; i++) {
int segcnt = segcnts[i];
for (int j=0; j<segcnt; j++) {
tracks[i,j].SetPosition(0, segpos[i,j,0]);
tracks[i,j].SetPosition(1, segpos[i,j,1]);
}
}
}
}
Answer by waterdog · Aug 28, 2013 at 06:58 AM
OK...Answering my own question. If anyone can point out improvements I'd be happy to see them. I cut my code from above down to it's most basic form and dropped all arrays in favor of hard coded parameters. Then, with enough head scratching and googling I finally put together all the pieces that never quite seem to show up all in one place at one time in all the available code snippets...;^)
The solution was to use empty GameObjects and attach a single LineRenderer to each of them. That much was pretty clear early on. But, how to actually do it...not so much. Here is what I came up with. It requires just an empty GameObject in the Inspector to attach the script to. The script handles everything else. It renders two line segments end to end with a continuous color gradient from the start of the first to the end of the second.
using UnityEngine;
public class HardCodeTest : MonoBehaviour {
public LineRenderer track0;
public LineRenderer track1;
public GameObject gameObj0;
public GameObject gameObj1;
public Color Black = new Color(0f,0f,0f,1f);
public Color Coral = new Color(1f,0.498f,0.314f,1f);
public Color Cyan = new Color(0f,1f,1f,1f);
public Color Lime = new Color(0f,1f,0f,1f);
public Color MediumOrchid = new Color(0.729f,0.333f,0.824f,1f);
public Color Red = new Color(1f,0f,0f,1f);
void Start () {
gameObj0 = new GameObject("track0_GO");
track0 = gameObj0.AddComponent<LineRenderer>();
track0.SetVertexCount(2);
track0.SetColors(Red, MediumOrchid);
track0.SetWidth(0.7f, 0.1f);
track0.material = new Material(Shader.Find("Particles/Additive"));
track0.enabled = true;
gameObj1 = new GameObject("track1_GO");
track1 = gameObj1.AddComponent<LineRenderer>();
track1.SetVertexCount(2);
track1.SetColors(MediumOrchid, Cyan);
track1.SetWidth(0.1f, 0.7f);
track1.material = new Material(Shader.Find("Particles/Additive"));
track1.enabled = true;
}
void Update () {
track0.SetPosition(0, new Vector3(0f,0f,0f));
track0.SetPosition(1, new Vector3(0.75f,0.5f,0.375f));
track1.SetPosition(0, new Vector3(0.75f,0.5f,0.375f));
track1.SetPosition(1, new Vector3(1.75f,0.5f,-0.375f));
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it possible to update variable in Coroutine? 1 Answer
Problem with Web GL Build 1 Answer
How to let multiple values contribute two one value constantly 1 Answer