line render some lines not visible
hello
I want to draw a grid on a panel using line render this is the code which i use to draw the lines
public class paint_grid : MonoBehaviour
{
private int tempXTiles;
private int tempYTiles;
private float tempPanelY;
private float tempPanelX;
private int screenX;
private int screenY;
private float size;
private LineRenderer line;
public int xTiles;
public int yTiles;
public LineRenderer prefabline;
public Material lineBlack;
public Material lineWhite;
// Start is called before the first frame update
void Start()
{
RectTransform panel = gameObject.GetComponent<RectTransform>();
line = Instantiate(prefabline,transform);
line.material = lineBlack;
drawGrid(panel);
}
// Update is called once per frame
void Update()
{
RectTransform panel = gameObject.GetComponent<RectTransform>();
if (tempXTiles != xTiles || tempYTiles != yTiles || screenX != Screen.width || screenY != Screen.height) {
drawGrid(panel);
}
}
private void drawGrid(RectTransform panel) {
tempPanelX = panel.rect.width;
tempPanelY = panel.rect.height;
float xSize = tempPanelX / xTiles;
float ySize = tempPanelY / yTiles;
if( xSize < ySize) {
size = xSize;
}
else {
size = ySize;
}
// tiles +1 = linen anzahl
// tiles = verbindungslinen anzahl
// +2 für anfangspunkt und endpunkt
int lineCount = (yTiles + 1 + yTiles + xTiles + 1 + xTiles) +2;
line.positionCount = lineCount;
Vector3[] v3 = new Vector3[lineCount];
float y = size * yTiles;
int yt1 = 0;
int yt2 = 1;
int xt1 = 0;
float x=0;
int i;
for (i =0; i< (yTiles + 1 + yTiles +1);) {
x = x + ((xt1 ) * size);
v3[i] = new Vector3(x , yt1 *(-y) , 0);
// Reihenfolge: 0 1 1 0 0 1 1...
yt1 = (yt1 + yt2) % 2;
yt2 = ( yt2 + 1) % 2;
// Reiehenfolge: 0 0 1 0 1...
xt1 = i % 2;
i = i + 1;
}
y = size * xTiles;
yt1 = 0;
yt2 = 1;
xt1 = 0;
x = 0;
for (; i < lineCount;) {
x = x + ((xt1) * size);
v3[i] = new Vector3(yt1 * (y), -x, 0);
// Reihenfolge: 0 1 1 0 0 1 1...
yt1 = (yt1 + yt2) % 2;
yt2 = (yt2 + 1) % 2;
// Reiehenfolge: 0 0 1 0 1...
xt1 = i % 2;
i = i + 1;
}
line.SetPositions(v3);
screenX = Screen.width;
screenY = Screen.height;
tempXTiles = xTiles;
tempYTiles = yTiles;
}
}
the lines points area all corectly in the the line render, however some lines are not visible while others are just fine. If I set the line size to 2 insteeed of 1 all lines are visible, but then some are ticker than other lines, which shouldn't be. additional Infos: the panel which I draw on is part of a canvas with render mode "Screen Space Camera" the Camera is set to "Ortographic" the material use for the lines is a material with shader "UI/Default" with black oder white color
Does anyone have a tip how i could solve my problem?
Answer by DGames01 · Nov 28, 2019 at 09:31 PM
i found a script to draw lines without the line render it uses the GUI to draw. It probably is not the best solution but at least it always draws all lines insteed of the line render which only draws half of them.
Answer by goutham12 · Nov 25, 2019 at 10:15 AM
just try modify this values in the start
int numCapVertices;
int numCornerVertices;
void Update()
{
lr.numCapVertices = numCapVertices;
lr.numCornerVertices = numCornerVertices;
}
void OnGUI()
{
GUI.Label(new Rect(25, 20, 200, 30), "Num Cap Vertices");
numCapVertices = (int)GUI.HorizontalSlider(new Rect(165, 25, 200, 30),
(float)numCapVertices, 0.0f, 20.0f);
GUI.Label(new Rect(25, 60, 200, 30), "Num Corner Vertices");
numCornerVertices = (int)GUI.HorizontalSlider(new Rect(165, 65, 200, 30),
(float)numCornerVertices, 0.0f, 20.0f);
}
Now try to adjust values untill you get the best result. in my can numCapVertices is 5 and numCornerVertices is 20.
for more details
https://docs.unity3d.com/ScriptReference/LineRenderer-numCornerVertices.html https://docs.unity3d.com/ScriptReference/LineRenderer-numCapVertices.html
I tried it but it but it seems to only slightly change the last line drawn by my code
Your answer
Follow this Question
Related Questions
issue with LineRender start from 0 0 Answers
Raycast line renderer not drawing correctly 0 Answers
Any way to save a trail renderer? 1 Answer